test_prunecategories.py 5.9 KB

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