Skip to content

Commit 1c84332

Browse files
committed
Started adding property tests
#3
1 parent f06d3cc commit 1c84332

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

openapi_python_client/openapi_parser/properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass, field
2-
from typing import Any, ClassVar, Dict, List, Optional, Union
2+
from typing import Any, ClassVar, Dict, List, Optional
33

44
from .reference import Reference
55

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
3+
4+
class TestProperty:
5+
def test_get_type_string(self):
6+
from openapi_python_client.openapi_parser.properties import Property
7+
8+
p = Property(name="test", required=True, default=None)
9+
p._type_string = "TestType"
10+
11+
assert p.get_type_string() == "TestType"
12+
p.required = False
13+
assert p.get_type_string() == "Optional[TestType]"
14+
15+
def test_to_string(self, mocker):
16+
from openapi_python_client.openapi_parser.properties import Property
17+
18+
name = mocker.MagicMock()
19+
p = Property(name=name, required=True, default=None)
20+
get_type_string = mocker.patch.object(p, "get_type_string")
21+
22+
assert p.to_string() == f"{name}: {get_type_string()}"
23+
p.required = False
24+
assert p.to_string() == f"{name}: {get_type_string()} = None"
25+
26+
p.default = "TEST"
27+
assert p.to_string() == f"{name}: {get_type_string()} = TEST"
28+
29+
def test_transform(self, mocker):
30+
from openapi_python_client.openapi_parser.properties import Property
31+
32+
name = mocker.MagicMock()
33+
p = Property(name=name, required=True, default=None)
34+
assert p.transform() == name
35+
36+
def test_constructor_from_dict(self, mocker):
37+
from openapi_python_client.openapi_parser.properties import Property
38+
39+
name = mocker.MagicMock()
40+
p = Property(name=name, required=True, default=None)
41+
dict_name = mocker.MagicMock()
42+
43+
assert p.constructor_from_dict(dict_name) == f'{dict_name}["{name}"]'
44+
45+
p.required = False
46+
assert p.constructor_from_dict(dict_name) == f'{dict_name}.get("{name}")'
47+
48+
49+
class TestStringProperty:
50+
def test___post_init__(self):
51+
from openapi_python_client.openapi_parser.properties import StringProperty
52+
53+
sp = StringProperty(name="test", required=True, default="A Default Value",)
54+
55+
assert sp.default == '"A Default Value"'
56+
57+
def test_get_type_string(self):
58+
from openapi_python_client.openapi_parser.properties import StringProperty
59+
60+
p = StringProperty(name="test", required=True, default=None)
61+
62+
assert p.get_type_string() == "str"
63+
p.required = False
64+
assert p.get_type_string() == "Optional[str]"
65+
66+
67+
class TestListProperty:
68+
def test_get_type_string_when_type(self):
69+
from openapi_python_client.openapi_parser.properties import ListProperty
70+
71+
p = ListProperty(name="test", required=True, default=None, type="MyTestType", reference=None)
72+
73+
assert p.get_type_string() == "List[MyTestType]"
74+
p.required = False
75+
assert p.get_type_string() == "Optional[List[MyTestType]]"
76+
77+
def test_get_type_string_when_reference(self, mocker):
78+
from openapi_python_client.openapi_parser.properties import ListProperty, Reference
79+
80+
reference = mocker.MagicMock(autospec=Reference)
81+
reference.class_name = "MyTestClassName"
82+
p = ListProperty(name="test", required=True, default=None, type=None, reference=reference)
83+
84+
assert p.get_type_string() == "List[MyTestClassName]"
85+
p.required = False
86+
assert p.get_type_string() == "Optional[List[MyTestClassName]]"
87+
88+
def test_get_type_string_fails_when_no_type_nor_reference(self, mocker):
89+
from openapi_python_client.openapi_parser.properties import ListProperty
90+
91+
p = ListProperty(name="test", required=True, default=None, type=None, reference=None)
92+
93+
with pytest.raises(ValueError):
94+
p.get_type_string()

0 commit comments

Comments
 (0)