def test_error_messages_interface(): """ `errors.messages()` should return a list of Message instances. """ validator = Integer() value, error = validator.validate_or_error("abc") assert error.messages() == [Message(text="Must be a number.", code="type")]
def test_integer(): validator = Integer() value, error = validator.validate_or_error(123) assert value == 123 validator = Integer() value, error = validator.validate_or_error("123") assert value == 123 validator = Integer() value, error = validator.validate_or_error(123.0) assert value == 123 validator = Integer() value, error = validator.validate_or_error("123.0") assert value == 123 validator = Integer() value, error = validator.validate_or_error(None) assert error == ValidationError(text="May not be null.", code="null") validator = Integer() value, error = validator.validate_or_error("abc") assert error == ValidationError(text="Must be a number.", code="type") validator = Integer() value, error = validator.validate_or_error(True) assert error == ValidationError(text="Must be a number.", code="type") validator = Integer() value, error = validator.validate_or_error(123.1) assert error == ValidationError(text="Must be an integer.", code="integer") validator = Integer() value, error = validator.validate_or_error(float("inf")) assert error == ValidationError(text="Must be an integer.", code="integer") validator = Integer() value, error = validator.validate_or_error(float("nan")) assert error == ValidationError(text="Must be an integer.", code="integer") validator = Integer() value, error = validator.validate_or_error("123", strict=True) assert error == ValidationError(text="Must be a number.", code="type") validator = Integer(allow_null=True) value, error = validator.validate_or_error(None) assert value is None assert error is None validator = Integer(allow_null=True) value, error = validator.validate_or_error("") assert value is None assert error is None validator = Integer(allow_null=True) value, error = validator.validate_or_error("", strict=True) assert error == ValidationError(text="Must be a number.", code="type") validator = Integer(maximum=10) value, error = validator.validate_or_error(100) assert error == ValidationError(text="Must be less than or equal to 10.", code="maximum") validator = Integer(maximum=10) value, error = validator.validate_or_error(10) assert value == 10 validator = Integer(minimum=3) value, error = validator.validate_or_error(1) assert error == ValidationError(text="Must be greater than or equal to 3.", code="minimum") validator = Integer(minimum=3) value, error = validator.validate_or_error(3) assert value == 3 validator = Integer(exclusive_maximum=10) value, error = validator.validate_or_error(10) assert error == ValidationError(text="Must be less than 10.", code="exclusive_maximum") validator = Integer(exclusive_minimum=3) value, error = validator.validate_or_error(3) assert error == ValidationError(text="Must be greater than 3.", code="exclusive_minimum") validator = Integer(multiple_of=10) value, error = validator.validate_or_error(5) assert error == ValidationError(text="Must be a multiple of 10.", code="multiple_of")
def test_validation_error_is_hashable(): validator = Integer() _, error = validator.validate_or_error("abc") hash(error)