コード例 #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])
コード例 #2
0
    def test_property_from_data_union(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(
            anyOf=[{"type": "number", "default": "0.0"}],
            oneOf=[
                {"type": "integer", "default": "0"},
            ],
        )
        UnionProperty = mocker.patch(f"{MODULE_NAME}.UnionProperty")
        FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")
        IntProperty = mocker.patch(f"{MODULE_NAME}.IntProperty")
        mocker.patch("openapi_python_client.utils.remove_string_escapes", return_value=name)

        from openapi_python_client.parser.properties import Schemas, property_from_data

        p, s = property_from_data(
            name=name, required=required, data=data, schemas=Schemas(), parent_name="parent", config=MagicMock()
        )

        FloatProperty.assert_called_once_with(name=name, required=required, default=0.0, nullable=False)
        IntProperty.assert_called_once_with(name=name, required=required, default=0, nullable=False)
        UnionProperty.assert_called_once_with(
            name=name,
            required=required,
            default=None,
            inner_properties=[FloatProperty.return_value, IntProperty.return_value],
            nullable=False,
        )
        assert p == UnionProperty.return_value
        assert s == Schemas()
コード例 #3
0
    def test_get_type_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 = mocker.MagicMock()
        p = UnionProperty(
            name="test",
            required=True,
            default=None,
            inner_properties=[inner_property_1, inner_property_2])

        assert p.get_imports(prefix=prefix) == {
            inner_import_1,
            inner_import_2,
            "from typing import Union",
        }
        p.required = False
        assert p.get_imports(prefix=prefix) == {
            inner_import_1,
            inner_import_2,
            "from typing import Union",
            "from typing import Optional",
        }
コード例 #4
0
    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"
コード例 #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]]"
コード例 #6
0
    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
コード例 #7
0
    def test_property_from_data_union(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(anyOf=[
            {
                "type": "number",
                "default": "0.0"
            },
            {
                "type": "integer",
                "default": "0"
            },
        ])
        UnionProperty = mocker.patch(f"{MODULE_NAME}.UnionProperty")
        FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")
        IntProperty = mocker.patch(f"{MODULE_NAME}.IntProperty")
        mocker.patch("openapi_python_client.utils.remove_string_escapes",
                     return_value=name)

        from openapi_python_client.parser.properties import property_from_data

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

        FloatProperty.assert_called_once_with(name=name,
                                              required=required,
                                              default="0.0")
        IntProperty.assert_called_once_with(name=name,
                                            required=required,
                                            default="0")
        UnionProperty.assert_called_once_with(
            name=name,
            required=required,
            default=None,
            inner_properties=[
                FloatProperty.return_value, IntProperty.return_value
            ],
        )
        assert p == UnionProperty.return_value
コード例 #8
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],
            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
コード例 #9
0
 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)