test_prunecategories.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ...threads import test
  7. from ..management.commands import prunecategories
  8. from ..models import Category
  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 = test.post_thread(category, started_on=started_on)
  20. test.reply_thread(thread, posted_on=posted_on)
  21. # post recent threads that will be preserved
  22. threads = [test.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 = test.post_thread(category, started_on=started_on)
  46. test.reply_thread(thread)
  47. # post recent threads that will be preserved
  48. threads = [test.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, rght=8, tree_id=2, level=0, name="Archive", slug="archive"
  68. )
  69. category.prune_started_after = 20
  70. category.archive_pruned_in = archive
  71. category.save()
  72. # post old threads with recent replies
  73. started_on = timezone.now() - timedelta(days=30)
  74. posted_on = timezone.now()
  75. for _ in range(10):
  76. thread = test.post_thread(category, started_on=started_on)
  77. test.reply_thread(thread, posted_on=posted_on)
  78. # post recent threads that will be preserved
  79. threads = [test.post_thread(category) for _ in range(10)]
  80. category.synchronize()
  81. self.assertEqual(category.threads, 20)
  82. self.assertEqual(category.posts, 30)
  83. # run command
  84. command = prunecategories.Command()
  85. out = StringIO()
  86. call_command(command, stdout=out)
  87. category.synchronize()
  88. self.assertEqual(category.threads, 10)
  89. self.assertEqual(category.posts, 10)
  90. archive.synchronize()
  91. self.assertEqual(archive.threads, 10)
  92. self.assertEqual(archive.posts, 20)
  93. for thread in threads:
  94. category.thread_set.get(id=thread.id)
  95. command_output = out.getvalue().strip()
  96. self.assertEqual(command_output, "Categories were pruned")
  97. def test_category_archive_by_last_reply(self):
  98. """command archives category content based on last reply date"""
  99. category = Category.objects.all_categories()[:1][0]
  100. archive = Category.objects.create(
  101. lft=7, rght=8, tree_id=2, level=0, name="Archive", slug="archive"
  102. )
  103. category.prune_replied_after = 20
  104. category.archive_pruned_in = archive
  105. category.save()
  106. # post old threads with recent replies
  107. started_on = timezone.now() - timedelta(days=30)
  108. for _ in range(10):
  109. thread = test.post_thread(category, started_on=started_on)
  110. test.reply_thread(thread)
  111. # post recent threads that will be preserved
  112. threads = [test.post_thread(category) for _ in range(10)]
  113. category.synchronize()
  114. self.assertEqual(category.threads, 20)
  115. self.assertEqual(category.posts, 30)
  116. # run command
  117. command = prunecategories.Command()
  118. out = StringIO()
  119. call_command(command, stdout=out)
  120. category.synchronize()
  121. self.assertEqual(category.threads, 10)
  122. self.assertEqual(category.posts, 10)
  123. archive.synchronize()
  124. self.assertEqual(archive.threads, 10)
  125. self.assertEqual(archive.posts, 20)
  126. for thread in threads:
  127. category.thread_set.get(id=thread.id)
  128. command_output = out.getvalue().strip()
  129. self.assertEqual(command_output, "Categories were pruned")