def test_type_builder_handles_all_of_references(): """ Test type builder handles all of references. """ schema = [ SchemaObject( name="ClassWithAllOf", properties=[ SchemaAllOf( name="authorValue", all_of=[ SchemaReference(name="", reference="ReferencedObject"), SchemaObject( name="role", properties=[ SchemaEnum( name="", value_type="string", values=["AUTHOR"] ), ], ), ], ) ], ), SchemaObject( name="ReferencedObject", properties=[SchemaValue(name="stringValue", value_type="string")], ), ] build_result = build_types(schema) assert len(build_result) == 2 assert build_result[0] == ClassDefinition( name="ClassWithAllOf", properties=[ PropertyDefinition( name="author_value", key="authorValue", value_type="ReferencedObject", known_type=False, ) ], depends_on={"ReferencedObject"}, ) assert build_result[1] == ClassDefinition( name="ReferencedObject", properties=[ PropertyDefinition( name="string_value", key="stringValue", value_type="str", known_type=True, ) ], depends_on=set(), )
def test_type_builder_handles_reference_types(): """ Test type builder handles reference types correctly. """ schema = [ SchemaObject( name="ObjectA", properties=[SchemaReference(name="refB", reference="ObjectB")], ), SchemaObject( name="ObjectB", properties=[SchemaReference(name="refC", reference="ObjectC")], ), SchemaObject( name="ObjectC", properties=[SchemaValue(name="intValue", value_type="number")], ), ] build_result = build_types(schema) assert len(build_result) == 3 assert build_result[0] == ClassDefinition( name="ObjectA", properties=[ PropertyDefinition( name="ref_b", key="refB", value_type="ObjectB", known_type=False ) ], depends_on={"ObjectB"}, ) assert build_result[1] == ClassDefinition( name="ObjectB", properties=[ PropertyDefinition( name="ref_c", key="refC", value_type="ObjectC", known_type=False ) ], depends_on={"ObjectC"}, ) assert build_result[2] == ClassDefinition( name="ObjectC", properties=[ PropertyDefinition( name="int_value", key="intValue", value_type="int", known_type=True ) ], depends_on=set(), )
def test_schema_parser_parses_multiple_type_definitions(): """ Test schema parser handles multiple type definitions. """ schema = """{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "MultiTypedObject": { "properties": { "html": { "type": [ "string", "null" ] } }, "type": "object" } } }""" definitions = parse_schema(schema) assert len(definitions) == 1 assert definitions[0] == SchemaObject( name="MultiTypedObject", properties=[SchemaValue(name="html", value_types=["string", "null"])], )
def test_type_builder_handles_optional_references(): """ Test type builder handles optional references. """ schema = [ SchemaObject( name="ObjectA", properties=[ SchemaAnyOf( name="refB", any_of=[ SchemaReference(name="refB", reference="ObjectB"), SchemaValue(name="refB", value_type="null"), ], ) ], ), SchemaObject( name="ObjectB", properties=[SchemaValue(name="strField", value_type="string")], ), ] build_result = build_types(schema) assert len(build_result) == 2 assert build_result[0] == ClassDefinition( name="ObjectA", properties=[ PropertyDefinition( name="ref_b", key="refB", value_type="Optional[ObjectB]", known_type=False, ) ], depends_on={"ObjectB"}, ) assert build_result[1] == ClassDefinition( name="ObjectB", properties=[ PropertyDefinition( name="str_field", key="strField", value_type="str", known_type=True ) ], depends_on=set(), )
def test_type_builder_resolves_union_types_to_any(): """ Test type builder resolves union types to Any. """ schema = [ SchemaObject( name="ObjectA", properties=[ SchemaAnyOf( name="refB", any_of=[ SchemaReference(name="refB", reference="ObjectB"), SchemaValue(name="refB", value_type="str"), ], ) ], ), SchemaObject( name="ObjectB", properties=[SchemaValue(name="strField", value_type="string")], ), ] build_result = build_types(schema) assert len(build_result) == 2 assert build_result[0] == ClassDefinition( name="ObjectA", properties=[ PropertyDefinition( name="ref_b", key="refB", value_type="Any", known_type=True, ) ], depends_on=set(), ) assert build_result[1] == ClassDefinition( name="ObjectB", properties=[ PropertyDefinition( name="str_field", key="strField", value_type="str", known_type=True ) ], depends_on=set(), )
def test_type_builder_builds_correct_model_for_simple_class(): """ Test type builder builds correct model for simple class. """ schema = [ SchemaObject( name="TestClass", properties=[ SchemaValue(name="stringValue", value_type="string"), SchemaValue(name="booleanValue", value_type="boolean"), SchemaValue(name="anyValue", value_type="any"), SchemaValue(name="nullValue", value_type="null"), SchemaValue(name="optionalStringValue", value_types=["null", "string"]), ], ) ] build_result = build_types(schema) assert len(build_result) == 1 assert build_result[0] == ClassDefinition( name="TestClass", properties=[ PropertyDefinition( name="string_value", key="stringValue", value_type="str", known_type=True, ), PropertyDefinition( name="boolean_value", key="booleanValue", value_type="bool", known_type=True, ), PropertyDefinition( name="any_value", key="anyValue", value_type="Any", known_type=True ), PropertyDefinition( name="null_value", key="nullValue", value_type="Any", known_type=True ), PropertyDefinition( name="optional_string_value", key="optionalStringValue", value_type="Optional[str]", known_type=True, ), ], depends_on=set(), )
def test_type_builder_raises_exception_on_invalid_schema_item_type(): """ Test type builder raises an exception on invalid schema item type. """ class UnknownSchemaItem(SchemaItem): pass schema = [ SchemaObject( name="FakeObject", properties=[UnknownSchemaItem(name="objectUnknown")] ) ] with pytest.raises(ValueError): _ = build_types(schema)
def test_type_builder_handles_enums(): """ Test type builder handles enums correctly. """ schema = [ SchemaObject( name="ClassWithEnums", properties=[ SchemaValue(name="string_value", value_type="string"), SchemaEnum( name="enumValue", value_type="string", values=["first", "second", "third"], ), ], ) ] build_result = build_types(schema) assert len(build_result) == 2 assert build_result[0] == ClassDefinition( name="ClassWithEnums", properties=[ PropertyDefinition( name="string_value", key="string_value", value_type="str", known_type=True, ), PropertyDefinition( name="enum_value", key="enumValue", value_type="ClassWithEnumsEnumValue", known_type=False, ), ], depends_on={"ClassWithEnumsEnumValue"}, ) assert build_result[1] == EnumDefinition( name="ClassWithEnumsEnumValue", values=[("FIRST", "first"), ("SECOND", "second"), ("THIRD", "third")], depends_on=set(), )
def test_schema_parser_parses_any_of_type(): """ Test schema parser parses any of type. """ schema = """{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "AnyOfObject": { "properties": { "user": { "anyOf": [ { "$ref": "#/definitions/GitLabUser" }, { "type": "null" } ] } }, "type": "object" } } }""" definitions = parse_schema(schema) assert len(definitions) == 1 assert definitions[0] == SchemaObject( name="AnyOfObject", properties=[ SchemaAnyOf( name="user", any_of=[ SchemaReference(name="user", reference="GitLabUser"), SchemaValue(name="user", value_type="null"), ], ) ], )
def test_type_builder_handles_specific_non_camel_case_property_names(): """ Test type builder handles specific non-camel case property names. """ schema = [ SchemaObject( name="NonStandardObject", properties=[ SchemaValue(name="baseURL", value_type="string"), SchemaValue(name="DEFAULTS", value_type="any"), SchemaValue(name="thisPR", value_type="null"), SchemaValue(name="instanceID", value_type="number"), ], ) ] build_result = build_types(schema) assert len(build_result) == 1 assert build_result[0] == ClassDefinition( name="NonStandardObject", properties=[ PropertyDefinition( name="base_url", key="baseURL", value_type="str", known_type=True, ), PropertyDefinition( name="defaults", key="DEFAULTS", value_type="Any", known_type=True, ), PropertyDefinition( name="this_pr", key="thisPR", value_type="Any", known_type=True ), PropertyDefinition( name="instance_id", key="instanceID", value_type="int", known_type=True ), ], depends_on=set(), )
def test_schema_parser_parses_definitions(): """ Test that schema parser parses definitions. """ schema = """{ "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "BitBucketCloudCommitsLinks": { "properties": { "html": { "properties": { "href": { "type": "string" } }, "type": "object" }, "ref_property": { "$ref": "#/definitions/BitBucketCloudPRDSL" } }, "type": "object" }, "EnumType": { "properties": { "enum_property": { "enum": [ "NORMAL", "SERVICE" ], "type": "string" }, "arr_property": { "items": { "$ref": "#/definitions/SomeDefinition" }, "type": "array" } }, "type": "object" }, "AllOfArrayType": { "properties": { "all_of_array": { "items": { "allOf": [ { "$ref": "#/definitions/SomeReference" }, { "properties": { "role": { "enum": [ "AUTHOR" ], "type": "string" } }, "type": "object" } ] }, "type": "array" } }, "type": "object" } } }""" definitions = parse_schema(schema) assert len(definitions) == 3 assert definitions == [ SchemaObject( name="AllOfArrayType", properties=[ SchemaArray( name="all_of_array", item=SchemaAllOf( name="", all_of=[ SchemaReference(name="", reference="SomeReference"), SchemaObject( name="", properties=[ SchemaEnum( name="role", value_type="string", values=["AUTHOR"], ) ], ), ], ), ) ], ), SchemaObject( name="BitBucketCloudCommitsLinks", properties=[ SchemaObject( name="html", properties=[SchemaValue(name="href", value_type="string")], ), SchemaReference(name="ref_property", reference="BitBucketCloudPRDSL"), ], ), SchemaObject( name="EnumType", properties=[ SchemaArray( name="arr_property", item=SchemaReference(name="", reference="SomeDefinition"), ), SchemaEnum( name="enum_property", value_type="string", values=["NORMAL", "SERVICE"], ), ], ), ]
def test_type_builder_handles_arrays(): """ Test that type builder handles arrays. """ schema = [ SchemaObject( name="ClassWithAllOfArray", properties=[ SchemaArray( name="authorsArray", item=SchemaAllOf( name="", all_of=[ SchemaReference(name="", reference="SomeOtherObject"), SchemaObject( name="role", properties=[ SchemaEnum( name="", value_type="string", values=["PARTICIPANT"], ) ], ), ], ), ) ], ), SchemaObject( name="SomeOtherObject", properties=[SchemaValue(name="anyProperty", value_type="number")], ), SchemaObject( name="ClassWithPropertyArray", properties=[ SchemaArray( name="propertiesArray", item=SchemaObject( name="", properties=[ SchemaValue(name="firstField", value_type="string"), SchemaValue(name="secondField", value_type="number"), ], ), ) ], ), ] build_result = build_types(schema) assert len(build_result) == 4 assert build_result[0] == ClassDefinition( name="ClassWithAllOfArray", properties=[ PropertyDefinition( name="authors_array", key="authorsArray", value_type="List[SomeOtherObject]", known_type=False, ) ], depends_on={"SomeOtherObject"}, ) assert build_result[1] == ClassDefinition( name="ClassWithPropertyArray", properties=[ PropertyDefinition( name="properties_array", key="propertiesArray", value_type="List[ClassWithPropertyArrayPropertiesArray]", known_type=False, ) ], depends_on={"ClassWithPropertyArrayPropertiesArray"}, ) assert build_result[2] == ClassDefinition( name="ClassWithPropertyArrayPropertiesArray", properties=[ PropertyDefinition( name="first_field", key="firstField", value_type="str", known_type=True ), PropertyDefinition( name="second_field", key="secondField", value_type="int", known_type=True, ), ], depends_on=set(), ) assert build_result[3] == ClassDefinition( name="SomeOtherObject", properties=[ PropertyDefinition( name="any_property", key="anyProperty", value_type="int", known_type=True, ) ], depends_on=set(), )
def test_type_builder_handles_nested_properties(): """ Test type builder handles nested properties correctly. """ schema = [ SchemaObject( name="ClassWithNestedClass", properties=[ SchemaObject( name="nestedValue", properties=[ SchemaValue(name="string_value", value_type="string"), SchemaEnum( name="enum_value", value_type="string", values=["hey", "new", "value"], ), ], ), ], ) ] build_result = build_types(schema) assert len(build_result) == 3 assert build_result[0] == ClassDefinition( name="ClassWithNestedClass", properties=[ PropertyDefinition( name="nested_value", key="nestedValue", value_type="ClassWithNestedClassNestedValue", known_type=False, ), ], depends_on={"ClassWithNestedClassNestedValue"}, ) assert build_result[1] == ClassDefinition( name="ClassWithNestedClassNestedValue", properties=[ PropertyDefinition( name="string_value", key="string_value", value_type="str", known_type=True, ), PropertyDefinition( name="enum_value", key="enum_value", value_type="ClassWithNestedClassNestedValueEnumValue", known_type=False, ), ], depends_on={"ClassWithNestedClassNestedValueEnumValue"}, ) assert build_result[2] == EnumDefinition( name="ClassWithNestedClassNestedValueEnumValue", values=[("HEY", "hey"), ("NEW", "new"), ("VALUE", "value")], depends_on=set(), )