test_synchronizethreads.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from io import StringIO
  2. from django.core.management import call_command
  3. from django.test import TestCase
  4. from misago.categories.models import Category
  5. from misago.threads import testutils
  6. from misago.threads.management.commands import synchronizethreads
  7. class SynchronizeThreadsTests(TestCase):
  8. def test_no_threads_sync(self):
  9. """command works when there are no threads"""
  10. command = synchronizethreads.Command()
  11. out = StringIO()
  12. call_command(command, stdout=out)
  13. command_output = out.getvalue().strip()
  14. self.assertEqual(command_output, "No threads were found")
  15. def test_threads_sync(self):
  16. """command synchronizes threads"""
  17. category = Category.objects.all_categories()[:1][0]
  18. threads = [testutils.post_thread(category) for _ in range(10)]
  19. for i, thread in enumerate(threads):
  20. [testutils.reply_thread(thread) for _ in range(i)]
  21. thread.replies = 0
  22. thread.save()
  23. command = synchronizethreads.Command()
  24. out = StringIO()
  25. call_command(command, stdout=out)
  26. for i, thread in enumerate(threads):
  27. db_thread = category.thread_set.get(id=thread.id)
  28. self.assertEqual(db_thread.replies, i)
  29. command_output = out.getvalue().splitlines()[-1].strip()
  30. self.assertEqual(command_output, "Synchronized 10 threads")