test_synchronizethreads.py 1.4 KB

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