def test__validate_default(self): from openapi_python_client.parser.properties import DateTimeProperty with pytest.raises(ValidationError): DateTimeProperty(name="a name", required=True, default="not a datetime", nullable=False) p = DateTimeProperty(name="a name", required=True, default="2017-07-21T17:32:28Z", nullable=False) assert p.default == "datetime.datetime(2017, 7, 21, 17, 32, 28, tzinfo=datetime.timezone.utc)"
def test_get_imports(self): from openapi_python_client.parser.properties import DateTimeProperty p = DateTimeProperty(name="test", required=True, default=None, nullable=False) assert p.get_imports(prefix="...") == { "import datetime", "from typing import cast", "from dateutil.parser import isoparse", } p = DateTimeProperty(name="test", required=False, default=None, nullable=False) assert p.get_imports(prefix="...") == { "import datetime", "from typing import cast", "from dateutil.parser import isoparse", "from typing import Union", "from ...types import UNSET, Unset", } p = DateTimeProperty(name="test", required=False, default=None, nullable=True) assert p.get_imports(prefix="...") == { "import datetime", "from typing import cast", "from dateutil.parser import isoparse", "from typing import Union", "from typing import Optional", "from ...types import UNSET, Unset", }
def test_get_imports(self, mocker): from openapi_python_client.parser.properties import DateTimeProperty name = mocker.MagicMock() mocker.patch("openapi_python_client.utils.snake_case") p = DateTimeProperty(name=name, required=True, default=None) assert p.get_imports(prefix="") == { "import datetime", "from typing import cast", } p.required = False assert p.get_imports(prefix="") == { "from typing import Optional", "import datetime", "from typing import cast", }
def test__string_based_property_datetime_format(self, mocker): name = mocker.MagicMock() required = mocker.MagicMock() data = oai.Schema.construct(type="string", schema_format="date-time") DateTimeProperty = mocker.patch(f"{MODULE_NAME}.DateTimeProperty") from openapi_python_client.parser.properties import _string_based_property p = _string_based_property(name=name, required=required, data=data) DateTimeProperty.assert_called_once_with(name=name, required=required, default=None) assert p == DateTimeProperty.return_value # Test optional values DateTimeProperty.reset_mock() data.default = mocker.MagicMock() _string_based_property( name=name, required=required, data=data, ) DateTimeProperty.assert_called_once_with(name=name, required=required, default=data.default)
def test__add_responses(self, mocker): from openapi_python_client.parser.openapi import Endpoint, Response from openapi_python_client.parser.properties import DateProperty, DateTimeProperty response_1_data = mocker.MagicMock() response_2_data = mocker.MagicMock() data = { "200": response_1_data, "404": response_2_data, } endpoint = self.make_endpoint() schemas = mocker.MagicMock() schemas_1 = mocker.MagicMock() schemas_2 = mocker.MagicMock() response_1 = Response( status_code=200, source="source", prop=DateTimeProperty(name="datetime", required=True, nullable=False, default=None), ) response_2 = Response( status_code=404, source="source", prop=DateProperty(name="date", required=True, nullable=False, default=None), ) response_from_data = mocker.patch( f"{MODULE_NAME}.response_from_data", side_effect=[(response_1, schemas_1), (response_2, schemas_2)] ) config = MagicMock() endpoint, response_schemas = Endpoint._add_responses( endpoint=endpoint, data=data, schemas=schemas, config=config ) response_from_data.assert_has_calls( [ mocker.call(status_code=200, data=response_1_data, schemas=schemas, parent_name="name", config=config), mocker.call( status_code=404, data=response_2_data, schemas=schemas_1, parent_name="name", config=config ), ] ) assert endpoint.responses == [response_1, response_2] assert endpoint.relative_imports == { "from dateutil.parser import isoparse", "from typing import cast", "import datetime", "import_3", } assert response_schemas == schemas_2
def test_build_model_property(additional_properties_schema, expected_additional_properties): from openapi_python_client.parser.properties import Schemas, build_model_property 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=False, additionalProperties=additional_properties_schema, ) schemas = Schemas(models={"OtherModel": None}) model, new_schemas = build_model_property( data=data, name="prop", schemas=schemas, required=True, parent_name="parent", ) assert new_schemas != schemas assert new_schemas.models == { "OtherModel": None, "ParentMyModel": model, } assert model == ModelProperty( name="prop", required=True, nullable=False, default=None, reference=Reference(class_name="ParentMyModel", module_name="parent_my_model"), required_properties=[StringProperty(name="req", required=True, nullable=False, default=None)], optional_properties=[DateTimeProperty(name="opt", required=False, nullable=False, default=None)], 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=expected_additional_properties, )
def test__string_based_property_datetime_format(self): from openapi_python_client.parser.properties import DateTimeProperty, _string_based_property name = "datetime_prop" required = True data = oai.Schema.construct( type="string", schema_format="date-time", nullable=True, default="2020-11-06T12:00:00" ) p = _string_based_property(name=name, required=required, data=data) assert p == DateTimeProperty( name=name, required=required, nullable=True, default="isoparse('2020-11-06T12:00:00')" ) # Test bad default data.default = "a" with pytest.raises(ValidationError): _string_based_property(name=name, required=required, data=data)
def test_conflicting_properties_different_types(self, model_property_factory): from openapi_python_client.parser.properties import Schemas from openapi_python_client.parser.properties.model_property import _process_properties data = oai.Schema.construct( allOf=[oai.Reference.construct(ref="#/First"), oai.Reference.construct(ref="#/Second")] ) schemas = Schemas( classes_by_reference={ "/First": model_property_factory( optional_properties=[StringProperty(name="prop", required=True, nullable=True, default=None)] ), "/Second": model_property_factory( optional_properties=[DateTimeProperty(name="prop", required=True, nullable=True, default=None)] ), } ) result = _process_properties(data=data, schemas=schemas, class_name="", config=Config()) assert isinstance(result, PropertyError)
def _factory(**kwargs): kwargs = _common_kwargs(kwargs) return DateTimeProperty(**kwargs)