def api_wrapper(*args, **kwargs): post_id = kwargs["post_id"] post_storage = PostStorageImpl() json_presenter = JsonPresenterImpl() interactor = DeletePostInteractor(post_storage, json_presenter) response = interactor.delete_post(post_id) return HttpResponse(json.dumps(response), status=201)
def api_wrapper(*args, **kwargs): post_id = kwargs['postid'] post_storage = Storage() presenter = Presenter() interactor = DeletePostInteractor(post_storage, presenter) response = interactor.delete_post(post_id) import json return json.dumps(response)
def test_post_not_exists(self): post_storage_mock = Mock(spec=PostStorage) presenter_mock = Mock(spec=JsonPresenter) delete_post = DeletePostInteractor(post_storage_mock, presenter_mock) post_id = 2 post_storage_mock.post_exists.return_value = False presenter_mock.post_not_exists.side_effect = ObjectDoesNotExist with self.assertRaises(ObjectDoesNotExist): response = delete_post.delete_post(post_id) post_storage_mock.post_exists.assert_called_once_with(post_id) presenter_mock.post_not_exists.assert_called_once()
def test_delete_post_interactor_with_invalid_post_id_raise_exception(self): # Arrange user_id = 1 invalid_post_id = -1 post_storage = create_autospec(PostStorageInterface) presenter = create_autospec(PresenterInterface) interactor = DeletePostInteractor(post_storage=post_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.delete_post(user_id=user_id, post_id=invalid_post_id)
def test_delete_post_interactor_interactor_with_valid_details(self): # Arrange user_id = 1 post_id = 1 post_storage = create_autospec(PostStorageInterface) presenter = create_autospec(PresenterInterface) interactor = DeletePostInteractor(post_storage=post_storage, presenter=presenter) # Act interactor.delete_post(user_id=user_id, post_id=post_id) # Assert post_storage.is_valid_post_id.assert_called_once_with(post_id=post_id) post_storage.delete_post.assert_called_once_with(post_id=post_id)
def test_post_does_not_exists(self): post_storage_mock = create_autospec(PostStorage) presenter_mock = create_autospec(JsonPresenter) post_id = 1 post_storage_mock.post_exists.return_value = False presenter_mock.raise_post_does_not_exist_exception.side_effect\ = BadRequest delete_post_interactor = DeletePostInteractor(post_storage_mock, presenter_mock) with self.assertRaises(BadRequest): delete_post_interactor.delete_post(post_id) post_storage_mock.post_exists.assert_called_once_with(post_id)
def test_delete_post(self): post_storage_mock = Mock(spec=PostStorage) presenter_mock = Mock(spec=JsonPresenter) delete_post = DeletePostInteractor(post_storage_mock, presenter_mock) post_id = 1 response_data = {"status": "post deleted"} post_storage_mock.post_exists.return_value = True post_storage_mock.delete_post.return_value = response_data presenter_mock.get_delete_post_response.return_value = response_data response = delete_post.delete_post(post_id) post_storage_mock.delete_post.assert_called_once_with(post_id) presenter_mock.get_delete_post_response.assert_called_once_with( response_data) assert response == response_data
def test_delete_post_interactor_with_invalid_user_id_to_delete_post_raise_exception( self): # Arrange user_id = 2 post_id = 1 post_storage = create_autospec(PostStorageInterface) presenter = create_autospec(PresenterInterface) interactor = DeletePostInteractor(post_storage=post_storage, presenter=presenter) post_storage.is_post_created_by_user.return_value = False presenter.raise_user_cannot_delete_exception.side_effect = \ UserCannotDeleteException #TodDo -done change the exception # Act with pytest.raises(UserCannotDeleteException): interactor.delete_post(user_id=user_id, post_id=post_id)
def test_post_exists(self): post_storage_mock = create_autospec(PostStorage) presenter_mock = create_autospec(JsonPresenter) post_id = 1 response_data = None post_storage_mock.post_exists.return_value = True post_storage_mock.delete_post.return_value = response_data presenter_mock.get_delete_post_response.return_value = response_data delete_post_interactor = DeletePostInteractor(post_storage_mock, presenter_mock) response = delete_post_interactor.delete_post(post_id) post_storage_mock.post_exists.assert_called_once_with(post_id) post_storage_mock.delete_post.assert_called_once_with(post_id) presenter_mock.get_delete_post_response.assert_called_once_with() assert response == response_data