def test_nullable_array_of_strings(): output_schema = { HTTPStatus.OK: { "basic_string": prop.String(), "basic_boolean": prop.Boolean(), "array_of_strings": prop.Array( repeated_structure=prop.String("just a list of strings"), description="A description of what this array contains", required=True, nullable=False) } } result = HTTPStatus.OK, { "basic_string": "test string", "basic_boolean": True, "array_of_strings": None } with pytest.raises(prop.ValidationError) as e: create_output(result, output_schema) assert e.value.message == "Non nullable field 'array_of_strings' is null!"
def test_create_output_array_of_strings(): output_schema = { HTTPStatus.OK: { "basic_string": prop.String(), "basic_boolean": prop.Boolean(), "array_of_strings": prop.Array( repeated_structure=prop.String("just a list of strings"), description="A description of what this array contains") } } result = HTTPStatus.OK, { "basic_string": "test string", "basic_boolean": True, "array_of_strings": ["hello", "hello again"] } http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == { "basic_string": "test string", "basic_boolean": True, "array_of_strings": ["hello", "hello again"] }
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)
def test_nested_specializer_object_with_results_structure_and_specialize(): output_schema = { HTTPStatus.OK: { "results": prop.Array(repeated_structure=prop.Object( structure=NestedFields().specialize(only=["top_level_id"]), description="A basic field object")) } } result = HTTPStatus.OK, { "results": [ { "top_level_id": 31, "don_t_return_me": "super secret data" }, { "top_level_id": 32 }, { "top_level_id": 33 }, { "top_level_id": 34, "don_t_return_me": "even more secret data" }, ] } http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == { "results": [ { "top_level_id": 31 }, { "top_level_id": 32 }, { "top_level_id": 33 }, { "top_level_id": 34 }, ] }
def test_nested_specializer_object_with_results_array_of_strings(): output_schema = { HTTPStatus.OK: { "results": prop.Array(repeated_structure=prop.String( "this is an array of strings", required=True, nullable=False)) } } result = HTTPStatus.OK, { "results": ["perfectly", "valid", "array", "of", "strings"] } http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == { "results": ["perfectly", "valid", "array", "of", "strings"] }
def test_create_output_array_of_objects(): output_schema = { HTTPStatus.OK: { "basic_string": prop.String(), "basic_boolean": prop.Boolean(), "array_of_objects": prop.Array(repeated_structure=prop.Object( structure={"sub_string": prop.String("the string")}), description="A list of plans.") } } result = HTTPStatus.OK, { "basic_string": "test string", "basic_boolean": True, "array_of_objects": [{ "sub_string": "hello", "not_this": "not this" }, { "sub_string": "hello", "not_this": "still not this", "another_one": "???" }] } http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == { "basic_string": "test string", "basic_boolean": True, "array_of_objects": [{ "sub_string": "hello", }, { "sub_string": "hello", }] }
def test_very_nested_nullable_field(): output_schema = { HTTPStatus.OK: { "basic_string": prop.String(), "basic_boolean": prop.Boolean(), "array_of_objects": prop.Array(repeated_structure=prop.Object( structure={ "sub_string": prop.String("the string", required=True, nullable=False), "required_one": prop.String(required=True, nullable=False) }), description="A list of plans.") } } result = HTTPStatus.OK, { "basic_string": "test string", "basic_boolean": True, "array_of_objects": [{ "sub_string": None, "required_one": "here" }, { "sub_string": "hello", "required_one": "here_too" }] } with pytest.raises(prop.ValidationError) as e: create_output(result, output_schema) assert e.value.message == "Non nullable field 'sub_string' is null!"
def test_nested_specializer_object_with_results_structure(): output_schema = { HTTPStatus.OK: { "results": prop.Array(repeated_structure=prop.Object( structure=SimpleFields().all(), description="A basic field object")) } } result = HTTPStatus.OK, { "results": [{ "message": "hello" }, { "message": "hello" }, { "message": "hello" }, { "message": "hello" }] } http_status_code, returned_dict = create_output(result, output_schema) assert http_status_code == 200 assert returned_dict == { "results": [{ "message": "hello" }, { "message": "hello" }, { "message": "hello" }, { "message": "hello" }] }