test_forum_models.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. from flaskbb.forum.models import Category, Forum, Topic, Post
  2. from flaskbb.user.models import User
  3. def test_category_save(database):
  4. category = Category(title="Test Category")
  5. category.save()
  6. assert category.title == "Test Category"
  7. def test_category_delete(category):
  8. category.delete()
  9. category = Category.query.filter_by(id=category.id).first()
  10. assert category is None
  11. def test_category_delete_with_user(topic_normal):
  12. user = topic_normal.user
  13. forum = topic_normal.forum
  14. category = topic_normal.forum.category
  15. assert user.post_count == 1
  16. assert forum.post_count == 1
  17. assert forum.topic_count == 1
  18. category.delete([user])
  19. assert user.post_count == 0
  20. category = Category.query.filter_by(id=category.id).first()
  21. topic = Topic.query.filter_by(id=topic_normal.id).first()
  22. assert category is None
  23. # The topic should also be deleted
  24. assert topic is None
  25. def test_category_delete_with_forum(forum):
  26. forum.category.delete()
  27. assert forum is not None
  28. assert forum.category is not None
  29. category = Category.query.filter_by(id=forum.category.id).first()
  30. forum = Forum.query.filter_by(id=forum.id).first()
  31. assert forum is None
  32. assert category is None
  33. def test_forum_save(category, moderator_user):
  34. forum = Forum(title="Test Forum", category_id=category.id)
  35. forum.save()
  36. assert forum.title == "Test Forum"
  37. # Test with adding a moderator
  38. forum.save([moderator_user])
  39. assert forum.moderators == [moderator_user]
  40. def test_forum_delete(forum):
  41. forum.delete()
  42. forum = Forum.query.filter_by(id=forum.id).first()
  43. assert forum is None
  44. def test_forum_delete_with_user(topic_normal, normal_user):
  45. assert normal_user.post_count == 1
  46. topic_normal.forum.delete([normal_user])
  47. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  48. assert forum is None
  49. assert normal_user.post_count == 0
  50. def test_forum_update_last_post(topic_normal, normal_user):
  51. post = Post(content="Test Content 2")
  52. post.save(topic=topic_normal, user=normal_user)
  53. assert topic_normal.forum.last_post == post
  54. post.delete()
  55. topic_normal.forum.update_last_post()
  56. assert topic_normal.forum.last_post == topic_normal.first_post
  57. def test_forum_url(forum):
  58. assert forum.url == "http://localhost:5000/forum/1-test-forum"
  59. def test_forum_slugify(forum):
  60. assert forum.slug == "test-forum"
  61. def test_topic_save(forum, normal_user):
  62. post = Post(content="Test Content")
  63. topic = Topic(title="Test Title")
  64. assert forum.last_post_id is None
  65. assert forum.post_count == 0
  66. assert forum.topic_count == 0
  67. topic.save(forum=forum, post=post, user=normal_user)
  68. assert topic.title == "Test Title"
  69. topic.title = "Test Edit Title"
  70. topic.save()
  71. assert topic.title == "Test Edit Title"
  72. # The first post in the topic is also the last post
  73. assert topic.first_post_id == post.id
  74. assert topic.last_post_id == post.id
  75. assert forum.last_post_id == post.id
  76. assert forum.post_count == 1
  77. assert forum.topic_count == 1
  78. def test_topic_delete(topic_normal):
  79. assert topic_normal.user.post_count == 1
  80. assert topic_normal.post_count == 1
  81. assert topic_normal.forum.topic_count == 1
  82. assert topic_normal.forum.post_count == 1
  83. topic_normal.delete(users=[topic_normal.user])
  84. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  85. user = User.query.filter_by(id=topic_normal.user_id).first()
  86. topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
  87. assert topic_normal is None
  88. assert user.post_count == 0
  89. assert forum.topic_count == 0
  90. assert forum.post_count == 0
  91. assert forum.last_post_id is None
  92. def test_topic_merge(topic_normal):
  93. topic_other = Topic(title="Test Topic Merge")
  94. post = Post(content="Test Content Merge")
  95. topic_other.save(post=post, user=topic_normal.user, forum=topic_normal.forum)
  96. # Save the last_post_id in another variable because topic_other will be
  97. # overwritten later
  98. last_post_other = topic_other.last_post_id
  99. assert topic_other.merge(topic_normal)
  100. # I just want to be sure that the topic is deleted
  101. topic_other = Topic.query.filter_by(id=topic_other.id).first()
  102. assert topic_other is None
  103. assert topic_normal.post_count == 2
  104. assert topic_normal.last_post_id == last_post_other
  105. def test_topic_merge_other_forum(topic_normal):
  106. """You cannot merge a topic with a topic from another forum"""
  107. forum_other = Forum(title="Test Forum 2", category_id=1)
  108. forum_other.save()
  109. topic_other = Topic(title="Test Topic 2")
  110. post_other = Post(content="Test Content 2")
  111. topic_other.save(user=topic_normal.user, forum=forum_other, post=post_other)
  112. assert not topic_normal.merge(topic_other)
  113. def test_topic_move(topic_normal):
  114. forum_other = Forum(title="Test Forum 2", category_id=1)
  115. forum_other.save()
  116. forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first()
  117. assert topic_normal.move(forum_other)
  118. assert forum_old.topics == []
  119. assert forum_old.last_post_id == 0
  120. assert forum_old.topic_count == 0
  121. assert forum_old.post_count == 0
  122. assert forum_other.last_post_id == topic_normal.last_post_id
  123. assert forum_other.topic_count == 1
  124. assert forum_other.post_count == 1
  125. def test_topic_move_same_forum(topic_normal):
  126. assert not topic_normal.move(topic_normal.forum)
  127. def test_topic_update_read():
  128. # TODO: Refactor it, to make it easier to test it
  129. pass
  130. def test_topic_url(topic_normal):
  131. assert topic_normal.url == "http://localhost:5000/topic/1-test-topic-normal"
  132. def test_topic_slug(topic_normal):
  133. assert topic_normal.slug == "test-topic-normal"
  134. def test_post_save(topic_normal, normal_user):
  135. post = Post(content="Test Content")
  136. post.save(topic=topic_normal, user=normal_user)
  137. assert post.content == "Test Content"
  138. post.content = "Test Edit Content"
  139. post.save()
  140. assert post.content == "Test Edit Content"
  141. assert topic_normal.user.post_count == 2
  142. assert topic_normal.post_count == 2
  143. assert topic_normal.last_post == post
  144. assert topic_normal.forum.post_count == 2
  145. def test_post_delete(topic_normal):
  146. post_middle = Post(content="Test Content Middle")
  147. post_middle.save(topic=topic_normal, user=topic_normal.user)
  148. post_last = Post(content="Test Content Last")
  149. post_last.save(topic=topic_normal, user=topic_normal.user)
  150. assert topic_normal.post_count == 3
  151. assert topic_normal.forum.post_count == 3
  152. assert topic_normal.user.post_count == 3
  153. post_middle.delete()
  154. # Check the last posts
  155. assert topic_normal.last_post == post_last
  156. assert topic_normal.forum.last_post == post_last
  157. post_last.delete()
  158. # That was a bit trickier..
  159. assert topic_normal.post_count == 1
  160. assert topic_normal.forum.post_count == 1
  161. assert topic_normal.user.post_count == 1
  162. assert topic_normal.first_post_id == topic_normal.last_post_id
  163. assert topic_normal.forum.last_post_id == topic_normal.last_post_id