test_prunecategories.py 5.8 KB

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