Skip to content

Commit 5fe28f5

Browse files
committed
Add test related to quoted argument
1 parent fbcfd65 commit 5fe28f5

File tree

4 files changed

+225
-57
lines changed

4 files changed

+225
-57
lines changed

tests/conftest.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from openapi_python_client import schema as oai
66
from openapi_python_client.parser.properties import (
77
AnyProperty,
8+
BooleanProperty,
89
DateProperty,
910
DateTimeProperty,
1011
EnumProperty,
@@ -43,7 +44,6 @@ def _factory(**kwargs):
4344
"lazy_imports": None,
4445
"additional_properties": None,
4546
"python_name": "",
46-
"description": "",
4747
"example": "",
4848
**kwargs,
4949
}
@@ -149,6 +149,21 @@ def _factory(**kwargs):
149149
return _factory
150150

151151

152+
@pytest.fixture
153+
def boolean_property_factory() -> Callable[..., BooleanProperty]:
154+
"""
155+
This fixture surfaces in the test as a function which manufactures StringProperties with defaults.
156+
157+
You can pass the same params into this as the StringProperty constructor to override defaults.
158+
"""
159+
160+
def _factory(**kwargs):
161+
kwargs = _common_kwargs(kwargs)
162+
return BooleanProperty(**kwargs)
163+
164+
return _factory
165+
166+
152167
@pytest.fixture
153168
def date_time_property_factory() -> Callable[..., DateTimeProperty]:
154169
"""

tests/test_parser/test_properties/test_init.py

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414

1515
class TestStringProperty:
16+
def test_is_base_type(self, string_property_factory):
17+
assert string_property_factory().is_base_type is True
18+
1619
@pytest.mark.parametrize(
1720
"required, nullable, expected",
1821
(
@@ -29,6 +32,9 @@ def test_get_type_string(self, string_property_factory, required, nullable, expe
2932

3033

3134
class TestDateTimeProperty:
35+
def test_is_base_type(self, date_time_property_factory):
36+
assert date_time_property_factory().is_base_type is True
37+
3238
@pytest.mark.parametrize("required", (True, False))
3339
@pytest.mark.parametrize("nullable", (True, False))
3440
def test_get_imports(self, date_time_property_factory, required, nullable):
@@ -51,6 +57,9 @@ def test_get_imports(self, date_time_property_factory, required, nullable):
5157

5258

5359
class TestDateProperty:
60+
def test_is_base_type(self, date_property_factory):
61+
assert date_property_factory().is_base_type is True
62+
5463
@pytest.mark.parametrize("required", (True, False))
5564
@pytest.mark.parametrize("nullable", (True, False))
5665
def test_get_imports(self, date_property_factory, required, nullable):
@@ -73,6 +82,9 @@ def test_get_imports(self, date_property_factory, required, nullable):
7382

7483

7584
class TestFileProperty:
85+
def test_is_base_type(self, file_property_factory):
86+
assert file_property_factory().is_base_type is True
87+
7688
@pytest.mark.parametrize("required", (True, False))
7789
@pytest.mark.parametrize("nullable", (True, False))
7890
def test_get_imports(self, file_property_factory, required, nullable):
@@ -93,7 +105,30 @@ def test_get_imports(self, file_property_factory, required, nullable):
93105
assert p.get_imports(prefix="...") == expected
94106

95107

108+
class TestNoneProperty:
109+
def test_is_base_type(self, none_property_factory):
110+
assert none_property_factory().is_base_type is True
111+
112+
113+
class TestBooleanProperty:
114+
def test_is_base_type(self, boolean_property_factory):
115+
assert boolean_property_factory().is_base_type is True
116+
117+
118+
class TestAnyProperty:
119+
def test_is_base_type(self, any_property_factory):
120+
assert any_property_factory().is_base_type is True
121+
122+
123+
class TestIntProperty:
124+
def test_is_base_type(self, int_property_factory):
125+
assert int_property_factory().is_base_type is True
126+
127+
96128
class TestListProperty:
129+
def test_is_base_type(self, list_property_factory):
130+
assert list_property_factory().is_base_type is False
131+
97132
@pytest.mark.parametrize(
98133
"required, nullable, expected",
99134
(
@@ -103,11 +138,51 @@ class TestListProperty:
103138
(False, True, "Union[Unset, None, List[str]]"),
104139
),
105140
)
106-
def test_get_type_string(self, list_property_factory, required, nullable, expected):
141+
def test_get_type_string_base_inner(self, list_property_factory, required, nullable, expected):
107142
p = list_property_factory(required=required, nullable=nullable)
108143

109144
assert p.get_type_string() == expected
110145

146+
@pytest.mark.parametrize(
147+
"required, nullable, expected",
148+
(
149+
(True, False, "List['MyClass']"),
150+
(True, True, "Optional[List['MyClass']]"),
151+
(False, False, "Union[Unset, List['MyClass']]"),
152+
(False, True, "Union[Unset, None, List['MyClass']]"),
153+
),
154+
)
155+
def test_get_type_string_model_inner(
156+
self, list_property_factory, model_property_factory, required, nullable, expected
157+
):
158+
m = model_property_factory()
159+
p = list_property_factory(required=required, nullable=nullable, inner_property=m)
160+
161+
assert p.get_type_string() == expected
162+
163+
@pytest.mark.parametrize(
164+
"quoted,expected",
165+
[
166+
(False, "List[str]"),
167+
(True, "List[str]"),
168+
],
169+
)
170+
def test_get_base_type_string_base_inner(self, list_property_factory, quoted, expected):
171+
p = list_property_factory()
172+
assert p.get_base_type_string(quoted=quoted) == expected
173+
174+
@pytest.mark.parametrize(
175+
"quoted,expected",
176+
[
177+
(False, "List['MyClass']"),
178+
(True, "List['MyClass']"),
179+
],
180+
)
181+
def test_get_base_type_string_model_inner(self, list_property_factory, model_property_factory, quoted, expected):
182+
m = model_property_factory()
183+
p = list_property_factory(inner_property=m)
184+
assert p.get_base_type_string(quoted=quoted) == expected
185+
111186
@pytest.mark.parametrize("required", (True, False))
112187
@pytest.mark.parametrize("nullable", (True, False))
113188
def test_get_type_imports(self, list_property_factory, date_time_property_factory, required, nullable):
@@ -131,6 +206,9 @@ def test_get_type_imports(self, list_property_factory, date_time_property_factor
131206

132207

133208
class TestUnionProperty:
209+
def test_is_base_type(self, union_property_factory):
210+
assert union_property_factory().is_base_type is False
211+
134212
@pytest.mark.parametrize(
135213
"nullable,required,no_optional,json,expected",
136214
[
@@ -173,18 +251,34 @@ def test_get_type_string(
173251

174252
assert p.get_type_string(no_optional=no_optional, json=json) == expected
175253

176-
def test_get_base_type_string(self, union_property_factory, date_time_property_factory, string_property_factory):
254+
def test_get_base_type_string_base_inners(
255+
self, union_property_factory, date_time_property_factory, string_property_factory
256+
):
177257
p = union_property_factory(inner_properties=[date_time_property_factory(), string_property_factory()])
178258

179259
assert p.get_base_type_string() == "Union[datetime.datetime, str]"
180260

181-
def test_get_base_type_string_one_element(self, union_property_factory, date_time_property_factory):
261+
def test_get_base_type_string_one_base_inner(self, union_property_factory, date_time_property_factory):
182262
p = union_property_factory(
183263
inner_properties=[date_time_property_factory()],
184264
)
185265

186266
assert p.get_base_type_string() == "datetime.datetime"
187267

268+
def test_get_base_type_string_one_model_inner(self, union_property_factory, model_property_factory):
269+
p = union_property_factory(
270+
inner_properties=[model_property_factory()],
271+
)
272+
273+
assert p.get_base_type_string() == "'MyClass'"
274+
275+
def test_get_base_type_string_model_inners(
276+
self, union_property_factory, date_time_property_factory, model_property_factory
277+
):
278+
p = union_property_factory(inner_properties=[date_time_property_factory(), model_property_factory()])
279+
280+
assert p.get_base_type_string() == "Union['MyClass', datetime.datetime]"
281+
188282
def test_get_base_json_type_string(self, union_property_factory, date_time_property_factory):
189283
p = union_property_factory(
190284
inner_properties=[date_time_property_factory()],
@@ -216,6 +310,9 @@ def test_get_type_imports(self, union_property_factory, date_time_property_facto
216310

217311

218312
class TestEnumProperty:
313+
def test_is_base_type(self, enum_property_factory):
314+
assert enum_property_factory().is_base_type is True
315+
219316
@pytest.mark.parametrize(
220317
"required, nullable, expected",
221318
(

tests/test_parser/test_properties/test_model_property.py

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,71 @@
1010
MODULE_NAME = "openapi_python_client.parser.properties.model_property"
1111

1212

13-
@pytest.mark.parametrize(
14-
"no_optional,nullable,required,json,expected",
15-
[
16-
(False, False, False, False, "Union[Unset, MyClass]"),
17-
(False, False, True, False, "MyClass"),
18-
(False, True, False, False, "Union[Unset, None, MyClass]"),
19-
(False, True, True, False, "Optional[MyClass]"),
20-
(True, False, False, False, "MyClass"),
21-
(True, False, True, False, "MyClass"),
22-
(True, True, False, False, "MyClass"),
23-
(True, True, True, False, "MyClass"),
24-
(False, False, True, True, "Dict[str, Any]"),
25-
],
26-
)
27-
def test_get_type_string(no_optional, nullable, required, json, expected, model_property_factory):
28-
29-
prop = model_property_factory(
30-
required=required,
31-
nullable=nullable,
13+
class TestModelProperty:
14+
@pytest.mark.parametrize(
15+
"no_optional,nullable,required,json,quoted,expected",
16+
[
17+
(False, False, False, False, False, "Union[Unset, MyClass]"),
18+
(False, False, True, False, False, "MyClass"),
19+
(False, True, False, False, False, "Union[Unset, None, MyClass]"),
20+
(False, True, True, False, False, "Optional[MyClass]"),
21+
(True, False, False, False, False, "MyClass"),
22+
(True, False, True, False, False, "MyClass"),
23+
(True, True, False, False, False, "MyClass"),
24+
(True, True, True, False, False, "MyClass"),
25+
(False, False, True, True, False, "Dict[str, Any]"),
26+
(False, False, False, False, True, "Union[Unset, 'MyClass']"),
27+
(False, False, True, False, True, "'MyClass'"),
28+
(False, True, False, False, True, "Union[Unset, None, 'MyClass']"),
29+
(False, True, True, False, True, "Optional['MyClass']"),
30+
(True, False, False, False, True, "'MyClass'"),
31+
(True, False, True, False, True, "'MyClass'"),
32+
(True, True, False, False, True, "'MyClass'"),
33+
(True, True, True, False, True, "'MyClass'"),
34+
(False, False, True, True, True, "Dict[str, Any]"),
35+
],
3236
)
37+
def test_get_type_string(self, no_optional, nullable, required, json, expected, model_property_factory, quoted):
38+
39+
prop = model_property_factory(
40+
required=required,
41+
nullable=nullable,
42+
)
3343

34-
assert prop.get_type_string(no_optional=no_optional, json=json) == expected
44+
assert prop.get_type_string(no_optional=no_optional, json=json, quoted=quoted) == expected
3545

46+
def test_get_imports(self, model_property_factory):
47+
prop = model_property_factory(required=False, nullable=True)
3648

37-
def test_get_imports(model_property_factory):
38-
prop = model_property_factory(required=False, nullable=True)
49+
assert prop.get_imports(prefix="..") == {
50+
"from typing import Optional",
51+
"from typing import Union",
52+
"from ..types import UNSET, Unset",
53+
"from typing import Dict",
54+
"from typing import cast",
55+
}
56+
57+
def test_get_lazy_imports(self, model_property_factory):
58+
prop = model_property_factory(required=False, nullable=True)
3959

40-
assert prop.get_imports(prefix="..") == {
41-
"from typing import Optional",
42-
"from typing import Union",
43-
"from ..types import UNSET, Unset",
44-
"from typing import Dict",
45-
"from typing import cast",
46-
}
60+
assert prop.get_lazy_imports(prefix="..") == {
61+
"from ..models.my_module import MyClass",
62+
}
4763

64+
def test_is_base_type(self, model_property_factory):
65+
assert model_property_factory().is_base_type is False
4866

49-
def test_get_lazy_imports(model_property_factory):
50-
prop = model_property_factory(required=False, nullable=True)
67+
@pytest.mark.parametrize(
68+
"quoted,expected",
69+
[
70+
(False, "MyClass"),
71+
(True, '"MyClass"'),
72+
],
73+
)
74+
def test_get_base_type_string(self, quoted, expected, model_property_factory):
5175

52-
assert prop.get_lazy_imports(prefix="..") == {
53-
"from ..models.my_module import MyClass",
54-
}
76+
m = model_property_factory()
77+
assert m.get_base_type_string(quoted=quoted) == expected
5578

5679

5780
class TestBuildModelProperty:

0 commit comments

Comments
 (0)