123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- from datetime import timedelta
- from django.test import TestCase
- from django.utils import timezone
- from django.utils.six import StringIO
- from misago.threads import testutils
- from misago.forums.management.commands import pruneforums
- from misago.forums.models import Forum
- class PruneForumsTests(TestCase):
- def test_forum_prune_by_start_date(self):
- """command prunes forum content based on start date"""
- forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
- forum.prune_started_after = 20
- forum.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- posted_on = timezone.now()
- for t in xrange(10):
- thread = testutils.post_thread(forum, started_on=started_on)
- testutils.reply_thread(thread, posted_on=posted_on)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(forum) for t in xrange(10)]
- forum.synchronize()
- self.assertEqual(forum.threads, 20)
- self.assertEqual(forum.posts, 30)
- # run command
- command = pruneforums.Command()
- out = StringIO()
- command.execute(stdout=out)
- forum.synchronize()
- self.assertEqual(forum.threads, 10)
- self.assertEqual(forum.posts, 10)
- for thread in threads:
- forum.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Forums were pruned')
- def test_forum_prune_by_last_reply(self):
- """command prunes forum content based on last reply date"""
- forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
- forum.prune_replied_after = 20
- forum.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- for t in xrange(10):
- thread = testutils.post_thread(forum, started_on=started_on)
- testutils.reply_thread(thread)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(forum) for t in xrange(10)]
- forum.synchronize()
- self.assertEqual(forum.threads, 20)
- self.assertEqual(forum.posts, 30)
- # run command
- command = pruneforums.Command()
- out = StringIO()
- command.execute(stdout=out)
- forum.synchronize()
- self.assertEqual(forum.threads, 10)
- self.assertEqual(forum.posts, 10)
- for thread in threads:
- forum.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Forums were pruned')
- def test_forum_archive_by_start_date(self):
- """command archives forum content based on start date"""
- forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
- archive = Forum.objects.all_forums().filter(role="category")[:1][0]
- forum.prune_started_after = 20
- forum.archive_pruned_in = archive
- forum.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- posted_on = timezone.now()
- for t in xrange(10):
- thread = testutils.post_thread(forum, started_on=started_on)
- testutils.reply_thread(thread, posted_on=posted_on)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(forum) for t in xrange(10)]
- forum.synchronize()
- self.assertEqual(forum.threads, 20)
- self.assertEqual(forum.posts, 30)
- # run command
- command = pruneforums.Command()
- out = StringIO()
- command.execute(stdout=out)
- forum.synchronize()
- self.assertEqual(forum.threads, 10)
- self.assertEqual(forum.posts, 10)
- archive.synchronize()
- self.assertEqual(archive.threads, 10)
- self.assertEqual(archive.posts, 20)
- for thread in threads:
- forum.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Forums were pruned')
- def test_forum_archive_by_last_reply(self):
- """command archives forum content based on last reply date"""
- forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
- archive = Forum.objects.all_forums().filter(role="category")[:1][0]
- forum.prune_replied_after = 20
- forum.archive_pruned_in = archive
- forum.save()
- # post old threads with recent replies
- started_on = timezone.now() - timedelta(days=30)
- for t in xrange(10):
- thread = testutils.post_thread(forum, started_on=started_on)
- testutils.reply_thread(thread)
- # post recent threads that will be preserved
- threads = [testutils.post_thread(forum) for t in xrange(10)]
- forum.synchronize()
- self.assertEqual(forum.threads, 20)
- self.assertEqual(forum.posts, 30)
- # run command
- command = pruneforums.Command()
- out = StringIO()
- command.execute(stdout=out)
- forum.synchronize()
- self.assertEqual(forum.threads, 10)
- self.assertEqual(forum.posts, 10)
- archive.synchronize()
- self.assertEqual(archive.threads, 10)
- self.assertEqual(archive.posts, 20)
- for thread in threads:
- forum.thread_set.get(id=thread.id)
- command_output = out.getvalue().strip()
- self.assertEqual(command_output, 'Forums were pruned')
|