test_pruneforums.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from datetime import timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from django.utils.six import StringIO
  5. from misago.threads import testutils
  6. from misago.forums.management.commands import pruneforums
  7. from misago.forums.models import Forum
  8. class PruneForumsTests(TestCase):
  9. def test_forum_prune_by_start_date(self):
  10. """command prunes forum content based on start date"""
  11. forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  12. forum.prune_started_after = 20
  13. forum.save()
  14. # post old threads with recent replies
  15. started_on = timezone.now() - timedelta(days=30)
  16. posted_on = timezone.now()
  17. for t in xrange(10):
  18. thread = testutils.post_thread(forum, started_on=started_on)
  19. testutils.reply_thread(thread, posted_on=posted_on)
  20. # post recent threads that will be preserved
  21. threads = [testutils.post_thread(forum) for t in xrange(10)]
  22. forum.synchronize()
  23. self.assertEqual(forum.threads, 20)
  24. self.assertEqual(forum.posts, 30)
  25. # run command
  26. command = pruneforums.Command()
  27. out = StringIO()
  28. command.execute(stdout=out)
  29. forum.synchronize()
  30. self.assertEqual(forum.threads, 10)
  31. self.assertEqual(forum.posts, 10)
  32. for thread in threads:
  33. forum.thread_set.get(id=thread.id)
  34. command_output = out.getvalue().strip()
  35. self.assertEqual(command_output, 'Forums were pruned')
  36. def test_forum_prune_by_last_reply(self):
  37. """command prunes forum content based on last reply date"""
  38. forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  39. forum.prune_replied_after = 20
  40. forum.save()
  41. # post old threads with recent replies
  42. started_on = timezone.now() - timedelta(days=30)
  43. for t in xrange(10):
  44. thread = testutils.post_thread(forum, started_on=started_on)
  45. testutils.reply_thread(thread)
  46. # post recent threads that will be preserved
  47. threads = [testutils.post_thread(forum) for t in xrange(10)]
  48. forum.synchronize()
  49. self.assertEqual(forum.threads, 20)
  50. self.assertEqual(forum.posts, 30)
  51. # run command
  52. command = pruneforums.Command()
  53. out = StringIO()
  54. command.execute(stdout=out)
  55. forum.synchronize()
  56. self.assertEqual(forum.threads, 10)
  57. self.assertEqual(forum.posts, 10)
  58. for thread in threads:
  59. forum.thread_set.get(id=thread.id)
  60. command_output = out.getvalue().strip()
  61. self.assertEqual(command_output, 'Forums were pruned')
  62. def test_forum_archive_by_start_date(self):
  63. """command archives forum content based on start date"""
  64. forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  65. archive = Forum.objects.all_forums().filter(role="category")[:1][0]
  66. forum.prune_started_after = 20
  67. forum.archive_pruned_in = archive
  68. forum.save()
  69. # post old threads with recent replies
  70. started_on = timezone.now() - timedelta(days=30)
  71. posted_on = timezone.now()
  72. for t in xrange(10):
  73. thread = testutils.post_thread(forum, started_on=started_on)
  74. testutils.reply_thread(thread, posted_on=posted_on)
  75. # post recent threads that will be preserved
  76. threads = [testutils.post_thread(forum) for t in xrange(10)]
  77. forum.synchronize()
  78. self.assertEqual(forum.threads, 20)
  79. self.assertEqual(forum.posts, 30)
  80. # run command
  81. command = pruneforums.Command()
  82. out = StringIO()
  83. command.execute(stdout=out)
  84. forum.synchronize()
  85. self.assertEqual(forum.threads, 10)
  86. self.assertEqual(forum.posts, 10)
  87. archive.synchronize()
  88. self.assertEqual(archive.threads, 10)
  89. self.assertEqual(archive.posts, 20)
  90. for thread in threads:
  91. forum.thread_set.get(id=thread.id)
  92. command_output = out.getvalue().strip()
  93. self.assertEqual(command_output, 'Forums were pruned')
  94. def test_forum_archive_by_last_reply(self):
  95. """command archives forum content based on last reply date"""
  96. forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  97. archive = Forum.objects.all_forums().filter(role="category")[:1][0]
  98. forum.prune_replied_after = 20
  99. forum.archive_pruned_in = archive
  100. forum.save()
  101. # post old threads with recent replies
  102. started_on = timezone.now() - timedelta(days=30)
  103. for t in xrange(10):
  104. thread = testutils.post_thread(forum, started_on=started_on)
  105. testutils.reply_thread(thread)
  106. # post recent threads that will be preserved
  107. threads = [testutils.post_thread(forum) for t in xrange(10)]
  108. forum.synchronize()
  109. self.assertEqual(forum.threads, 20)
  110. self.assertEqual(forum.posts, 30)
  111. # run command
  112. command = pruneforums.Command()
  113. out = StringIO()
  114. command.execute(stdout=out)
  115. forum.synchronize()
  116. self.assertEqual(forum.threads, 10)
  117. self.assertEqual(forum.posts, 10)
  118. archive.synchronize()
  119. self.assertEqual(archive.threads, 10)
  120. self.assertEqual(archive.posts, 20)
  121. for thread in threads:
  122. forum.thread_set.get(id=thread.id)
  123. command_output = out.getvalue().strip()
  124. self.assertEqual(command_output, 'Forums were pruned')