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_renderer_handles_empty_classes_and_enums(): """ Test renderer handles empty classes and enums. """ to_render = [ EnumDefinition(name="EmptyEnum", depends_on=set(), values=[]), ClassDefinition(name="EmptyClass", depends_on=set(), properties=[]), ] rendered_code = render_classes(to_render) assert rendered_code == ( "from enum import Enum\n" "from typing import Any, List, Optional\n" "\n" "from pydantic import BaseModel\n" "\n" "\n" "class EmptyEnum(Enum):\n" " pass\n" "\n" "\n" "class EmptyClass(BaseModel):\n" " pass\n" "\n" "\n" "EmptyClass.update_forward_refs()" "\n" "\n" )
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_renderer_renders_definitions_correctly(): """ Test that renderer renders definitions correctly. """ to_render = [ EnumDefinition( name="SomeNiceEnum", depends_on=set(), values=[("FIRST_VALUE", "first_value"), ("SECOND_VALUE", "second_value")], ), ClassDefinition( name="APythonClass", depends_on=set(), properties=[ PropertyDefinition( name="string_val", key="stringVal", value_type="str", known_type=True, ), PropertyDefinition( name="int_val", key="intVal", value_type="int", known_type=True ), ], ), ] rendered_code = render_classes(to_render) assert rendered_code == ( "from enum import Enum\n" "from typing import Any, List, Optional\n" "\n" "from pydantic import BaseModel\n" "\n" "\n" "class SomeNiceEnum(Enum):\n" ' FIRST_VALUE = "first_value"\n' ' SECOND_VALUE = "second_value"\n' "\n" "\n" "class APythonClass(BaseModel):\n" " string_val: Optional[str]\n" " int_val: Optional[int]\n" "\n" " class Config:\n" " fields = {\n" ' "string_val": "stringVal",\n' ' "int_val": "intVal",\n' " }\n" "\n" "\n" "APythonClass.update_forward_refs()" "\n" "\n" )
def test_renderer_aliases_properties(): """ Test that renderer aliases properties. """ to_render = [ ClassDefinition( name="ClassWithAliases", depends_on=set(), properties=[ PropertyDefinition( name="self", key="self", value_type="str", known_type=True ), PropertyDefinition( name="from", key="from", value_type="int", known_type=True ), PropertyDefinition( name="non_aliased", key="nonAliased", value_type="Optional[str]", known_type=True, ), ], ), ] rendered_code = render_classes(to_render) assert rendered_code == ( "from enum import Enum\n" "from typing import Any, List, Optional\n" "\n" "from pydantic import BaseModel\n" "\n" "\n" "class ClassWithAliases(BaseModel):\n" " self_: Optional[str]\n" " from_: Optional[int]\n" " non_aliased: Optional[str]\n" "\n" " class Config:\n" " fields = {\n" ' "self_": "self",\n' ' "from_": "from",\n' ' "non_aliased": "nonAliased",\n' " }\n" "\n" "\n" "ClassWithAliases.update_forward_refs()" "\n" "\n" )
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_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_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_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(), )
def test_renderer_renders_custom_attributes_correctly(): """ Test that renderer renders definitions correctly. """ to_render = [ ClassDefinition( name="ClassWithUnknownTypes", depends_on=set(), properties=[ PropertyDefinition( name="first_prop", key="firstProp", value_type="FirstUnknownType", known_type=False, ), PropertyDefinition( name="second_prop", key="secondProp", value_type="List[SecondUnknownType]", known_type=False, ), PropertyDefinition( name="third_prop", key="thirdProp", value_type="Optional[ThirdUnknownType]", known_type=False, ), PropertyDefinition( name="any_prop", key="anyProp", value_type="Any", known_type=True ), ], ), ] rendered_code = render_classes(to_render) assert rendered_code == ( "from enum import Enum\n" "from typing import Any, List, Optional\n" "\n" "from pydantic import BaseModel\n" "\n" "\n" "class ClassWithUnknownTypes(BaseModel):\n" ' first_prop: Optional["FirstUnknownType"]\n' ' second_prop: Optional[List["SecondUnknownType"]]\n' ' third_prop: Optional["ThirdUnknownType"]\n' " any_prop: Any\n" "\n" " class Config:\n" " fields = {\n" ' "first_prop": "firstProp",\n' ' "second_prop": "secondProp",\n' ' "third_prop": "thirdProp",\n' ' "any_prop": "anyProp",\n' " }\n" "\n" "\n" "ClassWithUnknownTypes.update_forward_refs()" "\n" "\n" )