예제 #1
0
    def test_property_from_data_enum(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = mocker.MagicMock(title=None)
        EnumProperty = mocker.patch(f"{MODULE_NAME}.EnumProperty")
        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)

        EnumProperty.values_from_list.assert_called_once_with(data.enum)
        EnumProperty.assert_called_once_with(
            name=name,
            required=required,
            values=EnumProperty.values_from_list(),
            default=data.default,
            title=name)
        assert p == EnumProperty()

        EnumProperty.reset_mock()
        data.title = mocker.MagicMock()

        property_from_data(
            name=name,
            required=required,
            data=data,
        )
        EnumProperty.assert_called_once_with(
            name=name,
            required=required,
            values=EnumProperty.values_from_list(),
            default=data.default,
            title=data.title)
예제 #2
0
    def test_property_from_data_simple_types(self, openapi_type, prop_type, python_type):
        from openapi_python_client.parser.properties import Schemas, property_from_data

        name = "test_prop"
        required = True
        data = oai.Schema.construct(type=openapi_type, default=1)
        schemas = Schemas()

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

        assert p == prop_type(name=name, required=required, default=python_type(data.default), nullable=False)
        assert new_schemas == schemas

        # Test nullable values
        data.default = 0
        data.nullable = True

        p, _ = property_from_data(
            name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=MagicMock()
        )
        assert p == prop_type(name=name, required=required, default=python_type(data.default), nullable=True)

        # Test bad default value
        data.default = "a"
        p, _ = property_from_data(
            name=name, required=required, data=data, schemas=schemas, parent_name="parent", config=MagicMock()
        )
        assert python_type is bool or isinstance(p, PropertyError)
예제 #3
0
    def test_property_from_data_simple_types(self, mocker, openapi_type,
                                             python_type):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema.construct(type=openapi_type)
        clazz = mocker.patch(f"{MODULE_NAME}.{python_type}")
        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)

        clazz.assert_called_once_with(name=name,
                                      required=required,
                                      default=None)
        assert p == clazz()

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

        property_from_data(
            name=name,
            required=required,
            data=data,
        )
        clazz.assert_called_once_with(name=name,
                                      required=required,
                                      default=data.default)
예제 #4
0
    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,
        }
예제 #5
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()
예제 #6
0
    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,
        }
예제 #7
0
    def test_property_from_data_union_of_one_element(self, mocker,
                                                     model_property_factory):
        from openapi_python_client.parser.properties import Class, ModelProperty, Schemas, property_from_data

        name = "new_name"
        required = False
        class_name = "MyModel"
        nullable = True
        existing_model = model_property_factory()
        schemas = Schemas(
            classes_by_reference={f"/{class_name}": existing_model})

        data = oai.Schema.construct(
            allOf=[oai.Reference.construct(ref=f"#/{class_name}")],
            nullable=nullable,
        )
        build_union_property = mocker.patch(
            f"{MODULE_NAME}.build_union_property")

        prop, schemas = property_from_data(name=name,
                                           required=required,
                                           data=data,
                                           schemas=schemas,
                                           parent_name="parent",
                                           config=Config())

        assert prop == attr.evolve(existing_model,
                                   name=name,
                                   required=required,
                                   nullable=nullable,
                                   python_name=name)
        build_union_property.assert_not_called()
예제 #8
0
    def test_property_from_data_union(
        self, union_property_factory, date_time_property_factory, string_property_factory
    ):
        from openapi_python_client.parser.properties import Schemas, property_from_data

        name = "union_prop"
        required = True
        data = oai.Schema(
            anyOf=[{"type": "string", "default": "a"}],
            oneOf=[
                {"type": "string", "format": "date-time"},
            ],
        )
        expected = union_property_factory(
            name=name,
            required=required,
            inner_properties=[
                string_property_factory(name=f"{name}_type_0", default="'a'"),
                date_time_property_factory(name=f"{name}_type_1"),
            ],
        )

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

        assert p == expected
        assert s == Schemas()
예제 #9
0
    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
예제 #10
0
    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
