forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_model_property.py
More file actions
201 lines (178 loc) · 7.41 KB
/
test_model_property.py
File metadata and controls
201 lines (178 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import pytest
@pytest.mark.parametrize(
"no_optional,nullable,required,expected",
[
(False, False, False, "Union[MyClass, Unset]"),
(False, False, True, "MyClass"),
(False, True, False, "Union[Optional[MyClass], Unset]"),
(False, True, True, "Optional[MyClass]"),
(True, False, False, "MyClass"),
(True, False, True, "MyClass"),
(True, True, False, "MyClass"),
(True, True, True, "MyClass"),
],
)
def test_get_type_string(no_optional, nullable, required, expected):
from openapi_python_client.parser.properties import ModelProperty, Reference
prop = ModelProperty(
name="prop",
required=required,
nullable=nullable,
default=None,
reference=Reference(class_name="MyClass", module_name="my_module"),
references=[],
description="",
optional_properties=[],
required_properties=[],
relative_imports=set(),
additional_properties=False,
)
assert prop.get_type_string(no_optional=no_optional) == expected
def test_get_imports():
from openapi_python_client.parser.properties import ModelProperty, Reference
prop = ModelProperty(
name="prop",
required=False,
nullable=True,
default=None,
reference=Reference(class_name="MyClass", module_name="my_module"),
references=[],
description="",
optional_properties=[],
required_properties=[],
relative_imports=set(),
additional_properties=False,
)
assert prop.get_imports(prefix="..") == {
"from typing import Optional",
"from typing import Union",
"from ..types import UNSET, Unset",
"from ..models.my_module import MyClass",
"from typing import Dict",
"from typing import cast",
}
def test_resolve_references(mocker):
import openapi_python_client.schema as oai
from openapi_python_client.parser.properties import build_model_property
schemas = {
"RefA": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["String"],
properties={
"String": oai.Schema.construct(type="string"),
"Enum": oai.Schema.construct(type="string", enum=["aValue"]),
"DateTime": oai.Schema.construct(type="string", format="date-time"),
},
),
"RefB": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["DateTime"],
properties={
"Int": oai.Schema.construct(type="integer"),
"DateTime": oai.Schema.construct(type="string", format="date-time"),
"Float": oai.Schema.construct(type="number", format="float"),
},
),
# Intentionally no properties defined
"RefC": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
),
}
model_schema = oai.Schema.construct(
allOf=[
oai.Reference.construct(ref="#/components/schemas/RefA"),
oai.Reference.construct(ref="#/components/schemas/RefB"),
oai.Reference.construct(ref="#/components/schemas/RefC"),
oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["Float"],
properties={
"String": oai.Schema.construct(type="string"),
"Float": oai.Schema.construct(type="number", format="float"),
},
),
]
)
components = {**schemas, "Model": model_schema}
from openapi_python_client.parser.properties import Schemas
schemas_holder = Schemas()
model, schemas_holder = build_model_property(
data=model_schema, name="Model", required=True, schemas=schemas_holder, parent_name=None
)
model.resolve_references(components, schemas_holder)
assert sorted(p.name for p in model.required_properties) == ["DateTime", "Float", "String"]
assert all(p.required for p in model.required_properties)
assert sorted(p.name for p in model.optional_properties) == ["Enum", "Int"]
assert all(not p.required for p in model.optional_properties)
def test_resolve_references_nested_allof(mocker):
import openapi_python_client.schema as oai
from openapi_python_client.parser.properties import build_model_property
schemas = {
"RefA": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["String"],
properties={
"String": oai.Schema.construct(type="string"),
"Enum": oai.Schema.construct(type="string", enum=["aValue"]),
"DateTime": oai.Schema.construct(type="string", format="date-time"),
},
),
"RefB": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["DateTime"],
properties={
"Int": oai.Schema.construct(type="integer"),
"DateTime": oai.Schema.construct(type="string", format="date-time"),
"Float": oai.Schema.construct(type="number", format="float"),
},
),
# Intentionally no properties defined
"RefC": oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
),
}
model_schema = oai.Schema.construct(
type="object",
properties={
"Key": oai.Schema.construct(
allOf=[
oai.Reference.construct(ref="#/components/schemas/RefA"),
oai.Reference.construct(ref="#/components/schemas/RefB"),
oai.Reference.construct(ref="#/components/schemas/RefC"),
oai.Schema.construct(
title=mocker.MagicMock(),
description=mocker.MagicMock(),
required=["Float"],
properties={
"String": oai.Schema.construct(type="string"),
"Float": oai.Schema.construct(type="number", format="float"),
},
),
]
),
}
)
components = {**schemas, "Model": model_schema}
from openapi_python_client.parser.properties import ModelProperty, Schemas
schemas_holder = Schemas()
model, schemas_holder = build_model_property(
data=model_schema, name="Model", required=True, schemas=schemas_holder, parent_name=None
)
model.resolve_references(components, schemas_holder)
assert sorted(p.name for p in model.required_properties) == []
assert sorted(p.name for p in model.optional_properties) == ["Key"]
assert all(not p.required for p in model.optional_properties)
key_property = model.optional_properties[0]
assert isinstance(key_property, ModelProperty)
key_property.resolve_references(components, schemas_holder)
assert sorted(p.name for p in key_property.required_properties) == ["DateTime", "Float", "String"]
assert all(p.required for p in key_property.required_properties)
assert sorted(p.name for p in key_property.optional_properties) == ["Enum", "Int"]
assert all(not p.required for p in key_property.optional_properties)