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_validate_number_string(): input_schema = {"id": prop.Number()} given_request = {"id": "1,000"} http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == {} assert http_status is HTTPStatus.OK
def test_validate_input_string_id_not_negative_number(): input_schema = { "id": prop.Number(), } given_request = {"id": "-2"} http_status, reject_dict = validate_input(given_request, input_schema) assert reject_dict == {} assert http_status is HTTPStatus.OK
def test_output_number(): output_schema = { HTTPStatus.OK: { "basic_number": prop.Number(), } } result = HTTPStatus.OK, {"basic_number": 80.99} http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == {"basic_number": 80.99}
def test_output_number_string(): output_schema = { HTTPStatus.OK: { "basic_number": prop.Number(), } } result = HTTPStatus.OK, {"basic_number": "80.99"} with pytest.raises(prop.ValidationError) as e: create_output(result, output_schema) assert e.value.message == "The value '80.99' from field 'basic_number' is the wrong type, expected: Number"
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_string_id_not_a_number(): input_schema = { "id": 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 " "the wrong type, expected: Number" } } assert http_status == HTTPStatus.BAD_REQUEST
def test_validate_number_social_security_number(): input_schema = { "social_security_number": prop.Number(validators=[validators.SocialSecurityNumber()]), } given_request = { "social_security_number": "219099999", } 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': 'SocialSecurityNumber is not supported for class Number!!' } } assert http_status == HTTPStatus.BAD_REQUEST
class NestedFields(Fields): base_schema = { "top_level_id": prop.Integer(description="The top level ID.", ), "top_level_array": prop.Array(repeated_structure=prop.Object( structure={ "nested_id": prop.Integer("The nested ID."), "nested_array": prop.Array(repeated_structure=prop.Object( structure={ "super_nested_id": prop.Integer(description="The super nested ID."), "another_number": prop.Number(description="The super nested number.", ), }), ), })), } def __init__(self): super().__init__(self.base_schema)