def test_should_return_the_first_error_if_many_validation_fails( mock_validate: MagicMock, sut: ValidationComposite, ): mock_validate.side_effect = [ Exception("first error"), Exception("second error"), Exception("third error"), ] input = dict(field="field") with pytest.raises(Exception) as excinfo: sut.validate(input) assert excinfo.value.args[0] == "first error"
def make_signup_validation(): signup_schema = Schema( dict( name=And(str, len, error="Invalid key: 'name'"), email=And(str, len, error="Invalid key: 'email'"), password=And( And(str, len, error="Invalid key: 'password'"), And( lambda s: len(s) > 5, error="Invalid key: 'password minimum 6 characters'", ), ), password_confirmation=And( And(str, len, error="Invalid key: 'password_confirmation'"), ), ) ) return ValidationComposite( [ RequiredFieldsValidation(signup_schema, "signup"), CompareFieldsValidation("password", "password_confirmation"), EmailValidation( "email", EmailValidatorAdapter(check_mx=False, skip_smtp=True) ), UniqueFieldValidation("email", MongoDbAdapter(get_collection("accounts"))), ] )
def test_should_return_an_error_if_any_validation_fails( mock_validate: MagicMock, sut: ValidationComposite, ): mock_validate.return_value = Exception("field error") input = dict(field="field") is_error = sut.validate(input) assert is_error.args[0] == "field error"
def make_login_validation(): login_schema = Schema( dict( email=And(str, len, error="Invalid key: 'email'"), password=And(str, len, error="Invalid key: 'password'"), )) return ValidationComposite([ RequiredFieldsValidation(login_schema, "login"), EmailValidation( "email", EmailValidatorAdapter(check_mx=False, skip_smtp=True), ), ])
def make_add_survey_validation(): add_survey_schema = Schema( dict( question=And(str, len, error="Invalid key: 'question'"), answers=And( And(list, len, error="Invalid key: 'answers'"), Schema( [ { Optional("image"): And(str, len, error="Invalid key: 'image'"), "answer": And(str, len, error="Invalid key: 'answer'"), }, ], error="Missing key: 'answer'", ), ), )) return ValidationComposite([ RequiredFieldsValidation(add_survey_schema, "add_survey"), ])
def test_should_not_return_if_all_validation_succeeds( sut: ValidationComposite, ): input = dict(field="field") is_error = sut.validate(input) assert not is_error
def sut(): validation_stub = ValidationStub() sut = ValidationComposite( [validation_stub, validation_stub, validation_stub]) yield sut