예제 #11
0
    def test_property_from_data_object(self, mocker):
        from openapi_python_client.parser.properties import Schemas, property_from_data

        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(type="object", )
        build_model_property = mocker.patch(
            f"{MODULE_NAME}.build_model_property")
        mocker.patch("openapi_python_client.utils.remove_string_escapes",
                     return_value=name)
        schemas = Schemas()
        config = MagicMock()

        response = property_from_data(name=name,
                                      required=required,
                                      data=data,
                                      schemas=schemas,
                                      parent_name="parent",
                                      config=config)

        assert response == build_model_property.return_value
        build_model_property.assert_called_once_with(data=data,
                                                     name=name,
                                                     required=required,
                                                     schemas=schemas,
                                                     parent_name="parent",
                                                     config=config)
예제 #12
0
    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,
        }
예제 #13
0
    def test_property_from_data_ref_enum(self):
        from openapi_python_client.parser.properties import EnumProperty, Reference, Schemas, property_from_data

        name = "some_enum"
        data = oai.Reference.construct(ref="MyEnum")
        existing_enum = EnumProperty(
            name="an_enum",
            required=True,
            nullable=False,
            default=None,
            values={"A": "a"},
            value_type=str,
            reference=Reference(class_name="MyEnum", module_name="my_enum"),
        )
        schemas = Schemas(enums={"MyEnum": existing_enum})

        prop, new_schemas = property_from_data(name=name, required=False, data=data, schemas=schemas, parent_name="")

        assert prop == EnumProperty(
            name="some_enum",
            required=False,
            nullable=False,
            default=None,
            values={"A": "a"},
            value_type=str,
            reference=Reference(class_name="MyEnum", module_name="my_enum"),
        )
        assert schemas == new_schemas
예제 #14
0
    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")
예제 #15
0
    def test_property_from_data_array(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(
            type="array",
            items={
                "type": "number",
                "default": "0.0"
            },
        )
        ListProperty = mocker.patch(f"{MODULE_NAME}.ListProperty")
        FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")
        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=True,
                                              default="0.0")
        ListProperty.assert_called_once_with(
            name=name,
            required=required,
            default=None,
            inner_property=FloatProperty.return_value)
        assert p == ListProperty.return_value
