def test_property_from_data_ref_enum(self):
        from openapi_python_client.parser.properties import Class, EnumProperty, Schemas, property_from_data

        name = "some_enum"
        data = oai.Reference.construct(ref="#/components/schemas/MyEnum")
        existing_enum = EnumProperty(
            name="an_enum",
            required=True,
            nullable=False,
            default=None,
            values={"A": "a"},
            value_type=str,
            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 == EnumProperty(
            name="some_enum",
            required=False,
            nullable=False,
            default=None,
            values={"A": "a"},
            value_type=str,
            class_info=Class(name="MyEnum", module_name="my_enum"),
        )
        assert schemas == new_schemas
    def test_values_from_list_duplicate(self):
        from openapi_python_client.parser.properties import EnumProperty

        data = ["abc", "123", "a23", "abc"]

        with pytest.raises(ValueError):
            EnumProperty.values_from_list(data)
示例#3
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)
    def test_property_from_data_int_enum(self, mocker):
        from openapi_python_client.parser.properties import Class, EnumProperty
        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(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={"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": schemas.classes_by_name["AnEnum"],
            "ParentAnEnum": prop,
        }
示例#5
0
    def test_property_from_data_str_enum(self, mocker):
        from openapi_python_client.parser.properties import EnumProperty, Reference
        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(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={"A": "A", "B": "B", "C": "C"},
            reference=Reference(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.enums == {
            "AnEnum": schemas.enums["AnEnum"],
            "ParentAnEnum": prop,
        }
 def _factory(**kwargs):
     kwargs = _common_kwargs(kwargs)
     kwargs = {
         "class_info": Class(name="", module_name=""),
         "values": {},
         "value_type": str,
         **kwargs,
     }
     return EnumProperty(**kwargs)
 def _factory(**kwargs):
     kwargs = {
         "name": "test",
         "required": True,
         "nullable": False,
         "default": None,
         "class_info": Class(name="", module_name=""),
         "values": {},
         "value_type": str,
         **kwargs,
     }
     return EnumProperty(**kwargs)
示例#8
0
    def test_values_from_list(self):
        from openapi_python_client.parser.properties import EnumProperty

        data = ["abc", "123", "a23", "1bc"]

        result = EnumProperty.values_from_list(data)

        assert result == {
            "ABC": "abc",
            "VALUE_1": "123",
            "A23": "a23",
            "VALUE_3": "1bc",
        }
示例#9
0
    def test_values_from_list(self):
        from openapi_python_client.parser.properties import EnumProperty

        data = ["abc", "123", "a23", "1bc", 4, -3, "a Thing WIth spaces"]

        result = EnumProperty.values_from_list(data)

        assert result == {
            "ABC": "abc",
            "VALUE_1": "123",
            "A23": "a23",
            "VALUE_3": "1bc",
            "VALUE_4": 4,
            "VALUE_NEGATIVE_3": -3,
            "A_THING_WITH_SPACES": "a Thing WIth spaces",
        }