def test_set_comment_to_deleted(mocker, reddit_comment_obj):
    """
    Test that set_comment_to_deleted calls the indexing task to update a comment's deleted property
    and calls another task to decrement the parent post's comment count
    """
    patched_partial_update_task = mocker.patch(
        "search.task_helpers.update_document_with_partial"
    )
    patched_increment_task = mocker.patch(
        "search.task_helpers.increment_document_integer_field"
    )
    set_comment_to_deleted(reddit_comment_obj)
    assert patched_partial_update_task.delay.called is True
    assert patched_partial_update_task.delay.call_args[0] == (
        gen_comment_id(reddit_comment_obj.id),
        {"deleted": True},
        COMMENT_TYPE,
    )
    assert patched_increment_task.delay.called is True
    assert patched_increment_task.delay.call_args[0] == (
        gen_post_id(reddit_comment_obj.submission.id),
    )
    assert patched_increment_task.delay.call_args[1] == {
        "field_name": "num_comments",
        "incr_amount": -1,
        "object_type": POST_TYPE,
    }
Beispiel #2
0
def update_indexed_score(instance, instance_type, vote_action=None):
    """
    Runs a task to update the score for a post/comment document in ES.

    Args:
        instance: A PRAW Comment or Submission (a.k.a. post) object
        instance_type (str): A string indicating the type of object being passed in
        vote_action (VoteActions): A value indicating what kind of vote action was taken
    """
    if vote_action in (VoteActions.UPVOTE, VoteActions.CLEAR_DOWNVOTE):
        vote_increment = 1
    elif vote_action in (VoteActions.DOWNVOTE, VoteActions.CLEAR_UPVOTE):
        vote_increment = -1
    else:
        raise ValueError("Received an invalid vote action value: %s" %
                         vote_action)

    if instance_type not in [POST_TYPE, COMMENT_TYPE]:
        return
    content_id = (gen_post_id(instance.id) if instance_type == POST_TYPE else
                  gen_comment_id(instance.id))

    increment_document_integer_field.delay(
        content_id,
        field_name="score",
        incr_amount=vote_increment,
        object_type=instance_type,
    )
def test_index_new_comment(mocker):
    """
    Test that index_new_comment calls indexing tasks with the right parameters
    """
    fake_serialized_data = {"serialized": "comment"}
    comment = CommentFactory.create()
    mock_submission = mocker.Mock(id="123")
    mock_comment = mocker.Mock(id=comment.comment_id, submission=mock_submission)
    patched_serialize_func = mocker.patch(
        "search.task_helpers.ESCommentSerializer.to_representation",
        return_value=fake_serialized_data,
    )
    patched_create_task = mocker.patch("search.task_helpers.create_document")
    patched_increment_task = mocker.patch(
        "search.task_helpers.increment_document_integer_field"
    )
    index_new_comment(mock_comment)
    patched_serialize_func.assert_called_once_with(comment)
    assert patched_create_task.delay.called is True
    assert patched_create_task.delay.call_args[0] == (
        gen_comment_id(mock_comment.id),
        fake_serialized_data,
    )
    assert patched_increment_task.delay.called is True
    assert patched_increment_task.delay.call_args[0] == (
        gen_post_id(mock_submission.id),
    )
    assert patched_increment_task.delay.call_args[1] == {
        "field_name": "num_comments",
        "incr_amount": 1,
        "object_type": POST_TYPE,
    }
Beispiel #4
0
def set_comment_to_deleted(comment_obj):
    """
    Sets a comment to deleted and updates the parent post's comment count.

    Args:
        comment_obj (praw.models.reddit.comment.Comment): A PRAW comment object
    """
    update_document_with_partial.delay(gen_comment_id(comment_obj.id),
                                       {"deleted": True}, COMMENT_TYPE)
    decrement_parent_post_comment_count(comment_obj)
Beispiel #5
0
def update_comment_text(comment_obj):
    """
    Serializes comment object text and runs a task to update the text for the associated ES document.

    Args:
        comment_obj (praw.models.reddit.comment.Comment): A PRAW comment object
    """
    update_document_with_partial.delay(gen_comment_id(comment_obj.id),
                                       {"text": comment_obj.body},
                                       COMMENT_TYPE)
Beispiel #6
0
def index_new_comment(comment_obj):
    """
    Serializes a comment object and runs a task to create an ES document for it.

    Args:
        comment_obj (praw.models.reddit.comment.Comment): A PRAW comment object
    """
    comment = Comment.objects.get(comment_id=comment_obj.id)
    data = ESCommentSerializer(instance=comment).data
    create_document.delay(gen_comment_id(comment_obj.id), data)
    increment_parent_post_comment_count(comment_obj)
def test_update_comment_text(mocker, reddit_comment_obj):
    """
    Test that update_post_text calls the indexing task with the right parameters
    """
    patched_task = mocker.patch("search.task_helpers.update_document_with_partial")
    update_comment_text(reddit_comment_obj)
    assert patched_task.delay.called is True
    assert patched_task.delay.call_args[0] == (
        gen_comment_id(reddit_comment_obj.id),
        {"text": reddit_comment_obj.body},
        COMMENT_TYPE,
    )
Beispiel #8
0
def update_comment_removal_status(comment_obj):
    """
    Serializes the removal status for a comment object and runs a task to update that status
    for the associated ES document.

    Args:
        comment_obj (praw.models.reddit.comment.Comment): A PRAW comment object
    """
    update_document_with_partial.delay(
        gen_comment_id(comment_obj.id),
        {"removed": is_reddit_object_removed(comment_obj)},
        COMMENT_TYPE,
    )
def test_update_comment_removal_status(
    mocker, reddit_comment_obj, removal_status, expected_removed_arg
):
    """
    Test that update_comment_removal_status calls the indexing task with the right parameters
    """
    patched_task = mocker.patch("search.task_helpers.update_document_with_partial")
    patched_reddit_object_removed = mocker.patch(
        "search.task_helpers.is_reddit_object_removed"
    )
    patched_reddit_object_removed.return_value = removal_status
    update_comment_removal_status(reddit_comment_obj)
    assert patched_task.delay.called is True
    assert patched_task.delay.call_args[0] == (
        gen_comment_id(reddit_comment_obj.id),
        {"removed": expected_removed_arg},
        COMMENT_TYPE,
    )
def serialize_comment_for_bulk(comment_obj):
    """
    Serialize a comment for a bulk API request

    Args:
        comment_obj (channels.models.Comment): A Comment object

    Returns:
        dict: the serialized comment
    """
    try:
        return {
            "_id": gen_comment_id(comment_obj.comment_id),
            **ESCommentSerializer(instance=comment_obj).data,
        }
    except NotFound:
        log.exception("Reddit comment not found: %s", comment_obj.id)
        raise