def test_given_invalid_post_id_raises_exception(self): post_id = 1 comment_content = "usha" user_id = 1 storage = create_autospec(StorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(storage, presenter) storage.validate_post_id.side_effect = InvalidPostId presenter.raise_exception_for_invalid_post.side_effect = NotFound with pytest.raises(NotFound): interactor.create_comment(user_id=user_id, post_id=post_id, comment_content=comment_content) storage.validate_post_id.assert_called_once_with(post_id=post_id) presenter.raise_exception_for_invalid_post.assert_called_once()
def test_create_comment_with_invalid_post_id_raise_exception(): # Arrange invalid_post_id = -1 user_id = 1 comment_content ='New comment' post_storage = create_autospec(PostStorageInterface) comment_storage = create_autospec(CommentStorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(post_storage=post_storage, comment_storage=comment_storage, presenter=presenter) post_storage.is_valid_post_id.return_value = False presenter.raise_invalid_post_id_exception.side_effect = NotFound # Act with pytest.raises(NotFound): interactor.create_comment(user_id=user_id, post_id=invalid_post_id, comment_content=comment_content)
def api_wrapper(*args, **kwargs): user = kwargs['user'] post_id = kwargs['postid'] request_data = kwargs['request_data'] post_storage = Storage() presenter = Presenter() interactor = CreateCommentInteractor(post_storage, presenter) response = interactor.create_comment(post_id, user.id, request_data['comment_message']) import json return json.dumps(response)
def api_wrapper(*args, **kwargs): user = kwargs['user'] request_data = kwargs["request_data"] comment_content = request_data["content"] post_id = request_data['post_id'] user_id = user.id post_storage = StorageImplementation() comment_storage = StorageImplementation() presenter = PresenterImplementation() interactor = CreateCommentInteractor(post_storage=post_storage, comment_storage=comment_storage, presenter=presenter) comment_id_dict = interactor.create_comment( user_id=user_id, post_id=post_id, comment_content=comment_content) response_data = json.dumps(comment_id_dict) return HttpResponse(response_data, status=201)
def test_create_comment(self): post_storage_mock = Mock(spec=PostStorage) presenter_mock = Mock(spec=JsonPresenter) create_comment = CreateCommentInteractor(post_storage_mock, presenter_mock) post_id = 1 commenter = 1 comment_content = "test comment" response_data = {"comment_id": 1} comment_id = 1 post_storage_mock.create_comment.return_value = comment_id presenter_mock.get_create_comment_response.return_value = response_data response = create_comment.create_comment(post_id, commenter, comment_content) post_storage_mock.create_comment.assert_called_once_with( post_id, commenter, comment_content) presenter_mock.get_create_comment_response.assert_called_once_with( comment_id) assert response == response_data
def test_create_comment_with_valid_details(): # Arrange post_id = 1 user_id = 1 comment_content ='New comment' expected_comment_id = 1 expected_comment_id_response = { "comment_id": expected_comment_id } post_storage = create_autospec(PostStorageInterface) comment_storage = create_autospec(CommentStorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(post_storage=post_storage, comment_storage=comment_storage, presenter=presenter) comment_storage.create_comment.return_value = expected_comment_id presenter.get_create_comment_response.return_value = \ expected_comment_id_response # Act actual_comment_id_dict = interactor.create_comment( user_id=user_id, post_id=post_id, comment_content=comment_content ) # Assert assert actual_comment_id_dict == expected_comment_id_response comment_storage.create_comment.assert_called_once_with( user_id=user_id, post_id=post_id, comment_content=comment_content ) presenter.get_create_comment_response.assert_called_once_with( comment_id=expected_comment_id )
def test_given_valid_post_id_creates_comment_and_returns_comment_id_dict( self): post_id = 1 comment_content = "usha" user_id = 1 expected_comment_id = 1 expected_comment_id_dict = {"comment_id": expected_comment_id} storage = create_autospec(StorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(storage, presenter) storage.create_comment.return_value = \ expected_comment_id presenter.get_create_comment_response.return_value = \ expected_comment_id_dict actual_comment_id_dict = interactor.create_comment( user_id=user_id, post_id=post_id, comment_content=comment_content) assert expected_comment_id_dict == actual_comment_id_dict storage.validate_post_id.assert_called_once_with(post_id=post_id) storage.create_comment.assert_called_once_with( user_id=user_id, post_id=post_id, comment_content=comment_content) presenter.get_create_comment_response.assert_called_once_with( comment_id=expected_comment_id)