def test_validate_input_one_of_invalid(): input_schema = { "id": prop_wrapper.OneOf([ prop.String(validators=[validators.ExactLength(3)]), prop.String(validators=[validators.ExactLength(8)]), prop.Number() ]) } given_request = {"id": "banana_phone"} http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == { 'message': 'A field has an error.', 'field_error_messages': { 'id': ("The value 'banana_phone' from field 'id' is not valid for one of the defined props for " "the following reasons: String is not the correct length! The string 'banana_phone' is " "12 characters long, not 3!, String is not the correct length! The string 'banana_phone' is " "12 characters long, not 8!, The value 'banana_phone' from field 'id' is the wrong " 'type, expected: Number') } } assert http_status == HTTPStatus.BAD_REQUEST
def test_multiple_invalid_fields_validation(): input_schema = { "social_security_number": prop.String(validators=[validators.SocialSecurityNumber()]), "exact_length_string": prop.String(validators=[validators.ExactLength(3)]), } given_request = { "social_security_number": "578234", "exact_length_string": "not three long", } http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == { 'message': 'Multiple fields have an error.', 'field_error_messages': { 'exact_length_string': "String is not the correct length! " "The string 'not three long' is 14 characters long, not 3!", 'social_security_number': '578234 is not a valid social security number!' } } assert http_status == HTTPStatus.BAD_REQUEST
def test_validate_input_one_of(): input_schema = { "id": prop_wrapper.OneOf([ prop.String(validators=[validators.ExactLength(3)]), prop.String(validators=[validators.ExactLength(8)]), prop.Number() ]) } given_request = {"id": "abc"} http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == {} assert http_status is HTTPStatus.OK
def test_validate_input_exact_length_string(): input_schema = { "basic_string": prop.String(validators=[validators.ExactLength(8)]), } given_request = { "basic_string": "noteightlong", } http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == { 'message': 'A field has an error.', 'field_error_messages': { 'basic_string': 'String is not the correct length! The string \'noteightlong\' is 12 characters long, not 8!' } } assert http_status == HTTPStatus.BAD_REQUEST
def test_impossible_validation(): input_schema = { "social_security_number": prop.String(validators=[ validators.SocialSecurityNumber(), validators.ExactLength(3) ]), } given_request = { "social_security_number": "578271234", } http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == { 'message': 'A field has an error.', 'field_error_messages': { 'social_security_number': 'String is not the correct length! The string \'578271234\' is 9 characters long, not 3!' } } assert http_status == HTTPStatus.BAD_REQUEST
def test_validate_input_exact_length_object(): input_schema = { "basic_object": prop.Object(structure={"basic_string": prop.String()}, validators=[validators.ExactLength(8)]), } given_request = { "basic_object": { "basic_string": "this is irrelevant", } } http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == { 'message': 'A field has an error.', 'field_error_messages': { 'basic_object': 'ExactLength is not supported for class Object!!' } } assert http_status == HTTPStatus.BAD_REQUEST
def test_one_of_invalid(): output_schema = { HTTPStatus.OK: { "basic_string": prop_wrapper.OneOf([ prop.String(validators=[validators.ExactLength(2)]), prop.Boolean() ]) } } result = HTTPStatus.OK, {"basic_string": "asdf"} with pytest.raises(prop.ValidationError) as e: create_output(result, output_schema) assert e.value.message == ( "The value 'asdf' from field 'basic_string' is not " "valid for one of the defined props for the following " "reasons: String is not the correct length! The string " "'asdf' is 4 characters long, not 2!, The value 'asdf' " "from field 'basic_string' is the wrong type, expected: Boolean")