From 0481a9ea01192c0fab4f683cacb2e6736d79e606 Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 9 Aug 2018 02:09:31 -0600 Subject: [PATCH] Add some more tests for comments triggers Unfortunately some of the triggers aren't currently testable due to the fact that postgresql's NOW() and similar functions will always return the same value during the entire test transaction, but this at least tests a couple more of the behaviors. --- tildes/tests/test_comment.py | 34 ----------- tildes/tests/test_triggers_comments.py | 84 ++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 tildes/tests/test_triggers_comments.py diff --git a/tildes/tests/test_comment.py b/tildes/tests/test_comment.py index f1ffae4..f830081 100644 --- a/tildes/tests/test_comment.py +++ b/tildes/tests/test_comment.py @@ -43,40 +43,6 @@ def test_comment_edit_uses_markdown_field(mocker, comment): assert Markdown._validate.called -def test_comments_affect_topic_num_comments(session_user, topic, db): - """Ensure adding/deleting comments affects the topic's comment count.""" - assert topic.num_comments == 0 - - # Insert some comments, ensure each one increments the count - comments = [] - for num in range(0, 5): - new_comment = Comment(topic, session_user, 'comment') - comments.append(new_comment) - db.add(new_comment) - db.commit() - db.refresh(topic) - assert topic.num_comments == len(comments) - - # Delete all the comments, ensure each one decrements the count - for num, comment in enumerate(comments, start=1): - comment.is_deleted = True - db.commit() - db.refresh(topic) - assert topic.num_comments == len(comments) - num - - -def test_delete_sets_deleted_time(db, comment): - """Ensure a deleted comment gets its deleted_time set.""" - assert not comment.is_deleted - assert not comment.deleted_time - - comment.is_deleted = True - db.commit() - db.refresh(comment) - - assert comment.deleted_time - - def test_edit_markdown_updates_html(comment): """Ensure editing a comment works and the markdown and HTML update.""" comment.markdown = 'Updated comment' diff --git a/tildes/tests/test_triggers_comments.py b/tildes/tests/test_triggers_comments.py new file mode 100644 index 0000000..6741a7d --- /dev/null +++ b/tildes/tests/test_triggers_comments.py @@ -0,0 +1,84 @@ +from tildes.models.comment import Comment + + +def test_comments_affect_topic_num_comments(session_user, topic, db): + """Ensure adding/deleting comments affects the topic's comment count.""" + assert topic.num_comments == 0 + + # Insert some comments, ensure each one increments the count + comments = [] + for num in range(0, 5): + new_comment = Comment(topic, session_user, 'comment') + comments.append(new_comment) + db.add(new_comment) + db.commit() + db.refresh(topic) + assert topic.num_comments == len(comments) + + # Delete all the comments, ensure each one decrements the count + for num, comment in enumerate(comments, start=1): + comment.is_deleted = True + db.commit() + db.refresh(topic) + assert topic.num_comments == len(comments) - num + + +def test_delete_sets_deleted_time(db, comment): + """Ensure a deleted comment gets its deleted_time set and unset.""" + assert not comment.is_deleted + assert not comment.deleted_time + + comment.is_deleted = True + db.commit() + db.refresh(comment) + + assert comment.deleted_time + + comment.is_deleted = False + db.commit() + db.refresh(comment) + + assert not comment.deleted_time + + +def test_remove_sets_removed_time(db, comment): + """Ensure a removed comment gets its removed_time set and unset.""" + assert not comment.is_removed + assert not comment.removed_time + + comment.is_removed = True + db.commit() + db.refresh(comment) + + assert comment.removed_time + + comment.is_removed = False + db.commit() + db.refresh(comment) + + assert not comment.removed_time + + +def test_remove_delete_single_decrement(db, topic, session_user): + """Ensure that remove+delete doesn't double-decrement num_comments.""" + # add 2 comments + comment1 = Comment(topic, session_user, 'Comment 1') + comment2 = Comment(topic, session_user, 'Comment 2') + db.add_all([comment1, comment2]) + db.commit() + db.refresh(topic) + assert topic.num_comments == 2 + + # remove one and check the decrement + comment1.is_removed = True + db.add(comment1) + db.commit() + db.refresh(topic) + assert topic.num_comments == 1 + + # delete the same comment and check it didn't decrement again + comment1.is_deleted = True + db.add(comment1) + db.commit() + db.refresh(topic) + assert topic.num_comments == 1