forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_responses.py
137 lines (89 loc) · 5.28 KB
/
test_responses.py
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
import pytest
MODULE_NAME = "openapi_python_client.openapi_parser.responses"
class TestResponse:
def test_return_string(self):
from openapi_python_client.openapi_parser.responses import Response
r = Response(200)
assert r.return_string() == "None"
def test_constructor(self):
from openapi_python_client.openapi_parser.responses import Response
r = Response(200)
assert r.constructor() == "None"
class TestListRefResponse:
def test_return_string(self, mocker):
from openapi_python_client.openapi_parser.responses import ListRefResponse
r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
assert r.return_string() == "List[SuperCoolClass]"
def test_constructor(self, mocker):
from openapi_python_client.openapi_parser.responses import ListRefResponse
r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
assert r.constructor() == "[SuperCoolClass.from_dict(item) for item in cast(List[Dict[str, Any]], response.json())]"
class TestRefResponse:
def test_return_string(self, mocker):
from openapi_python_client.openapi_parser.responses import RefResponse
r = RefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
assert r.return_string() == "SuperCoolClass"
def test_constructor(self, mocker):
from openapi_python_client.openapi_parser.responses import RefResponse
r = RefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
assert r.constructor() == "SuperCoolClass.from_dict(cast(Dict[str, Any], response.json()))"
class TestStringResponse:
def test_return_string(self):
from openapi_python_client.openapi_parser.responses import StringResponse
r = StringResponse(200)
assert r.return_string() == "str"
def test_constructor(self):
from openapi_python_client.openapi_parser.responses import StringResponse
r = StringResponse(200)
assert r.constructor() == "response.text"
class TestResponseFromDict:
def test_response_from_dict_no_content(self):
from openapi_python_client.openapi_parser.responses import response_from_dict
with pytest.raises(ValueError):
response_from_dict(status_code=200, data={})
def test_response_from_dict_unsupported_content_type(self):
from openapi_python_client.openapi_parser.responses import response_from_dict
with pytest.raises(ValueError):
response_from_dict(status_code=200, data={"content": {"not/real": {}}})
def test_response_from_dict_ref(self, mocker):
ref = mocker.MagicMock()
status_code = mocker.MagicMock(autospec=int)
data = {"content": {"application/json": {"schema": {"$ref": ref}}}}
from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")
RefResponse = mocker.patch(f"{MODULE_NAME}.RefResponse")
from openapi_python_client.openapi_parser.responses import response_from_dict
response = response_from_dict(status_code=status_code, data=data)
from_ref.assert_called_once_with(ref)
RefResponse.assert_called_once_with(status_code=status_code, reference=from_ref())
assert response == RefResponse()
def test_response_from_dict_empty(self, mocker):
status_code = mocker.MagicMock(autospec=int)
data = {"content": {"application/json": {"schema": {}}}}
Response = mocker.patch(f"{MODULE_NAME}.Response")
from openapi_python_client.openapi_parser.responses import response_from_dict
response = response_from_dict(status_code=status_code, data=data)
Response.assert_called_once_with(status_code=status_code)
assert response == Response()
def test_response_from_dict_array(self, mocker):
ref = mocker.MagicMock()
status_code = mocker.MagicMock(autospec=int)
data = {"content": {"application/json": {"schema": {"type": "array", "items": {"$ref": ref}}}}}
from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")
ListRefResponse = mocker.patch(f"{MODULE_NAME}.ListRefResponse")
from openapi_python_client.openapi_parser.responses import response_from_dict
response = response_from_dict(status_code=status_code, data=data)
from_ref.assert_called_once_with(ref)
ListRefResponse.assert_called_once_with(status_code=status_code, reference=from_ref())
assert response == ListRefResponse()
def test_response_from_dict_string(self, mocker):
status_code = mocker.MagicMock(autospec=int)
data = {"content": {"text/html": {"schema": {"type": "string"}}}}
StringResponse = mocker.patch(f"{MODULE_NAME}.StringResponse")
from openapi_python_client.openapi_parser.responses import response_from_dict
response = response_from_dict(status_code=status_code, data=data)
StringResponse.assert_called_once_with(status_code=status_code)
assert response == StringResponse()
def test_response_from_dict_unsupported_type(self):
from openapi_python_client.openapi_parser.responses import response_from_dict
with pytest.raises(ValueError):
response_from_dict(status_code=200, data={"content": {"application/json": {"schema": {"type": "blah"}}}})