Beispiel #1
0
    def test__validate_default(self):
        from openapi_python_client.parser.properties import DateProperty

        with pytest.raises(ValidationError):
            DateProperty(name="a name", required=True, default="not a date")

        p = DateProperty(name="a name", required=True, default="1010-10-10")
        assert p.default == "datetime.date(1010, 10, 10)"
    def test_get_imports(self):
        from openapi_python_client.parser.properties import DateProperty

        p = DateProperty(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 = DateProperty(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 = DateProperty(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",
        }
Beispiel #3
0
    def test_get_imports(self, mocker):
        from openapi_python_client.parser.properties import DateProperty

        name = mocker.MagicMock()
        mocker.patch("openapi_python_client.utils.snake_case")
        p = DateProperty(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 date_property(required=True, nullable=True, default=None) -> DateProperty:
    return DateProperty(
        name="a_prop",
        required=required,
        nullable=nullable,
        default=default,
        python_name="a_prop",
    )
Beispiel #5
0
    def test__string_based_property_date_format(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema.construct(type="string", schema_format="date")
        DateProperty = mocker.patch(f"{MODULE_NAME}.DateProperty")

        from openapi_python_client.parser.properties import _string_based_property

        p = _string_based_property(name=name, required=required, data=data)
        DateProperty.assert_called_once_with(name=name,
                                             required=required,
                                             default=None)
        assert p == DateProperty.return_value

        # Test optional values
        DateProperty.reset_mock()
        data.default = mocker.MagicMock()

        _string_based_property(
            name=name,
            required=required,
            data=data,
        )
        DateProperty.assert_called_once_with(name=name,
                                             required=required,
                                             default=data.default)
Beispiel #6
0
    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__string_based_property_date_format(self):
        from openapi_python_client.parser.properties import DateProperty, _string_based_property

        name = "date_prop"
        required = True
        data = oai.Schema.construct(type="string", schema_format="date", nullable=True, default="2020-11-06")

        p = _string_based_property(name=name, required=required, data=data)

        assert p == DateProperty(name=name, required=required, nullable=True, default="isoparse('2020-11-06').date()")

        # Test bad default
        data.default = "a"
        with pytest.raises(ValidationError):
            _string_based_property(name=name, required=required, data=data)
def test_optional_nullable():
    from openapi_python_client.parser.properties import DateProperty

    prop = DateProperty(
        name="a_prop",
        required=False,
        nullable=True,
        default=None,
    )
    here = Path(__file__).parent
    templates_dir = here.parent.parent.parent.parent / "openapi_python_client" / "templates"

    env = jinja2.Environment(loader=jinja2.ChoiceLoader([
        jinja2.FileSystemLoader(here),
        jinja2.FileSystemLoader(templates_dir)
    ]))

    template = env.get_template("date_property_template.py")
    content = template.render(property=prop)
    expected = here / "optional_nullable.py"
    assert content == expected.read_text()
 def _factory(**kwargs):
     kwargs = _common_kwargs(kwargs)
     return DateProperty(**kwargs)