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

        inner_property_1 = mocker.MagicMock()
        inner_property_1.get_type_string.return_value = "inner_type_string_1"
        inner_property_1._validate_default.side_effect = ValidationError()
        inner_property_2 = mocker.MagicMock()
        inner_property_2.get_type_string.return_value = "inner_type_string_2"
        inner_property_2._validate_default.return_value = "the default value"
        p = UnionProperty(
            name="test",
            required=True,
            default="a value",
            inner_properties=[inner_property_1, inner_property_2])

        assert p.default == "the default value"

        inner_property_2._validate_default.side_effect = ValidationError()

        with pytest.raises(ValidationError):
            UnionProperty(
                name="test",
                required=True,
                default="a value",
                inner_properties=[inner_property_1, inner_property_2])
    def test_get_imports(self, mocker):
        from openapi_python_client.parser.properties import UnionProperty

        inner_property_1 = mocker.MagicMock()
        inner_import_1 = mocker.MagicMock()
        inner_property_1.get_imports.return_value = {inner_import_1}
        inner_property_2 = mocker.MagicMock()
        inner_import_2 = mocker.MagicMock()
        inner_property_2.get_imports.return_value = {inner_import_2}
        prefix = "..."
        p = UnionProperty(
            name="test",
            required=True,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=False,
        )

        assert p.get_imports(prefix=prefix) == {
            inner_import_1,
            inner_import_2,
            "from typing import cast, Union",
        }

        p = UnionProperty(
            name="test",
            required=False,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=False,
        )
        assert p.get_imports(prefix=prefix) == {
            inner_import_1,
            inner_import_2,
            "from typing import Union",
            "from typing import cast, Union",
            "from ...types import UNSET, Unset",
        }

        p = UnionProperty(
            name="test",
            required=False,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=True,
        )
        assert p.get_imports(prefix=prefix) == {
            inner_import_1,
            inner_import_2,
            "from typing import Union",
            "from typing import cast, Union",
            "from typing import Optional",
            "from ...types import UNSET, Unset",
        }
    def test_get_type_string(self, mocker):
        from openapi_python_client.parser.properties import UnionProperty

        inner_property_1 = mocker.MagicMock()
        inner_property_1.get_type_string.return_value = "inner_type_string_1"
        inner_property_2 = mocker.MagicMock()
        inner_property_2.get_type_string.return_value = "inner_type_string_2"
        p = UnionProperty(
            name="test",
            required=True,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=False,
        )

        base_type_string = f"Union[inner_type_string_1, inner_type_string_2]"

        assert p.get_type_string() == base_type_string

        p = UnionProperty(
            name="test",
            required=True,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=True,
        )
        assert p.get_type_string() == f"Optional[{base_type_string}]"
        assert p.get_type_string(no_optional=True) == base_type_string

        base_type_string_with_unset = f"Union[Unset, inner_type_string_1, inner_type_string_2]"
        p = UnionProperty(
            name="test",
            required=False,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=True,
        )
        assert p.get_type_string() == f"Optional[{base_type_string_with_unset}]"
        assert p.get_type_string(no_optional=True) == base_type_string

        p = UnionProperty(
            name="test",
            required=False,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=False,
        )
        assert p.get_type_string() == base_type_string_with_unset
        assert p.get_type_string(no_optional=True) == base_type_string
    def test_get_base_type_string_one_element(self, mocker):
        from openapi_python_client.parser.properties import UnionProperty

        inner_property_1 = mocker.MagicMock()
        inner_property_1.get_type_string.side_effect = (
            lambda no_optional=False, json=False: "inner_json_type_string_1" if json else "inner_type_string_1"
        )
        p = UnionProperty(
            name="test",
            required=False,
            default=None,
            inner_properties=[inner_property_1],
            nullable=True,
        )
        assert p.get_base_type_string() == "inner_type_string_1"
Beispiel #5
0
    def test_get_type_string(self, mocker):
        from openapi_python_client.parser.properties import UnionProperty

        inner_property_1 = mocker.MagicMock()
        inner_property_1.get_type_string.return_value = "inner_type_string_1"
        inner_property_2 = mocker.MagicMock()
        inner_property_2.get_type_string.return_value = "inner_type_string_2"
        p = UnionProperty(
            name="test",
            required=True,
            default=None,
            inner_properties=[inner_property_1, inner_property_2])

        assert p.get_type_string(
        ) == "Union[inner_type_string_1, inner_type_string_2]"
        p.required = False
        assert p.get_type_string(
        ) == "Optional[Union[inner_type_string_1, inner_type_string_2]]"
    def test_get_type_string(self, mocker, nullable, required, no_optional, json, expected):
        from openapi_python_client.parser.properties import UnionProperty

        inner_property_1 = mocker.MagicMock()
        inner_property_1.get_type_string.side_effect = (
            lambda no_optional=False, json=False: "inner_json_type_string_1" if json else "inner_type_string_1"
        )
        inner_property_2 = mocker.MagicMock()
        inner_property_2.get_type_string.side_effect = (
            lambda no_optional=False, json=False: "inner_json_type_string_2" if json else "inner_type_string_2"
        )
        p = UnionProperty(
            name="test",
            required=required,
            default=None,
            inner_properties=[inner_property_1, inner_property_2],
            nullable=nullable,
        )
        assert p.get_type_string(no_optional=no_optional, json=json) == expected
 def _factory(**kwargs):
     kwargs = _common_kwargs(kwargs)
     if "inner_properties" not in kwargs:
         kwargs["inner_properties"] = [date_time_property_factory(), string_property_factory()]
     return UnionProperty(**kwargs)