def test_ber_instantiate_interactor(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) result = ber._instantiate_post_interactor(mock_req) mock_inter.assert_called_with(mock_req, mock_adapter, mock_class) assert result == mock_inter.return_value
def test_ber_get_validate_error(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(ValidationErrorException) as excinfo: ber.get_by_id('não importa') assert 'Erro de validação obtendo' in str(excinfo.value)
def test_ber_get_not_found(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(NotFoundException) as excinfo: ber.get_by_id('123asdf123') assert '123asdf123 não encontrado' in str(excinfo.value)
def test_ber_get(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) result = ber.get_by_id('meu id') mock_req.assert_called_with('meu id') mock_inter.assert_called_with(mock_req.return_value, mock_adapter) mock_inter.return_value.run.assert_called_once() assert result == mock_inter.return_value.run.return_value
def test_ber_put_validation_exception(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(ValidationErrorException) as exc_info: ber.put('meu id', 'data') assert 'Erro de validação atualizando' in str(exc_info.value) assert '123inválido321' in str(exc_info.value)
def test_ber_put_exception(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(PutException) as exc_info: ber.put('meu id', 'data') assert 'Erro atualizando' in str(exc_info.value) assert 'gravou não' in str(exc_info.value)
def test_ber_get_general_error(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(UnexpectedErrorException) as excinfo: ber.get_by_id('não importa') assert 'Erro obtendo' in str(excinfo.value) assert 'Errado!' in str(excinfo.value) assert 'ValueError' in str(excinfo.value)
def test_ber_instantiate_custom_delete_interactor(mock_req): mock_adapter = MagicMock() mock_class = MagicMock() mock_inter = MagicMock() ber = BasicEntityRoutes(adapter_instance=mock_adapter, entity_class=mock_class, custom_delete_interactor_calss=mock_inter) result = ber._instantiate_delete_interactor(mock_req) mock_inter.assert_called_with(mock_req, mock_adapter) assert result == mock_inter.return_value
def test_ber_get_all_validate_error(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() mock_query = MagicMock() mock_unit = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(ValidationErrorException) as excinfo: ber.get_all(mock_query, mock_unit) assert 'Erro de validação obtendo lista de' in str(excinfo.value)
def test_ber_put_general_exception(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(UnexpectedErrorException) as exc_info: ber.put('meu id', 'data') assert 'Erro atualizando' in str(exc_info.value) assert 'Oops!' in str(exc_info.value) assert 'ValueError' in str(exc_info.value)
def test_ber_post(mock_req): mock_adapter = MagicMock() mock_class = MagicMock() with patch.object(BasicEntityRoutes, '_instantiate_post_interactor') as mock: ber = BasicEntityRoutes(mock_adapter, mock_class) result = ber.post('data') mock_req.assert_called_with('data') mock.assert_called_with(mock_req.return_value) mock.return_value.run.assert_called_once() assert result == mock.return_value.run.return_value
def test_ber_get_all(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() mock_query = MagicMock() mock_unit = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) result = ber.get_all(mock_query, mock_unit) mock_req.assert_called_with(mock_query, mock_unit) mock_inter.assert_called_with(mock_req.return_value, mock_adapter) mock_inter.return_value.run.assert_called_once() assert result == mock_inter.return_value.run.return_value
def test_ber_get_all_general_error(mock_inter, mock_req): mock_adapter = MagicMock() mock_class = MagicMock() mock_query = MagicMock() mock_unit = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(UnexpectedErrorException) as excinfo: ber.get_all(mock_query, mock_unit) assert 'Erro obtendo lista de' in str(excinfo.value) assert 'Errado!' in str(excinfo.value) assert 'ValueError' in str(excinfo.value)
def test_ber_post_validate_error(mock_req): mock_adapter = MagicMock() mock_class = MagicMock() with patch.object(BasicEntityRoutes, '_instantiate_post_interactor', return_value=MagicMock(run=MagicMock( side_effect=ValidationError('oops')))): ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(ValidationErrorException) as excinfo: ber.post('não importa') assert 'Erro de validação criando' in str(excinfo.value) assert 'oops' in str(excinfo.value)
def test_ber_post_general_error(mock_req): mock_adapter = MagicMock() mock_class = MagicMock() with patch.object(BasicEntityRoutes, '_instantiate_post_interactor', return_value=MagicMock(run=MagicMock( side_effect=ValueError('Errado!')))): ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(UnexpectedErrorException) as excinfo: ber.post('não importa') assert 'Erro criando' in str(excinfo.value) assert 'Errado!' in str(excinfo.value) assert 'ValueError' in str(excinfo.value)
def test_ber_object_name(): mock_adapter = MagicMock(adapted_class_name='klasse') mock_class = MagicMock() ber = BasicEntityRoutes(mock_adapter, mock_class) result = ber._object_name assert result == 'klasse'
def test_ber_post_save_error(mock_req): mock_adapter = MagicMock() mock_class = MagicMock() ipi = '_instantiate_post_interactor' with patch.object(BasicEntityRoutes, ipi, return_value=MagicMock(run=MagicMock( side_effect=SaveEntityException('save error')))): ber = BasicEntityRoutes(mock_adapter, mock_class) with raises(PostException) as excinfo: ber.post('não importa') assert 'Erro gravando' in str(excinfo.value) assert 'save error' in str(excinfo.value)