def test_property_from_data_ref_enum(self, enum_property_factory): from openapi_python_client.parser.properties import Class, Schemas, property_from_data name = "some_enum" data = oai.Reference.construct(ref="#/components/schemas/MyEnum") existing_enum = enum_property_factory( name="an_enum", required=False, values={"A": "a"}, class_info=Class(name="MyEnum", module_name="my_enum"), ) schemas = Schemas( classes_by_reference={"/components/schemas/MyEnum": existing_enum}) prop, new_schemas = property_from_data(name=name, required=False, data=data, schemas=schemas, parent_name="", config=Config()) assert prop == enum_property_factory( name="some_enum", required=False, values={"A": "a"}, class_info=Class(name="MyEnum", module_name="my_enum"), ) assert schemas == new_schemas
def test_property_from_data_str_enum_with_null(self, enum_property_factory): from openapi_python_client.parser.properties import Class, Schemas, property_from_data from openapi_python_client.schema import Schema existing = enum_property_factory() data = Schema(title="AnEnum", enum=["A", "B", "C", None], nullable=False, default="B") name = "my_enum" required = True schemas = Schemas(classes_by_name={"AnEnum": existing}) prop, new_schemas = property_from_data( name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=Config() ) # None / null is removed from enum, and property is now nullable assert prop == enum_property_factory( name=name, required=required, nullable=True, values={"A": "A", "B": "B", "C": "C"}, class_info=Class(name="ParentAnEnum", module_name="parent_an_enum"), value_type=str, default="ParentAnEnum.B", ) assert prop.nullable is True assert schemas != new_schemas, "Provided Schemas was mutated" assert new_schemas.classes_by_name == { "AnEnum": existing, "ParentAnEnum": prop, }
def test_property_from_data_str_enum(self, mocker): from openapi_python_client.parser.properties import Class, EnumProperty from openapi_python_client.schema import Schema data = Schema(title="AnEnum", enum=["A", "B", "C"], nullable=False, default="B") name = "my_enum" required = True from openapi_python_client.parser.properties import Schemas, property_from_data schemas = Schemas(classes_by_name={"AnEnum": mocker.MagicMock()}) prop, new_schemas = property_from_data( name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=Config() ) assert prop == EnumProperty( name="my_enum", required=True, nullable=False, values={"A": "A", "B": "B", "C": "C"}, class_info=Class(name="ParentAnEnum", module_name="parent_an_enum"), value_type=str, default="ParentAnEnum.B", ) assert schemas != new_schemas, "Provided Schemas was mutated" assert new_schemas.classes_by_name == { "AnEnum": schemas.classes_by_name["AnEnum"], "ParentAnEnum": prop, }
def test_property_from_data_int_enum(self, enum_property_factory): from openapi_python_client.parser.properties import Class, Schemas, property_from_data from openapi_python_client.schema import Schema name = "my_enum" required = True nullable = False data = Schema.construct(title="anEnum", enum=[1, 2, 3], nullable=nullable, default=3) existing = enum_property_factory() schemas = Schemas(classes_by_name={"AnEnum": existing}) prop, new_schemas = property_from_data( name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=Config() ) assert prop == enum_property_factory( name=name, required=required, nullable=nullable, values={"VALUE_1": 1, "VALUE_2": 2, "VALUE_3": 3}, class_info=Class(name="ParentAnEnum", module_name="parent_an_enum"), value_type=int, default="ParentAnEnum.VALUE_3", ) assert schemas != new_schemas, "Provided Schemas was mutated" assert new_schemas.classes_by_name == { "AnEnum": existing, "ParentAnEnum": prop, }
def test_property_from_data_ref_enum_with_invalid_default( self, enum_property_factory): from openapi_python_client.parser.properties import Class, Schemas, property_from_data name = "some_enum" data = oai.Schema.construct( default="x", allOf=[oai.Reference.construct(ref="#/components/schemas/MyEnum")]) existing_enum = enum_property_factory( name="an_enum", default="MyEnum.A", values={ "A": "a", "B": "b" }, class_info=Class(name="MyEnum", module_name="my_enum"), python_name="an_enum", ) schemas = Schemas( classes_by_reference={"/components/schemas/MyEnum": existing_enum}) prop, new_schemas = property_from_data(name=name, required=False, data=data, schemas=schemas, parent_name="", config=Config()) assert schemas == new_schemas assert prop == PropertyError( data=data, detail="x is an invalid default for enum MyEnum")
def test_parse_multipart_body_existing_schema(self, mocker, model_property_factory): from openapi_python_client.parser.openapi import Endpoint, Schemas from openapi_python_client.parser.properties import Class class_info = Class(name="class_name", module_name="module_name") prop_before = model_property_factory(class_info=class_info, is_multipart_body=False) schemas_before = Schemas(classes_by_name={class_info.name: prop_before}) schema = mocker.MagicMock() body = oai.RequestBody.construct( content={"multipart/form-data": oai.MediaType.construct(media_type_schema=schema)} ) config = MagicMock() property_from_data = mocker.patch( f"{MODULE_NAME}.property_from_data", return_value=(prop_before, schemas_before) ) result = Endpoint.parse_multipart_body(body=body, schemas=schemas_before, parent_name="parent", config=config) property_from_data.assert_called_once_with( name="multipart_data", required=True, data=schema, schemas=schemas_before, parent_name="parent", config=config, ) prop_after = model_property_factory(class_info=class_info, is_multipart_body=True) schemas_after = Schemas(classes_by_name={class_info.name: prop_after}) assert result == (prop_after, schemas_after)
def test_property_from_data_ref_model(self, model_property_factory): from openapi_python_client.parser.properties import Class, ModelProperty, Schemas, property_from_data name = "new_name" required = False class_name = "MyModel" data = oai.Reference.construct( ref=f"#/components/schemas/{class_name}") class_info = Class(name=class_name, module_name="my_model") existing_model = model_property_factory( name="old_name", class_info=class_info, ) schemas = Schemas(classes_by_reference={ f"/components/schemas/{class_name}": existing_model }) prop, new_schemas = property_from_data(name=name, required=required, data=data, schemas=schemas, parent_name="", config=Config()) assert prop == model_property_factory( name=name, required=required, class_info=class_info, ) assert schemas == new_schemas
def test_happy_path(self, model_property_factory, string_property_factory, date_time_property_factory): from openapi_python_client.parser.properties import Class, Schemas, build_model_property name = "prop" nullable = False required = True data = oai.Schema.construct( required=["req"], title="MyModel", properties={ "req": oai.Schema.construct(type="string"), "opt": oai.Schema(type="string", format="date-time"), }, description="A class called MyModel", nullable=nullable, ) schemas = Schemas(classes_by_reference={"OtherModel": None}, classes_by_name={"OtherModel": None}) model, new_schemas = build_model_property(data=data, name=name, schemas=schemas, required=required, parent_name="parent", config=Config()) assert new_schemas != schemas assert new_schemas.classes_by_name == { "OtherModel": None, "ParentMyModel": model, } assert new_schemas.classes_by_reference == { "OtherModel": None, } assert model == model_property_factory( name=name, required=required, nullable=nullable, class_info=Class(name="ParentMyModel", module_name="parent_my_model"), required_properties=[ string_property_factory(name="req", required=True) ], optional_properties=[ date_time_property_factory(name="opt", required=False) ], description=data.description, relative_imports={ "from dateutil.parser import isoparse", "from typing import cast", "import datetime", "from ..types import UNSET, Unset", "from typing import Union", }, additional_properties=True, )
def test_class_from_string_default_config(): from openapi_python_client import Config from openapi_python_client.parser.properties import Class class_ = Class.from_string(string="#/components/schemas/PingResponse", config=Config()) assert class_.name == "PingResponse" assert class_.module_name == "ping_response"
def _factory(**kwargs): kwargs = _common_kwargs(kwargs) kwargs = { "class_info": Class(name="", module_name=""), "values": {}, "value_type": str, **kwargs, } return EnumProperty(**kwargs)
def _factory(**kwargs): kwargs = { "name": "test", "required": True, "nullable": False, "default": None, "class_info": Class(name="", module_name=""), "values": {}, "value_type": str, **kwargs, } return EnumProperty(**kwargs)
def _factory(**kwargs): kwargs = _common_kwargs(kwargs) kwargs = { "description": "", "class_info": Class(name="MyClass", module_name="my_module"), "required_properties": [], "optional_properties": [], "relative_imports": set(), "additional_properties": False, "python_name": "", **kwargs, } return ModelProperty(**kwargs)
def test_add_body_happy(self, mocker): from openapi_python_client.parser.openapi import Class, Endpoint from openapi_python_client.parser.properties import Property request_body = mocker.MagicMock() config = mocker.MagicMock() form_body_class = Class(name="A", module_name="a") parse_request_form_body = mocker.patch.object(Endpoint, "parse_request_form_body", return_value=form_body_class) multipart_body = mocker.MagicMock(autospec=Property) multipart_body_imports = mocker.MagicMock() multipart_body.get_imports.return_value = {multipart_body_imports} multipart_schemas = mocker.MagicMock() parse_multipart_body = mocker.patch.object( Endpoint, "parse_multipart_body", return_value=(multipart_body, multipart_schemas) ) json_body = mocker.MagicMock(autospec=Property) json_body_imports = mocker.MagicMock() json_body.get_imports.return_value = {json_body_imports} json_schemas = mocker.MagicMock() parse_request_json_body = mocker.patch.object( Endpoint, "parse_request_json_body", return_value=(json_body, json_schemas) ) import_string_from_class = mocker.patch(f"{MODULE_NAME}.import_string_from_class", return_value="import_1") endpoint = self.make_endpoint() initial_schemas = mocker.MagicMock() (endpoint, response_schemas) = Endpoint._add_body( endpoint=endpoint, data=oai.Operation.construct(requestBody=request_body), schemas=initial_schemas, config=config, ) assert response_schemas == multipart_schemas parse_request_form_body.assert_called_once_with(body=request_body, config=config) parse_request_json_body.assert_called_once_with( body=request_body, schemas=initial_schemas, parent_name="name", config=config ) parse_multipart_body.assert_called_once_with( body=request_body, schemas=json_schemas, parent_name="name", config=config ) import_string_from_class.assert_called_once_with(form_body_class, prefix="...models") json_body.get_imports.assert_called_once_with(prefix="...") multipart_body.get_imports.assert_called_once_with(prefix="...") assert endpoint.relative_imports == {"import_1", "import_3", json_body_imports, multipart_body_imports} assert endpoint.json_body == json_body assert endpoint.form_body_class == form_body_class assert endpoint.multipart_body == multipart_body
def test_property_from_data_ref_model(self): from openapi_python_client.parser.properties import Class, ModelProperty, Schemas, property_from_data name = "new_name" required = False class_name = "MyModel" data = oai.Reference.construct(ref=f"#/components/schemas/{class_name}") existing_model = ModelProperty( name="old_name", required=True, nullable=False, default=None, class_info=Class(name=class_name, module_name="my_model"), required_properties=[], optional_properties=[], description="", relative_imports=set(), additional_properties=False, ) schemas = Schemas(classes_by_reference={f"/components/schemas/{class_name}": existing_model}) prop, new_schemas = property_from_data( name=name, required=required, data=data, schemas=schemas, parent_name="", config=Config() ) assert prop == ModelProperty( name=name, required=required, nullable=False, default=None, class_info=Class(name=class_name, module_name="my_model"), required_properties=[], optional_properties=[], description="", relative_imports=set(), additional_properties=False, ) assert schemas == new_schemas
def _factory(**kwargs): kwargs = { "name": "", "description": "", "required": True, "nullable": True, "default": None, "class_info": Class(name="", module_name=""), "required_properties": [], "optional_properties": [], "relative_imports": set(), "additional_properties": False, **kwargs, } return ModelProperty(**kwargs)
def test_class_from_string(class_override, module_override, expected_class, expected_module): from openapi_python_client.config import ClassOverride, Config from openapi_python_client.parser.properties import Class ref = "#/components/schemas/MyResponse" config = Config( class_overrides={ "MyResponse": ClassOverride(class_name=class_override, module_name=module_override) }) result = Class.from_string(string=ref, config=config) assert result.name == expected_class assert result.module_name == expected_module
def get_class(): from openapi_python_client.parser.properties import Class return Class(name="MyClass", module_name="my_module")