test_prunecategories.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. from datetime import timedelta
  2. from io import StringIO
  3. from django.core.management import call_command
  4. from django.test import TestCase
  5. from django.utils import timezone
  6. from misago.categories.management.commands import prunecategories
  7. from misago.categories.models import Category
  8. from misago.threads import testutils
  9. class PruneCategoriesTests(TestCase):
  10. def test_category_prune_by_start_date(self):
  11. """command prunes category content based on start date"""
  12. category = Category.objects.all_categories()[:1][0]
  13. category.prune_started_after = 20
  14. category.save()
  15. # post old threads with recent replies
  16. started_on = timezone.now() - timedelta(days=30)
  17. posted_on = timezone.now()
  18. for _ in range(10):
  19. thread = testutils.post_thread(category, started_on=started_on)
  20. testutils.reply_thread(thread, posted_on=posted_on)
  21. # post recent threads that will be preserved
  22. threads = [testutils.post_thread(category) for _ in range(10)]
  23. category.synchronize()
  24. self.assertEqual(category.threads, 20)
  25. self.assertEqual(category.posts, 30)
  26. # run command
  27. command = prunecategories.Command()
  28. out = StringIO()
  29. call_command(command, stdout=out)
  30. category.synchronize()
  31. self.assertEqual(category.threads, 10)
  32. self.assertEqual(category.posts, 10)
  33. for thread in threads:
  34. category.thread_set.get(id=thread.id)
  35. command_output = out.getvalue().strip()
  36. self.assertEqual(command_output, 'Categories were pruned')
  37. def test_category_prune_by_last_reply(self):
  38. """command prunes category content based on last reply date"""
  39. category = Category.objects.all_categories()[:1][0]
  40. category.prune_replied_after = 20
  41. category.save()
  42. # post old threads with recent replies
  43. started_on = timezone.now() - timedelta(days=30)
  44. for _ in range(10):
  45. thread = testutils.post_thread(category, started_on=started_on)
  46. testutils.reply_thread(thread)
  47. # post recent threads that will be preserved
  48. threads = [testutils.post_thread(category) for _ in range(10)]
  49. category.synchronize()
  50. self.assertEqual(category.threads, 20)
  51. self.assertEqual(category.posts, 30)
  52. # run command
  53. command = prunecategories.Command()
  54. out = StringIO()
  55. call_command(command, stdout=out)
  56. category.synchronize()
  57. self.assertEqual(category.threads, 10)
  58. self.assertEqual(category.posts, 10)
  59. for thread in threads:
  60. category.thread_set.get(id=thread.id)
  61. command_output = out.getvalue().strip()
  62. self.assertEqual(command_output, 'Categories were pruned')
  63. def test_category_archive_by_start_date(self):
  64. """command archives category content based on start date"""
  65. category = Category.objects.all_categories()[:1][0]
  66. archive = Category.objects.create(
  67. lft=7,
  68. rght=8,
  69. tree_id=2,
  70. level=0,
  71. name='Archive',
  72. slug='archive',
  73. )
  74. category.prune_started_after = 20
  75. category.archive_pruned_in = archive
  76. category.save()
  77. # post old threads with recent replies
  78. started_on = timezone.now() - timedelta(days=30)
  79. posted_on = timezone.now()
  80. for _ in range(10):
  81. thread = testutils.post_thread(category, started_on=started_on)
  82. testutils.reply_thread(thread, posted_on=posted_on)
  83. # post recent threads that will be preserved
  84. threads = [testutils.post_thread(category) for _ in range(10)]
  85. category.synchronize()
  86. self.assertEqual(category.threads, 20)
  87. self.assertEqual(category.posts, 30)
  88. # run command
  89. command = prunecategories.Command()
  90. out = StringIO()
  91. call_command(command, stdout=out)
  92. category.synchronize()
  93. self.assertEqual(category.threads, 10)
  94. self.assertEqual(category.posts, 10)
  95. archive.synchronize()
  96. self.assertEqual(archive.threads, 10)
  97. self.assertEqual(archive.posts, 20)
  98. for thread in threads:
  99. category.thread_set.get(id=thread.id)
  100. command_output = out.getvalue().strip()
  101. self.assertEqual(command_output, 'Categories were pruned')
  102. def test_category_archive_by_last_reply(self):
  103. """command archives category content based on last reply date"""
  104. category = Category.objects.all_categories()[:1][0]
  105. archive = Category.objects.create(
  106. lft=7,
  107. rght=8,
  108. tree_id=2,
  109. level=0,
  110. name='Archive',
  111. slug='archive',
  112. )
  113. category.prune_replied_after = 20
  114. category.archive_pruned_in = archive
  115. category.save()
  116. # post old threads with recent replies
  117. started_on = timezone.now() - timedelta(days=30)
  118. for _ in range(10):
  119. thread = testutils.post_thread(category, started_on=started_on)
  120. testutils.reply_thread(thread)
  121. # post recent threads that will be preserved
  122. threads = [testutils.post_thread(category) for _ in range(10)]
  123. category.synchronize()
  124. self.assertEqual(category.threads, 20)
  125. self.assertEqual(category.posts, 30)
  126. # run command
  127. command = prunecategories.Command()
  128. out = StringIO()
  129. call_command(command, stdout=out)
  130. category.synchronize()
  131. self.assertEqual(category.threads, 10)
  132. self.assertEqual(category.posts, 10)
  133. archive.synchronize()
  134. self.assertEqual(archive.threads, 10)
  135. self.assertEqual(archive.posts, 20)
  136. for thread in threads:
  137. category.thread_set.get(id=thread.id)
  138. command_output = out.getvalue().strip()
  139. self.assertEqual(command_output, 'Categories were pruned')