123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- from datetime import timedelta
- from django.core.management import call_command
- from django.test import TestCase
- from django.utils import timezone
- from django.utils.six import StringIO
- from django.utils.six.moves import range
- from misago.threads import testutils
- from ..management.commands import prunecategories
- from ..models import Category
- class PruneCategoriesTests(TestCase):
- def test_category_prune_by_start_date(self):
- """command prunes category content based on start date"""
- category = Category.objects.all_categories()[:1][0]
- category.prune_started_after = 20
- category.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- posted_on = timezone.now()
- for t in range(10):
- thread = testutils.post_thread(category, started_on=started_on)
- testutils.reply_thread(thread, posted_on=posted_on)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(category) for t in range(10)]
- category.synchronize()
- self.assertEqual(category.threads, 20)
- self.assertEqual(category.posts, 30)
- # run command
- command = prunecategories.Command()
- out = StringIO()
- call_command(command, stdout=out)
- category.synchronize()
- self.assertEqual(category.threads, 10)
- self.assertEqual(category.posts, 10)
- for thread in threads:
- category.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Categories were pruned')
- def test_category_prune_by_last_reply(self):
- """command prunes category content based on last reply date"""
- category = Category.objects.all_categories()[:1][0]
- category.prune_replied_after = 20
- category.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- for t in range(10):
- thread = testutils.post_thread(category, started_on=started_on)
- testutils.reply_thread(thread)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(category) for t in range(10)]
- category.synchronize()
- self.assertEqual(category.threads, 20)
- self.assertEqual(category.posts, 30)
- # run command
- command = prunecategories.Command()
- out = StringIO()
- call_command(command, stdout=out)
- category.synchronize()
- self.assertEqual(category.threads, 10)
- self.assertEqual(category.posts, 10)
- for thread in threads:
- category.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Categories were pruned')
- def test_category_archive_by_start_date(self):
- """command archives category content based on start date"""
- category = Category.objects.all_categories()[:1][0]
- archive = Category.objects.create(
- lft=7,
- rght=8,
- tree_id=2,
- level=0,
- name='Archive',
- slug='archive',
- )
- category.prune_started_after = 20
- category.archive_pruned_in = archive
- category.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- posted_on = timezone.now()
- for t in range(10):
- thread = testutils.post_thread(category, started_on=started_on)
- testutils.reply_thread(thread, posted_on=posted_on)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(category) for t in range(10)]
- category.synchronize()
- self.assertEqual(category.threads, 20)
- self.assertEqual(category.posts, 30)
- # run command
- command = prunecategories.Command()
- out = StringIO()
- call_command(command, stdout=out)
- category.synchronize()
- self.assertEqual(category.threads, 10)
- self.assertEqual(category.posts, 10)
- archive.synchronize()
- self.assertEqual(archive.threads, 10)
- self.assertEqual(archive.posts, 20)
- for thread in threads:
- category.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Categories were pruned')
- def test_category_archive_by_last_reply(self):
- """command archives category content based on last reply date"""
- category = Category.objects.all_categories()[:1][0]
- archive = Category.objects.create(
- lft=7,
- rght=8,
- tree_id=2,
- level=0,
- name='Archive',
- slug='archive',
- )
- category.prune_replied_after = 20
- category.archive_pruned_in = archive
- category.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- for t in range(10):
- thread = testutils.post_thread(category, started_on=started_on)
- testutils.reply_thread(thread)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(category) for t in range(10)]
- category.synchronize()
- self.assertEqual(category.threads, 20)
- self.assertEqual(category.posts, 30)
- # run command
- command = prunecategories.Command()
- out = StringIO()
- call_command(command, stdout=out)
- category.synchronize()
- self.assertEqual(category.threads, 10)
- self.assertEqual(category.posts, 10)
- archive.synchronize()
- self.assertEqual(archive.threads, 10)
- self.assertEqual(archive.posts, 20)
- for thread in threads:
- category.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Categories were pruned')
|