def validate(self, request): validated_data = request for validator in self.validators: validation_result = validator( request=validated_data, dependencies=get_attribute_dependencies(self) ) # validator is eligible to alter input data for the decorated function # but doesnt have to; if it returns no result, his input will be used validated_data = validated_data if validation_result is None else validation_result return validated_data
def test_interactor_error_not_caught(self, interactor, request_data, validators, teams): error_instance = ValueError() teams.find.side_effect = error_instance with pytest.raises(ValueError): interactor(request_data) dependencies = get_attribute_dependencies(interactor) validators[0].assert_called_once_with(request=request_data, dependencies=dependencies) validators[1].assert_called_once_with(request=request_data, dependencies=dependencies) teams.find.assert_called_once_with(request_data['team_id']) interactor.error_handler.assert_not_called()
def test_late_validation_error_caught(self, interactor, request_data, error_result, validators): error_instance = LogicError('foo') validators[1].side_effect = error_instance response = interactor(request_data) dependencies = get_attribute_dependencies(interactor) assert response == error_result validators[0].assert_called_once_with(request=request_data, dependencies=dependencies) validators[1].assert_called_once_with(request=request_data, dependencies=dependencies) interactor.error_handler.assert_called_once_with(error=error_instance, request=request_data)
def test_multiple_error_class(self, interactor, request_data, validators, error_result, teams): error_instance = ValueError() interactor.error_class = (LogicError, ValueError) teams.find.side_effect = error_instance response = interactor(request_data) dependencies = get_attribute_dependencies(interactor) assert response == error_result validators[0].assert_called_once_with(request=request_data, dependencies=dependencies) validators[1].assert_called_once_with(request=request_data, dependencies=dependencies) teams.find.assert_called_once_with(request_data['team_id']) interactor.error_handler.assert_called_once_with(error=error_instance, request=request_data)
def test_get_dependencies_no_description(self): # instance is an non-DI-aware object instance = object() assert get_attribute_dependencies(instance) == {}
def test_get_dependencies_successful(self, instance): dependencies = get_attribute_dependencies(instance) assert {k: v.name for k, v in dependencies.items()} == instance.components