def test_reply_to_reply(self): post_storage_mock = Mock(spec=PostStorage) presenter_mock = Mock(spec=JsonPresenter) create_comment = CreateCommentInteractor(post_storage_mock, presenter_mock) reply_id = 2 commenter = 1 comment_content = "test comment" response_data = {"comment_id": 3} comment_id = 1 post_storage_mock.is_comment_or_reply.return_value = False post_storage_mock.get_comment_id.return_value = comment_id post_storage_mock.create_reply.return_value = comment_id presenter_mock.get_create_reply_response.return_value = response_data response = create_comment.create_reply(reply_id, commenter, comment_content) post_storage_mock.get_comment_id.assert_called_once_with(reply_id) post_storage_mock.create_reply.assert_called_once_with( comment_id, commenter, comment_content) presenter_mock.get_create_reply_response.assert_called_once_with( comment_id) assert response == response_data
def test_create_comment_interactor_given_valid_details(): #arrange user_id = 1 post_id = 1 comment_content = "HELLO" new_comment_id = 1 expected_response = {"comment_id": new_comment_id} validator = create_autospec(ValidatorsStorageInterface) storage = create_autospec(CommentStorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(validator=validator, storage=storage, presenter=presenter) validator.is_invalid_post_id.return_value = False storage.create_comment.return_value = new_comment_id presenter.get_create_comment_response.return_value = \ expected_response #act response = interactor.create_comment_return_comment_id( user_id=user_id, post_id=post_id, comment_content=comment_content) #assert 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=new_comment_id) assert response == expected_response
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 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 api_wrapper(*args, **kwargs): user = kwargs["user"] post_id = kwargs["post_id"] request_data = kwargs["request_data"] comment_content = request_data["comment_content"] presenter = PresenterImplementation() storage = CommentStorageImplementation() validator = ValidatorsStorageImplementation() interactor = CreateCommentInteractor(validator, storage, presenter) comment_id_dict = interactor.create_comment_return_comment_id( user_id=user.id, post_id=post_id, comment_content=comment_content) response_data = json.dumps(comment_id_dict) response = HttpResponse(response_data, status=201) return response
def api_wrapper(*args, **kwargs): user = kwargs['user'] comment_id = kwargs['commentid'] request_data = kwargs['request_data'] post_storage = Storage() presenter = Presenter() interactor = CreateCommentInteractor(post_storage, presenter) response = interactor.create_reply(comment_id, user.id, request_data['comment_message']) from django.http.response import HttpResponse import json return HttpResponse(json.dumps(response), status=201)
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'] 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_create_comment_interactor_given_invalid_details_raise_exception(): #arrange user_id = 1 post_id = 1 comment_content = "HELLO" validator = create_autospec(ValidatorsStorageInterface) storage = create_autospec(CommentStorageInterface) presenter = create_autospec(PresenterInterface) interactor = CreateCommentInteractor(validator=validator, storage=storage, presenter=presenter) validator.is_invalid_post_id.return_value = True presenter.raise_invalid_post_id_exception.side_effect = NotFound #act with pytest.raises(NotFound): interactor.create_comment_return_comment_id( post_id=post_id, comment_content=comment_content, user_id=user_id, ) validator.is_invalid_post_id.assert_called_once_with(post_id) presenter.raise_invalid_post_id_exception.assert_called_once()
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)