test_forum_models.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, topic_moderator):
  93. assert topic_normal.merge(topic_moderator)
  94. # TODO: Test last post
  95. topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
  96. assert topic_normal is None
  97. assert topic_moderator.post_count == 2
  98. def test_topic_merge_other_forum(topic_normal):
  99. """You cannot merge a topic with a topic from another forum"""
  100. forum_other = Forum(title="Test Forum 2", category_id=1)
  101. forum_other.save()
  102. topic_other = Topic(title="Test Topic 2")
  103. post_other = Post(content="Test Content 2")
  104. topic_other.save(user=topic_normal.user, forum=forum_other, post=post_other)
  105. assert not topic_normal.merge(topic_other)
  106. def test_topic_move(topic_normal):
  107. forum_other = Forum(title="Test Forum 2", category_id=1)
  108. forum_other.save()
  109. forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first()
  110. assert topic_normal.move(forum_other)
  111. assert forum_old.topics == []
  112. assert forum_old.last_post_id == 0
  113. assert forum_old.topic_count == 0
  114. assert forum_old.post_count == 0
  115. assert forum_other.last_post_id == topic_normal.last_post_id
  116. assert forum_other.topic_count == 1
  117. assert forum_other.post_count == 1
  118. def test_topic_move_same_forum(topic_normal):
  119. assert not topic_normal.move(topic_normal.forum)
  120. def test_topic_update_read():
  121. # TODO: Refactor it, to make it easier to test it
  122. pass
  123. def test_topic_url(topic_normal):
  124. assert topic_normal.url == "http://localhost:5000/topic/1-test-topic-normal"
  125. def test_topic_slug(topic_normal):
  126. assert topic_normal.slug == "test-topic-normal"
  127. def test_post_save(topic_normal, normal_user):
  128. post = Post(content="Test Content")
  129. post.save(topic=topic_normal, user=normal_user)
  130. assert post.content == "Test Content"
  131. post.content = "Test Edit Content"
  132. post.save()
  133. assert post.content == "Test Edit Content"
  134. assert topic_normal.user.post_count == 2
  135. assert topic_normal.post_count == 2
  136. assert topic_normal.last_post == post
  137. assert topic_normal.forum.post_count == 2
  138. def test_post_delete(topic_normal):
  139. post_middle = Post(content="Test Content Middle")
  140. post_middle.save(topic=topic_normal, user=topic_normal.user)
  141. post_last = Post(content="Test Content Last")
  142. post_last.save(topic=topic_normal, user=topic_normal.user)
  143. assert topic_normal.post_count == 3
  144. assert topic_normal.forum.post_count == 3
  145. assert topic_normal.user.post_count == 3
  146. post_middle.delete()
  147. # Check the last posts
  148. assert topic_normal.last_post == post_last
  149. assert topic_normal.forum.last_post == post_last
  150. post_last.delete()
  151. # That was a bit trickier..
  152. assert topic_normal.post_count == 1
  153. assert topic_normal.forum.post_count == 1
  154. assert topic_normal.user.post_count == 1
  155. assert topic_normal.first_post_id == topic_normal.last_post_id
  156. assert topic_normal.forum.last_post_id == topic_normal.last_post_id