test_forum_model.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from django.test import TestCase
  2. from django.utils import timezone
  3. from misago.threads.models import Thread, Post
  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 = Thread(
  42. forum=self.forum,
  43. weight=0,
  44. started_on=datetime,
  45. starter_name='Tester',
  46. starter_slug='tester',
  47. last_post_on=datetime,
  48. last_poster_name='Tester',
  49. last_poster_slug='tester')
  50. thread.set_title("Test thread")
  51. thread.save()
  52. post = Post.objects.create(
  53. forum=self.forum,
  54. thread=thread,
  55. poster_name='Tester',
  56. poster_ip='127.0.0.1',
  57. original="Hello! I am test message!",
  58. parsed="<p>Hello! I am test message!</p>",
  59. checksum="nope",
  60. posted_on=datetime,
  61. updated_on=datetime)
  62. thread.first_post = post
  63. thread.last_post = post
  64. thread.save()
  65. return thread
  66. def assertForumIsEmpty(self):
  67. self.assertIsNone(self.forum.last_post_on)
  68. self.assertIsNone(self.forum.last_thread)
  69. self.assertIsNone(self.forum.last_thread_title)
  70. self.assertIsNone(self.forum.last_thread_slug)
  71. self.assertIsNone(self.forum.last_poster)
  72. self.assertIsNone(self.forum.last_poster_name)
  73. self.assertIsNone(self.forum.last_poster_slug)
  74. def test_synchronize(self):
  75. """forum synchronization works"""
  76. self.forum.synchronize()
  77. self.assertEqual(self.forum.threads, 0)
  78. self.assertEqual(self.forum.posts, 0)
  79. thread = self.create_thread()
  80. hidden = self.create_thread()
  81. moderated = self.create_thread()
  82. self.forum.synchronize()
  83. self.assertEqual(self.forum.threads, 3)
  84. self.assertEqual(self.forum.posts, 3)
  85. self.assertEqual(self.forum.last_thread, moderated)
  86. moderated.is_moderated = True
  87. moderated.post_set.update(is_moderated=True)
  88. moderated.save()
  89. self.forum.synchronize()
  90. self.assertEqual(self.forum.threads, 2)
  91. self.assertEqual(self.forum.posts, 2)
  92. self.assertEqual(self.forum.last_thread, hidden)
  93. hidden.is_hidden = True
  94. hidden.post_set.update(is_hidden=True)
  95. hidden.save()
  96. self.forum.synchronize()
  97. self.assertEqual(self.forum.threads, 2)
  98. self.assertEqual(self.forum.posts, 2)
  99. self.assertEqual(self.forum.last_thread, hidden)
  100. moderated.is_moderated = False
  101. moderated.post_set.update(is_moderated=False)
  102. moderated.save()
  103. self.forum.synchronize()
  104. self.assertEqual(self.forum.threads, 2)
  105. self.assertEqual(self.forum.posts, 2)
  106. self.assertEqual(self.forum.last_thread, moderated)
  107. def test_delete_content(self):
  108. """delete_content empties forum"""
  109. for i in xrange(10):
  110. self.create_thread()
  111. self.forum.synchronize()
  112. self.assertEqual(self.forum.threads, 10)
  113. self.assertEqual(self.forum.posts, 10)
  114. self.forum.delete_content()
  115. self.forum.synchronize()
  116. self.assertEqual(self.forum.threads, 0)
  117. self.assertEqual(self.forum.posts, 0)
  118. self.assertForumIsEmpty()
  119. def test_move_content(self):
  120. """move_content moves forum threads and posts to other forum"""
  121. for i in xrange(10):
  122. self.create_thread()
  123. self.forum.synchronize()
  124. # we are using category so we don't have to fake another forum
  125. new_forum = Forum.objects.filter(role="category")[:1][0]
  126. self.forum.move_content(new_forum)
  127. self.forum.synchronize()
  128. new_forum.synchronize()
  129. self.assertEqual(self.forum.threads, 0)
  130. self.assertEqual(self.forum.posts, 0)
  131. self.assertForumIsEmpty()
  132. self.assertEqual(new_forum.threads, 10)
  133. self.assertEqual(new_forum.posts, 10)
  134. def test_set_last_thread(self):
  135. """set_last_thread changes forum's last thread"""
  136. self.forum.synchronize()
  137. new_thread = self.create_thread()
  138. self.forum.set_last_thread(new_thread)
  139. self.assertEqual(self.forum.last_post_on, new_thread.last_post_on)
  140. self.assertEqual(self.forum.last_thread, new_thread)
  141. self.assertEqual(self.forum.last_thread_title, new_thread.title)
  142. self.assertEqual(self.forum.last_thread_slug, new_thread.slug)
  143. self.assertEqual(self.forum.last_poster, new_thread.last_poster)
  144. self.assertEqual(self.forum.last_poster_name,
  145. new_thread.last_poster_name)
  146. self.assertEqual(self.forum.last_poster_slug,
  147. new_thread.last_poster_slug)
  148. def test_empty_last_thread(self):
  149. """empty_last_thread empties last forum thread"""
  150. self.create_thread()
  151. self.forum.synchronize()
  152. self.forum.empty_last_thread()
  153. self.assertForumIsEmpty()