test_forum_model.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from django.test import TestCase
  2. from django.utils import timezone
  3. from misago.threads import testutils
  4. from misago.forums.models import FORUMS_TREE_ID, Forum
  5. class ForumManagerTests(TestCase):
  6. def test_private_threads(self):
  7. """private_threads returns private threads forum"""
  8. forum = Forum.objects.private_threads()
  9. self.assertEqual(forum.special_role, 'private_threads')
  10. def test_root_category(self):
  11. """root_category returns forums tree root"""
  12. forum = Forum.objects.root_category()
  13. self.assertEqual(forum.special_role, 'root_category')
  14. def test_all_forums(self):
  15. """all_forums returns queryset with forums tree"""
  16. root = Forum.objects.root_category()
  17. test_forum_a = Forum(name='Test', role='category')
  18. test_forum_a.insert_at(root,
  19. position='last-child',
  20. save=True)
  21. test_forum_b = Forum(name='Test 2', role='category')
  22. test_forum_b.insert_at(root,
  23. position='last-child',
  24. save=True)
  25. all_forums_from_db = [f for f in Forum.objects.all_forums(True)]
  26. self.assertIn(test_forum_a, all_forums_from_db)
  27. self.assertIn(test_forum_b, all_forums_from_db)
  28. def test_get_forums_dict_from_db(self):
  29. """get_forums_dict_from_db returns dict with forums"""
  30. test_dict = Forum.objects.get_forums_dict_from_db()
  31. for forum in Forum.objects.all():
  32. if forum.tree_id == FORUMS_TREE_ID:
  33. self.assertIn(forum.id, test_dict)
  34. else:
  35. self.assertNotIn(forum.id, test_dict)
  36. class ForumModelTests(TestCase):
  37. def setUp(self):
  38. self.forum = Forum.objects.filter(role="forum")[:1][0]
  39. def create_thread(self):
  40. datetime = timezone.now()
  41. thread = testutils.post_thread(self.forum)
  42. return thread
  43. def assertForumIsEmpty(self):
  44. self.assertIsNone(self.forum.last_post_on)
  45. self.assertIsNone(self.forum.last_thread)
  46. self.assertIsNone(self.forum.last_thread_title)
  47. self.assertIsNone(self.forum.last_thread_slug)
  48. self.assertIsNone(self.forum.last_poster)
  49. self.assertIsNone(self.forum.last_poster_name)
  50. self.assertIsNone(self.forum.last_poster_slug)
  51. def test_synchronize(self):
  52. """forum synchronization works"""
  53. self.forum.synchronize()
  54. self.assertEqual(self.forum.threads, 0)
  55. self.assertEqual(self.forum.posts, 0)
  56. thread = self.create_thread()
  57. hidden = self.create_thread()
  58. moderated = self.create_thread()
  59. self.forum.synchronize()
  60. self.assertEqual(self.forum.threads, 3)
  61. self.assertEqual(self.forum.posts, 3)
  62. self.assertEqual(self.forum.last_thread, moderated)
  63. moderated.is_moderated = True
  64. moderated.post_set.update(is_moderated=True)
  65. moderated.save()
  66. self.forum.synchronize()
  67. self.assertEqual(self.forum.threads, 2)
  68. self.assertEqual(self.forum.posts, 2)
  69. self.assertEqual(self.forum.last_thread, hidden)
  70. hidden.is_hidden = True
  71. hidden.post_set.update(is_hidden=True)
  72. hidden.save()
  73. self.forum.synchronize()
  74. self.assertEqual(self.forum.threads, 2)
  75. self.assertEqual(self.forum.posts, 2)
  76. self.assertEqual(self.forum.last_thread, hidden)
  77. moderated.is_moderated = False
  78. moderated.post_set.update(is_moderated=False)
  79. moderated.save()
  80. self.forum.synchronize()
  81. self.assertEqual(self.forum.threads, 3)
  82. self.assertEqual(self.forum.posts, 3)
  83. self.assertEqual(self.forum.last_thread, moderated)
  84. def test_delete_content(self):
  85. """delete_content empties forum"""
  86. for i in xrange(10):
  87. self.create_thread()
  88. self.forum.synchronize()
  89. self.assertEqual(self.forum.threads, 10)
  90. self.assertEqual(self.forum.posts, 10)
  91. self.forum.delete_content()
  92. self.forum.synchronize()
  93. self.assertEqual(self.forum.threads, 0)
  94. self.assertEqual(self.forum.posts, 0)
  95. self.assertForumIsEmpty()
  96. def test_move_content(self):
  97. """move_content moves forum threads and posts to other forum"""
  98. for i in xrange(10):
  99. self.create_thread()
  100. self.forum.synchronize()
  101. # we are using category so we don't have to fake another forum
  102. new_forum = Forum.objects.filter(role="category")[:1][0]
  103. self.forum.move_content(new_forum)
  104. self.forum.synchronize()
  105. new_forum.synchronize()
  106. self.assertEqual(self.forum.threads, 0)
  107. self.assertEqual(self.forum.posts, 0)
  108. self.assertForumIsEmpty()
  109. self.assertEqual(new_forum.threads, 10)
  110. self.assertEqual(new_forum.posts, 10)
  111. def test_set_last_thread(self):
  112. """set_last_thread changes forum's last thread"""
  113. self.forum.synchronize()
  114. new_thread = self.create_thread()
  115. self.forum.set_last_thread(new_thread)
  116. self.assertEqual(self.forum.last_post_on, new_thread.last_post_on)
  117. self.assertEqual(self.forum.last_thread, new_thread)
  118. self.assertEqual(self.forum.last_thread_title, new_thread.title)
  119. self.assertEqual(self.forum.last_thread_slug, new_thread.slug)
  120. self.assertEqual(self.forum.last_poster, new_thread.last_poster)
  121. self.assertEqual(self.forum.last_poster_name,
  122. new_thread.last_poster_name)
  123. self.assertEqual(self.forum.last_poster_slug,
  124. new_thread.last_poster_slug)
  125. def test_empty_last_thread(self):
  126. """empty_last_thread empties last forum thread"""
  127. self.create_thread()
  128. self.forum.synchronize()
  129. self.forum.empty_last_thread()
  130. self.assertForumIsEmpty()