def test_to_string(self, mocker, default, required, expected):
        from openapi_python_client.parser.properties import Property

        name = "test"
        mocker.patch.object(Property, "_type_string", "TestType")
        p = Property(name=name, required=required, default=default, nullable=False)
        assert p.to_string() == expected
    def test_get_type_string(self, mocker, nullable, required, no_optional, json, expected):
        from openapi_python_client.parser.properties import Property

        mocker.patch.object(Property, "_type_string", "TestType")
        mocker.patch.object(Property, "_json_type_string", "str")
        p = Property(name="test", required=required, default=None, nullable=nullable)
        assert p.get_type_string(no_optional=no_optional, json=json) == expected
示例#3
0
    def test_get_imports(self, mocker):
        from openapi_python_client.parser.properties import Property

        name = mocker.MagicMock()
        mocker.patch("openapi_python_client.utils.snake_case")
        p = Property(name=name, required=True, default=None)
        assert p.get_imports(prefix="") == set()

        p.required = False
        assert p.get_imports(prefix="") == {"from typing import Optional"}
示例#4
0
    def test___post_init(self, mocker):
        from openapi_python_client.parser.properties import Property

        validate_default = mocker.patch(f"{MODULE_NAME}.Property._validate_default")

        Property(name="a name", required=True, default=None, nullable=False)
        validate_default.assert_not_called()

        Property(name="a name", required=True, default="the default value", nullable=False)
        validate_default.assert_called_with(default="the default value")
示例#5
0
    def test__validate_default(self):
        from openapi_python_client.parser.properties import Property

        # should be okay if default isn't specified
        p = Property(name="a name", required=True, default=None)

        with pytest.raises(ValidationError):
            p._validate_default("a default value")

        with pytest.raises(ValidationError):
            Property(name="a name", required=True, default="")
示例#6
0
    def test_get_type_string(self):
        from openapi_python_client.parser.properties import Property

        p = Property(name="test", required=True, default=None, nullable=False)
        p._type_string = "TestType"

        assert p.get_type_string() == "TestType"
        p.required = False
        assert p.get_type_string() == "Optional[TestType]"
        assert p.get_type_string(True) == "TestType"

        p.required = False
        p.nullable = True
        assert p.get_type_string() == "Optional[TestType]"
    def test_get_imports(self):
        from openapi_python_client.parser.properties import Property

        p = Property(name="test", required=True, default=None, nullable=False)
        assert p.get_imports(prefix="") == set()

        p = Property(name="test", required=False, default=None, nullable=False)
        assert p.get_imports(prefix="") == {"from types import UNSET, Unset", "from typing import Union"}

        p = Property(name="test", required=False, default=None, nullable=True)
        assert p.get_imports(prefix="") == {
            "from types import UNSET, Unset",
            "from typing import Optional",
            "from typing import Union",
        }
示例#8
0
    def test_to_string(self, mocker):
        from openapi_python_client.parser.properties import Property

        name = mocker.MagicMock()
        snake_case = mocker.patch("openapi_python_client.utils.snake_case")
        p = Property(name=name, required=True, default=None, nullable=False)
        get_type_string = mocker.patch.object(p, "get_type_string")

        assert p.to_string() == f"{snake_case(name)}: {get_type_string()}"
        p.required = False
        assert p.to_string() == f"{snake_case(name)}: {get_type_string()} = None"

        p.default = "TEST"
        assert p.to_string() == f"{snake_case(name)}: {get_type_string()} = TEST"
示例#9
0
    def test_to_string(self, mocker):
        from openapi_python_client.parser.properties import Property

        name = "test"
        get_type_string = mocker.patch.object(Property, "get_type_string")
        p = Property(name=name, required=True, default=None, nullable=False)

        assert p.to_string() == f"{name}: {get_type_string()}"

        p = Property(name=name, required=False, default=None, nullable=False)
        assert p.to_string() == f"{name}: {get_type_string()} = UNSET"

        p = Property(name=name, required=True, default=None, nullable=False)
        assert p.to_string() == f"{name}: {get_type_string()}"

        p = Property(name=name, required=True, default="TEST", nullable=False)
        assert p.to_string() == f"{name}: {get_type_string()} = TEST"
示例#10
0
    def test_get_type_string(self, mocker):
        from openapi_python_client.parser.properties import Property

        mocker.patch.object(Property, "_type_string", "TestType")
        p = Property(name="test", required=True, default=None, nullable=False)

        base_type_string = f"TestType"

        assert p.get_type_string() == base_type_string

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

        p = Property(name="test", required=False, default=None, nullable=True)
        assert p.get_type_string() == f"Union[Unset, Optional[{base_type_string}]]"
        assert p.get_type_string(no_optional=True) == base_type_string

        p = Property(name="test", required=False, default=None, nullable=False)
        assert p.get_type_string() == f"Union[Unset, {base_type_string}]"
        assert p.get_type_string(no_optional=True) == base_type_string
示例#11
0
 def _factory(**kwargs):
     kwargs = _common_kwargs(kwargs)
     return Property(**kwargs)