예제 #16
0
    def test_property_from_data_int_enum(self, mocker):
        from openapi_python_client.parser.properties import EnumProperty, Reference
        from openapi_python_client.schema import Schema

        data = Schema.construct(title="anEnum", enum=[1, 2, 3], nullable=False, default=3)
        name = "my_enum"
        required = True

        from openapi_python_client.parser.properties import Schemas, property_from_data

        schemas = Schemas(enums={"AnEnum": mocker.MagicMock()})

        prop, new_schemas = property_from_data(
            name=name, required=required, data=data, schemas=schemas, parent_name="parent"
        )

        assert prop == EnumProperty(
            name="my_enum",
            required=True,
            nullable=False,
            values={"VALUE_1": 1, "VALUE_2": 2, "VALUE_3": 3},
            reference=Reference(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.enums == {
            "AnEnum": schemas.enums["AnEnum"],
            "ParentAnEnum": prop,
        }
예제 #17
0
    def test_property_from_data_no_valid_props_in_data(self):
        from openapi_python_client.parser.errors import PropertyError
        from openapi_python_client.parser.properties import property_from_data

        data = oai.Schema()
        assert property_from_data(name="blah", required=True, data=data) == PropertyError(
            data=data, detail="Schemas must either have one of enum, anyOf, or type defined."
        )
예제 #18
0
    def test_property_from_data_validation_error(self, mocker):
        from openapi_python_client.parser.errors import PropertyError
        from openapi_python_client.parser.properties import property_from_data

        mocker.patch(f"{MODULE_NAME}._property_from_data").side_effect = ValidationError()

        data = oai.Schema()
        assert property_from_data(name="blah", required=True, data=data) == PropertyError(
            detail="Failed to validate default value", data=data
        )
예제 #19
0
    def test_property_from_data_array_no_items(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(type="array")

        from openapi_python_client.parser.properties import property_from_data

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

        assert p == PropertyError(data=data, detail="type array must have items defined")
예제 #20
0
    def test_property_from_data_unsupported_type(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema.construct(type=mocker.MagicMock())

        from openapi_python_client.parser.errors import PropertyError
        from openapi_python_client.parser.properties import property_from_data

        assert property_from_data(name=name, required=required, data=data) == PropertyError(
            data=data, detail=f"unknown type {data.type}"
        )
예제 #21
0
    def test_property_from_data_array_invalid_items(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(type="array", items={},)
        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)

        assert p == PropertyError(data=oai.Schema(), detail=f"invalid data in items of array {name}")
예제 #22
0
    def test_property_from_data_union_bad_type(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema(anyOf=[{}])
        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)

        assert p == PropertyError(detail=f"Invalid property in union {name}", data=oai.Schema())
예제 #23
0
    def test_property_from_data_union_bad_type(self, mocker):
        name = "bad_union"
        required = mocker.MagicMock()
        data = oai.Schema(anyOf=[{"type": "garbage"}])
        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")

        assert p == PropertyError(detail=f"Invalid property in union {name}", data=oai.Schema(type="garbage"))
예제 #24
0
    def test_property_from_data_no_valid_props_in_data(self):
        from openapi_python_client.parser.properties import NoneProperty, Schemas, property_from_data

        schemas = Schemas()
        data = oai.Schema()

        prop, new_schemas = property_from_data(
            name="blah", required=True, data=data, schemas=schemas, parent_name="parent", config=MagicMock()
        )

        assert prop == NoneProperty(name="blah", required=True, nullable=False, default=None)
        assert new_schemas == schemas
예제 #25
0
    def test__string_based_property_binary_format(self, file_property_factory):
        from openapi_python_client.parser.properties import property_from_data

        name = "file_prop"
        required = True
        nullable = True
        data = oai.Schema.construct(type="string", schema_format="binary", nullable=nullable, default="a")

        p, _ = property_from_data(
            name=name, required=required, data=data, schemas=Schemas(), config=Config(), parent_name=None
        )
        assert p == file_property_factory(name=name, required=required, nullable=nullable)
예제 #26
0
    def test_datetime_bad_default(self):
        from openapi_python_client.parser.properties import property_from_data

        name = "datetime_prop"
        required = True
        data = oai.Schema.construct(type="string", schema_format="date-time", nullable=True, default="a")

        result, _ = property_from_data(
            name=name, required=required, data=data, schemas=Schemas(), config=Config(), parent_name=None
        )

        assert result == PropertyError(detail="Failed to validate default value", data=data)
예제 #27
0
    def test_property_from_data_validation_error(self, mocker):
        from openapi_python_client.parser.errors import PropertyError
        from openapi_python_client.parser.properties import Schemas, property_from_data

        mocker.patch(f"{MODULE_NAME}._property_from_data").side_effect = ValidationError()
        schemas = Schemas()

        data = oai.Schema()
        err, new_schemas = property_from_data(
            name="blah", required=True, data=data, schemas=schemas, parent_name="parent", config=MagicMock()
        )
        assert err == PropertyError(detail="Failed to validate default value", data=data)
        assert new_schemas == schemas
예제 #28
0
    def test__string_based_property_unsupported_format(self, string_property_factory):
        from openapi_python_client.parser.properties import property_from_data

        name = "unknown"
        required = True
        nullable = True
        data = oai.Schema.construct(type="string", schema_format="blah", nullable=nullable)

        p, _ = property_from_data(
            name=name, required=required, data=data, schemas=Schemas, config=Config(), parent_name=None
        )

        assert p == string_property_factory(name=name, required=required, nullable=nullable)
예제 #29
0
    def test_no_format(self, string_property_factory, nullable, required):
        from openapi_python_client.parser.properties import property_from_data

        name = "some_prop"
        data = oai.Schema.construct(type="string", nullable=nullable, default='"hello world"', pattern="abcdef")

        p, _ = property_from_data(
            name=name, required=required, data=data, parent_name=None, config=Config(), schemas=Schemas()
        )

        assert p == string_property_factory(
            name=name, required=required, nullable=nullable, default="'\\\\\"hello world\\\\\"'", pattern=data.pattern
        )
예제 #30
0
    def test_property_from_data_string(self, mocker):
        _string_based_property = mocker.patch(f"{MODULE_NAME}._string_based_property")
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = oai.Schema.construct(type="string")
        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)

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