test_forum_model.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. post = testutils.reply_thread(thread)
  43. return thread
  44. def assertForumIsEmpty(self):
  45. self.assertIsNone(self.forum.last_post_on)
  46. self.assertIsNone(self.forum.last_thread)
  47. self.assertIsNone(self.forum.last_thread_title)
  48. self.assertIsNone(self.forum.last_thread_slug)
  49. self.assertIsNone(self.forum.last_poster)
  50. self.assertIsNone(self.forum.last_poster_name)
  51. self.assertIsNone(self.forum.last_poster_slug)
  52. def test_synchronize(self):
  53. """forum synchronization works"""
  54. self.forum.synchronize()
  55. self.assertEqual(self.forum.threads, 0)
  56. self.assertEqual(self.forum.posts, 0)
  57. thread = self.create_thread()
  58. hidden = self.create_thread()
  59. moderated = self.create_thread()
  60. self.forum.synchronize()
  61. self.assertEqual(self.forum.threads, 3)
  62. self.assertEqual(self.forum.posts, 3)
  63. self.assertEqual(self.forum.last_thread, moderated)
  64. moderated.is_moderated = True
  65. moderated.post_set.update(is_moderated=True)
  66. moderated.save()
  67. self.forum.synchronize()
  68. self.assertEqual(self.forum.threads, 2)
  69. self.assertEqual(self.forum.posts, 2)
  70. self.assertEqual(self.forum.last_thread, hidden)
  71. hidden.is_hidden = True
  72. hidden.post_set.update(is_hidden=True)
  73. hidden.save()
  74. self.forum.synchronize()
  75. self.assertEqual(self.forum.threads, 2)
  76. self.assertEqual(self.forum.posts, 2)
  77. self.assertEqual(self.forum.last_thread, hidden)
  78. moderated.is_moderated = False
  79. moderated.post_set.update(is_moderated=False)
  80. moderated.save()
  81. self.forum.synchronize()
  82. self.assertEqual(self.forum.threads, 3)
  83. self.assertEqual(self.forum.posts, 3)
  84. self.assertEqual(self.forum.last_thread, moderated)
  85. def test_delete_content(self):
  86. """delete_content empties forum"""
  87. for i in xrange(10):
  88. self.create_thread()
  89. self.forum.synchronize()
  90. self.assertEqual(self.forum.threads, 10)
  91. self.assertEqual(self.forum.posts, 10)
  92. self.forum.delete_content()
  93. self.forum.synchronize()
  94. self.assertEqual(self.forum.threads, 0)
  95. self.assertEqual(self.forum.posts, 0)
  96. self.assertForumIsEmpty()
  97. def test_move_content(self):
  98. """move_content moves forum threads and posts to other forum"""
  99. for i in xrange(10):
  100. self.create_thread()
  101. self.forum.synchronize()
  102. # we are using category so we don't have to fake another forum
  103. new_forum = Forum.objects.filter(role="category")[:1][0]
  104. self.forum.move_content(new_forum)
  105. self.forum.synchronize()
  106. new_forum.synchronize()
  107. self.assertEqual(self.forum.threads, 0)
  108. self.assertEqual(self.forum.posts, 0)
  109. self.assertForumIsEmpty()
  110. self.assertEqual(new_forum.threads, 10)
  111. self.assertEqual(new_forum.posts, 10)
  112. def test_set_last_thread(self):
  113. """set_last_thread changes forum's last thread"""
  114. self.forum.synchronize()
  115. new_thread = self.create_thread()
  116. self.forum.set_last_thread(new_thread)
  117. self.assertEqual(self.forum.last_post_on, new_thread.last_post_on)
  118. self.assertEqual(self.forum.last_thread, new_thread)
  119. self.assertEqual(self.forum.last_thread_title, new_thread.title)
  120. self.assertEqual(self.forum.last_thread_slug, new_thread.slug)
  121. self.assertEqual(self.forum.last_poster, new_thread.last_poster)
  122. self.assertEqual(self.forum.last_poster_name,
  123. new_thread.last_poster_name)
  124. self.assertEqual(self.forum.last_poster_slug,
  125. new_thread.last_poster_slug)
  126. def test_empty_last_thread(self):
  127. """empty_last_thread empties last forum thread"""
  128. self.create_thread()
  129. self.forum.synchronize()
  130. self.forum.empty_last_thread()
  131. self.assertForumIsEmpty()