Beispiel #1
0
def test_prevent_duplicate_notifications(db, user_list, topic):
    """Test that notifications are cleaned up for edits.

    Flow:
        1. A comment is created by user A that mentions user B. Notifications are
           generated, and yield A mentioning B.
        2. The comment is edited to mention C and not B.
        3. The comment is edited to mention B and C.
        4. The comment is deleted.
    """
    # 1
    comment = Comment(topic, user_list[0], f"@{user_list[1].username}")
    db.add(comment)
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 1
    assert mentions[0].user == user_list[1]
    db.add_all(mentions)
    db.commit()

    # 2
    comment.markdown = f"@{user_list[2].username}"
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 1
    to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
        db, comment, mentions)
    assert len(to_delete) == 1
    assert mentions == to_add
    assert to_delete[0].user.username == user_list[1].username

    # 3
    comment.markdown = f"@{user_list[1].username} @{user_list[2].username}"
    db.commit()
    mentions = CommentNotification.get_mentions_for_comment(db, comment)
    assert len(mentions) == 2
    to_delete, to_add = CommentNotification.prevent_duplicate_notifications(
        db, comment, mentions)
    assert not to_delete
    assert len(to_add) == 1

    # 4
    comment.is_deleted = True
    db.commit()
    notifications = (db.query(CommentNotification.user_id).filter(
        and_(
            CommentNotification.comment_id == comment.comment_id,
            CommentNotification.notification_type ==
            CommentNotificationType.USER_MENTION,
        )).all())
    assert not notifications
Beispiel #2
0
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
Beispiel #3
0
def test_comment_tree(db, topic, session_user):
    """Ensure that building and pruning a comment tree works."""
    all_comments = []

    sort = CommentTreeSortOption.POSTED

    # add two root comments
    root = Comment(topic, session_user, "root")
    root2 = Comment(topic, session_user, "root2")
    all_comments.extend([root, root2])
    db.add_all(all_comments)
    db.commit()

    # check that both show up in the tree as top-level comments
    tree = CommentTree(all_comments, sort)
    assert list(tree) == [root, root2]

    # delete the second root comment and check that the tree now excludes it
    root2.is_deleted = True
    db.commit()
    tree = list(CommentTree(all_comments, sort))
    assert tree == [root]

    # add two replies to the remaining root comment
    child = Comment(topic, session_user, "1", parent_comment=root)
    child2 = Comment(topic, session_user, "2", parent_comment=root)
    all_comments.extend([child, child2])
    db.add_all(all_comments)
    db.commit()

    # check that the tree is built as expected so far (one root, two replies)
    tree = list(CommentTree(all_comments, sort))
    assert tree == [root]
    assert root.replies == [child, child2]
    assert child.replies == []
    assert child2.replies == []

    # add two more replies to the second depth-1 comment
    subchild = Comment(topic, session_user, "2a", parent_comment=child2)
    subchild2 = Comment(topic, session_user, "2b", parent_comment=child2)
    all_comments.extend([subchild, subchild2])
    db.add_all(all_comments)
    db.commit()

    # check the tree again
    tree = list(CommentTree(all_comments, sort))
    assert tree == [root]
    assert root.replies == [child, child2]
    assert child.replies == []
    assert child2.replies == [subchild, subchild2]

    # check depth values are as expected
    assert root.depth == 0
    assert child.depth == 1
    assert subchild.depth == 2

    # delete child2 (which has replies) and ensure it stays in the tree
    child2.is_deleted = True
    db.commit()
    tree = list(CommentTree(all_comments, sort))
    assert root.replies == [child, child2]

    # delete child2's children and ensure that whole branch is pruned
    subchild.is_deleted = True
    subchild2.is_deleted = True
    db.commit()
    tree = list(CommentTree(all_comments, sort))
    assert root.replies == [child]

    # delete root and remaining child and ensure tree is empty
    child.is_deleted = True
    root.is_deleted = True
    db.commit()
    tree = list(CommentTree(all_comments, sort))
    assert not tree