def test_property_from_dict_simple_types(self, mocker, openapi_type,
                                             python_type):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "type": openapi_type,
        }
        clazz = mocker.patch(f"{MODULE_NAME}.{python_type}")

        from openapi_python_client.openapi_parser.properties import property_from_dict

        p = property_from_dict(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_dict(
            name=name,
            required=required,
            data=data,
        )
        clazz.assert_called_once_with(name=name,
                                      required=required,
                                      default=data["default"])
    def test_property_from_dict_unsupported_type(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "type": mocker.MagicMock(),
        }

        from openapi_python_client.openapi_parser.errors import ParseError
        from openapi_python_client.openapi_parser.properties import property_from_dict

        with pytest.raises(ParseError):
            property_from_dict(name=name, required=required, data=data)
    def test_property_from_dict_array(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "type": "array",
            "items": {
                "type": "number",
                "default": "0.0"
            },
        }
        ListProperty = mocker.patch(f"{MODULE_NAME}.ListProperty")
        FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")

        from openapi_python_client.openapi_parser.properties import property_from_dict

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

        FloatProperty.assert_called_once_with(name=f"{name}_item",
                                              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
    def test_property_from_dict_enum(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "enum": mocker.MagicMock(),
        }
        EnumProperty = mocker.patch(f"{MODULE_NAME}.EnumProperty")

        from openapi_python_client.openapi_parser.properties import property_from_dict

        p = property_from_dict(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=None,
            title=name)
        assert p == EnumProperty()

        EnumProperty.reset_mock()
        title = mocker.MagicMock()
        data["default"] = mocker.MagicMock()
        data["title"] = title

        property_from_dict(
            name=name,
            required=required,
            data=data,
        )
        EnumProperty.assert_called_once_with(
            name=name,
            required=required,
            values=EnumProperty.values_from_list(),
            default=data["default"],
            title=title)
    def test_property_from_dict_string(self, mocker):
        _string_based_property = mocker.patch(
            f"{MODULE_NAME}._string_based_property")
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "type": "string",
        }
        from openapi_python_client.openapi_parser.properties import property_from_dict

        p = property_from_dict(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)
    def test_property_from_dict_ref(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "$ref": mocker.MagicMock(),
        }
        from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")
        RefProperty = mocker.patch(f"{MODULE_NAME}.RefProperty")

        from openapi_python_client.openapi_parser.properties import property_from_dict

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

        from_ref.assert_called_once_with(data["$ref"])
        RefProperty.assert_called_once_with(name=name,
                                            required=required,
                                            reference=from_ref(),
                                            default=None)
        assert p == RefProperty()
    def test_property_from_dict_union(self, mocker):
        name = mocker.MagicMock()
        required = mocker.MagicMock()
        data = {
            "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")

        from openapi_python_client.openapi_parser.properties import property_from_dict

        p = property_from_dict(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
    def test_property_from_dict_no_valid_props_in_data(self):
        from openapi_python_client.openapi_parser.errors import ParseError
        from openapi_python_client.openapi_parser.properties import property_from_dict

        with pytest.raises(ParseError):
            property_from_dict(name="blah", required=True, data={})