def test_create_comment_with_invalid_user_returns_exception():
    # Arrange
    user_id = 2
    post_id = 1
    comment_content = "My first post"

    # Act
    with pytest.raises(InvalidUserException):
        assert create_comment(user_id=user_id,
                              post_id=post_id,
                              comment_content=comment_content)  # Assert
def test_create_comment_with_empty_content_returns_exception(user, post):
    # Arrange
    user_id = user.id
    post_id = post.id
    comment_content = ""

    # Act
    with pytest.raises(InvalidCommentContent):
        assert create_comment(user_id=user_id,
                              post_id=post_id,
                              comment_content=comment_content)  # Assert
def test_create_comment_returns_comment_id(user, user2, post):
    # Arrange
    user_id = user2.id
    post_id = post.id
    comment_content = "Hi mahesh"

    # Act
    comment_id = create_comment(user_id=user_id,
                                post_id=post_id,
                                comment_content=comment_content)

    # Assertf
    comment = Comment.objects.get(id=comment_id)
    assert comment.post == post
    assert comment.commented_by == user2
    assert comment.content == comment_content
Пример #4
0
def get_create_comment_view(request, post_id):
    request_serializer = CreateCommentRequestSerializer(data=request.data)
    is_valid_data = request_serializer.is_valid()
    if is_valid_data:
        request_object = request_serializer.save()
        try:
            comment_id = create_comment(user_id=request_object.user_id,
                                        post_id=post_id,
                                        comment_content=request_object.content)
        except InvalidUserException:
            return Response(status=404)
        except InvalidPostException:
            return Response(status=404)
        except InvalidCommentContent:
            return Response(status=400)

        response_object = CreateCommentResponseclass(comment_id=comment_id)
        response_serializer = CreateCommentResponseSerializer(response_object)
        return Response(response_serializer.data, status=200)
Пример #5
0
def api_wrapper(*args, **kwargs):

    user = kwargs['user']
    request_data = kwargs['request_data']
    post_id = request_data['post_id']
    comment_content = request_data['content']

    try:
        comment_id = create_comment(user_id=user.id,
                                    post_id=post_id,
                                    comment_content=comment_content)
    except InvalidPostException:
        raise BadRequest(*INVALID_POST)
    except InvalidCommentContent:
        raise BadRequest(*INVALID_COMMENT_CONTENT)

    json_data = json.dumps({"comment_id": comment_id})
    response = HttpResponse(json_data, status=201)

    return response