Browse Source

Update the last post when merging a topic with another topic

sh4nks 11 years ago
parent
commit
f892599f0b
2 changed files with 20 additions and 7 deletions
  1. 5 1
      flaskbb/forum/models.py
  2. 15 6
      tests/unit/test_forum_models.py

+ 5 - 1
flaskbb/forum/models.py

@@ -344,7 +344,7 @@ class Topic(db.Model):
         return True
 
     def merge(self, topic):
-        """Merges a topic with another topic together
+        """Merges a topic with another topic
 
         :param topic: The new topic for the posts in this topic
         """
@@ -357,6 +357,10 @@ class Topic(db.Model):
         Post.query.filter_by(topic_id=self.id).\
             update({Post.topic_id: topic.id})
 
+        # Update the last post
+        if topic.last_post.date_created < self.last_post.date_created:
+            topic.last_post_id = self.last_post_id
+
         # Increase the post and views count
         topic.post_count += self.post_count
         topic.views += self.views

+ 15 - 6
tests/unit/test_forum_models.py

@@ -150,14 +150,23 @@ def test_topic_delete(topic_normal):
     assert forum.last_post_id is None
 
 
-def test_topic_merge(topic_normal, topic_moderator):
-    assert topic_normal.merge(topic_moderator)
+def test_topic_merge(topic_normal):
+    topic_other = Topic(title="Test Topic Merge")
+    post = Post(content="Test Content Merge")
+    topic_other.save(post=post, user=topic_normal.user, forum=topic_normal.forum)
 
-    # TODO: Test last post
+    # Save the last_post_id in another variable because topic_other will be
+    # overwritten later
+    last_post_other = topic_other.last_post_id
 
-    topic_normal = Topic.query.filter_by(id=topic_normal.id).first()
-    assert topic_normal is None
-    assert topic_moderator.post_count == 2
+    assert topic_other.merge(topic_normal)
+
+    # I just want to be sure that the topic is deleted
+    topic_other = Topic.query.filter_by(id=topic_other.id).first()
+    assert topic_other is None
+
+    assert topic_normal.post_count == 2
+    assert topic_normal.last_post_id == last_post_other
 
 
 def test_topic_merge_other_forum(topic_normal):