test_forum_models.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # TODO: Test user post counts
  9. category.delete()
  10. category = Category.query.filter_by(id=category.id).first()
  11. assert category is None
  12. def test_category_delete_with_forum(forum):
  13. forum.category.delete()
  14. assert forum is not None
  15. assert forum.category is not None
  16. category = Category.query.filter_by(id=forum.category.id).first()
  17. forum = Forum.query.filter_by(id=forum.id).first()
  18. assert forum is None
  19. assert category is None
  20. def test_forum_save(category, moderator_user):
  21. forum = Forum(title="Test Forum", category_id=category.id)
  22. forum.save()
  23. assert forum.title == "Test Forum"
  24. # Test with adding a moderator
  25. forum.save([moderator_user])
  26. for moderator in forum.moderators:
  27. assert moderator == moderator_user
  28. def test_forum_delete(forum):
  29. # TODO: Test user post counts
  30. forum.delete()
  31. forum = Forum.query.filter_by(id=forum.id).first()
  32. assert forum is None
  33. def test_forum_delete_with_user(topic_normal, normal_user):
  34. assert normal_user.post_count == 1
  35. topic_normal.forum.delete([normal_user])
  36. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  37. assert forum is None
  38. assert normal_user.post_count == 0
  39. def test_forum_update_last_post(topic_normal, normal_user):
  40. post = Post(content="Test Content 2")
  41. post.save(topic=topic_normal, user=normal_user)
  42. assert topic_normal.forum.last_post == post
  43. post.delete()
  44. topic_normal.forum.update_last_post()
  45. assert topic_normal.forum.last_post == topic_normal.first_post
  46. def test_forum_url(forum):
  47. assert forum.url == "http://localhost:5000/forum/1-test-forum"
  48. def test_forum_slugify(forum):
  49. assert forum.slug == "test-forum"
  50. def test_topic_save(forum, normal_user):
  51. post = Post(content="Test Content")
  52. topic = Topic(title="Test Title")
  53. assert forum.last_post_id is None
  54. assert forum.post_count == 0
  55. assert forum.topic_count == 0
  56. topic.save(forum=forum, post=post, user=normal_user)
  57. assert topic.title == "Test Title"
  58. # The first post in the topic is also the last post
  59. assert topic.first_post_id == post.id
  60. assert topic.last_post_id == post.id
  61. assert forum.last_post_id == post.id
  62. assert forum.post_count == 1
  63. assert forum.topic_count == 1
  64. def test_topic_delete(topic_normal):
  65. assert topic_normal.user.post_count == 1
  66. assert topic_normal.post_count == 1
  67. assert topic_normal.forum.topic_count == 1
  68. assert topic_normal.forum.post_count == 1
  69. topic_normal.delete(users=[topic_normal.user])
  70. forum = Forum.query.filter_by(id=topic_normal.forum_id).first()
  71. user = User.query.filter_by(id=topic_normal.user_id).first()
  72. topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
  73. assert topic_normal is None
  74. assert user.post_count == 0
  75. assert forum.topic_count == 0
  76. assert forum.post_count == 0
  77. def test_topic_merge(topic_normal, topic_moderator):
  78. assert topic_normal.merge(topic_moderator)
  79. topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
  80. assert topic_normal is None
  81. assert topic_moderator.post_count == 2
  82. def test_topic_merge_other_forum(topic_normal):
  83. """You cannot merge a topic with a topic from another forum"""
  84. forum_other = Forum(title="Test Forum 2", category_id=1)
  85. forum_other.save()
  86. topic_other = Topic(title="Test Topic 2")
  87. post_other = Post(content="Test Content 2")
  88. topic_other.save(user=topic_normal.user, forum=forum_other, post=post_other)
  89. assert not topic_normal.merge(topic_other)
  90. def test_topic_move(topic_normal):
  91. forum_other = Forum(title="Test Forum 2", category_id=1)
  92. forum_other.save()
  93. forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first()
  94. assert topic_normal.move(forum_other)
  95. assert forum_old.topics == []
  96. assert forum_old.topic_count == 0
  97. assert forum_old.post_count == 0
  98. assert forum_other.topic_count == 1
  99. assert forum_other.post_count == 1
  100. def test_topic_move_same_forum(topic_normal):
  101. assert not topic_normal.move(topic_normal.forum)