sh4nks 11 лет назад
Родитель
Сommit
145f14f794
2 измененных файлов с 104 добавлено и 1 удалено
  1. 9 1
      flaskbb/forum/models.py
  2. 95 0
      tests/unit/test_forum_models.py

+ 9 - 1
flaskbb/forum/models.py

@@ -13,7 +13,6 @@ from datetime import datetime, timedelta
 from flask import current_app, url_for, abort
 
 from flaskbb.extensions import db
-from flaskbb.utils.query import TopicQuery
 from flaskbb.utils.helpers import slugify, get_categories_and_forums, get_forums
 
 
@@ -51,6 +50,9 @@ class TopicsRead(db.Model):
                          primary_key=True)
     last_read = db.Column(db.DateTime, default=datetime.utcnow())
 
+    def __repr__(self):
+        return "<{} {}>".format(self.__class__.__name__, self.id)
+
     def save(self):
         """Saves a TopicsRead entry."""
         db.session.add(self)
@@ -76,6 +78,9 @@ class ForumsRead(db.Model):
     last_read = db.Column(db.DateTime, default=datetime.utcnow())
     cleared = db.Column(db.DateTime)
 
+    def __repr__(self):
+        return "<{} {}>".format(self.__class__.__name__, self.id)
+
     def save(self):
         """Saves a ForumsRead entry."""
         db.session.add(self)
@@ -106,6 +111,9 @@ class Report(db.Model):
                                foreign_keys=[reporter_id])
     zapper = db.relationship("User", lazy="joined", foreign_keys=[zapped_by])
 
+    def __repr__(self):
+        return "<{} {}>".format(self.__class__.__name__, self.id)
+
     def save(self, post=None, user=None):
         """Saves a report.
 

+ 95 - 0
tests/unit/test_forum_models.py

@@ -61,6 +61,64 @@ def test_category_delete_with_forum(forum):
     assert category is None
 
 
+def test_category_get_forums(forum, user):
+    category = forum.category
+
+    with current_app.test_request_context():
+        # Test with logged in user
+        login_user(user)
+        assert current_user.is_authenticated()
+        cat, forums = Category.get_forums(category.id, current_user)
+
+        # Check if it is a list because in a category there are normally more
+        # than one forum in it (not in these tests)
+        assert isinstance(forums, list) is True
+
+        assert forums == [(forum, None)]
+        assert cat == category
+
+        # Test the same thing with a logged out user
+        logout_user()
+        assert not current_user.is_authenticated()
+        cat, forums = Category.get_forums(category.id, current_user)
+
+        # Check if it is a list because in a category there are normally more
+        # than one forum in it (not in these tests)
+        assert isinstance(forums, list) is True
+
+        assert forums == [(forum, None)]
+        assert cat == category
+
+
+def test_category_get_all(forum, user):
+    category = forum.category
+
+    with current_app.test_request_context():
+        # Test with logged in user
+        login_user(user)
+        assert current_user.is_authenticated()
+        categories = Category.get_all(current_user)
+
+        # All categories are stored in a list
+        assert isinstance(categories, list)
+        # The forums for a category are also stored in a list
+        assert isinstance(categories[0][1], list)
+
+        assert categories == [(category, [(forum, None)])]
+
+        # Test with logged out user
+        logout_user()
+        assert not current_user.is_authenticated()
+        categories = Category.get_all(current_user)
+
+        # All categories are stored in a list
+        assert isinstance(categories, list)
+        # The forums for a category are also stored in a list
+        assert isinstance(categories[0][1], list)
+
+        assert categories == [(category, [(forum, None)])]
+
+
 def test_forum_save(category, moderator_user):
     """Test the save forum method"""
     forum = Forum(title="Test Forum", category_id=category.id)
@@ -196,6 +254,43 @@ def test_forum_slugify(forum):
     assert forum.slug == "test-forum"
 
 
+def test_forum_get_forum(forum, user):
+    with current_app.test_request_context():
+        # Test with logged in user
+        login_user(user)
+
+        forum_with_forumsread = \
+            Forum.get_forum(forum_id=forum.id, user=current_user)
+
+        assert forum_with_forumsread == (forum, None)
+
+        # Test with logged out user
+        logout_user()
+
+        forum_with_forumsread = \
+            Forum.get_forum(forum_id=forum.id, user=current_user)
+
+        assert forum_with_forumsread == (forum, None)
+
+
+def test_forum_get_topics(topic, user):
+    forum = topic.forum
+    with current_app.test_request_context():
+        # Test with logged in user
+        login_user(user)
+
+        topics = Forum.get_topics(forum_id=forum.id, user=current_user)
+
+        assert topics.items == [(topic, None)]
+
+        # Test with logged out user
+        logout_user()
+
+        topics = Forum.get_topics(forum_id=forum.id, user=current_user)
+
+        assert topics.items == [(topic, None)]
+
+
 def test_topic_save(forum, user):
     """Test the save topic method with creating and editing a topic."""
     post = Post(content="Test Content")