forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_responses.py
More file actions
166 lines (136 loc) · 4.65 KB
/
test_responses.py
File metadata and controls
166 lines (136 loc) · 4.65 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
from unittest.mock import MagicMock
import openapi_python_client.schema as oai
from openapi_python_client.parser.errors import ParseError, PropertyError
from openapi_python_client.parser.properties import Schemas
from openapi_python_client.parser.responses import JSON_SOURCE, NONE_SOURCE
MODULE_NAME = "openapi_python_client.parser.responses"
def test_response_from_data_no_content(any_property_factory):
from openapi_python_client.parser.responses import Response, response_from_data
data = oai.Response.model_construct(description="")
response, schemas = response_from_data(
status_code=200,
data=data,
schemas=Schemas(),
parent_name="parent",
config=MagicMock(),
)
assert response == Response(
status_code=200,
prop=any_property_factory(
name="response_200",
default=None,
required=True,
description="",
),
source=NONE_SOURCE,
data=data,
)
def test_response_from_data_reference(any_property_factory):
from openapi_python_client.parser.responses import Response, response_from_data
data = oai.Reference.model_construct()
response, schemas = response_from_data(
status_code=200,
data=data,
schemas=Schemas(),
parent_name="parent",
config=MagicMock(),
)
assert response == Response(
status_code=200,
prop=any_property_factory(
name="response_200",
default=None,
required=True,
),
source=NONE_SOURCE,
data=data,
)
def test_response_from_data_unsupported_content_type():
from openapi_python_client.parser.responses import response_from_data
data = oai.Response.model_construct(description="", content={"blah": None})
response, schemas = response_from_data(
status_code=200,
data=data,
schemas=Schemas(),
parent_name="parent",
config=MagicMock(),
)
assert response == ParseError(data=data, detail="Unsupported content_type {'blah': None}")
def test_response_from_data_no_content_schema(any_property_factory):
from openapi_python_client.parser.responses import Response, response_from_data
data = oai.Response.model_construct(
description="",
content={"application/vnd.api+json; version=2.2": oai.MediaType.model_construct()},
)
response, schemas = response_from_data(
status_code=200,
data=data,
schemas=Schemas(),
parent_name="parent",
config=MagicMock(),
)
assert response == Response(
status_code=200,
prop=any_property_factory(
name="response_200",
default=None,
required=True,
description=data.description,
),
source=NONE_SOURCE,
data=data,
)
def test_response_from_data_property_error(mocker):
from openapi_python_client.parser import responses
property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(PropertyError(), Schemas()))
data = oai.Response.model_construct(
description="",
content={"application/json": oai.MediaType.model_construct(media_type_schema="something")},
)
config = MagicMock()
response, schemas = responses.response_from_data(
status_code=400,
data=data,
schemas=Schemas(),
parent_name="parent",
config=config,
)
assert response == PropertyError()
property_from_data.assert_called_once_with(
name="response_400",
required=True,
data="something",
schemas=Schemas(),
parent_name="parent",
config=config,
)
def test_response_from_data_property(mocker, any_property_factory):
from openapi_python_client.parser import responses
prop = any_property_factory()
property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas()))
data = oai.Response.model_construct(
description="",
content={"application/json": oai.MediaType.model_construct(media_type_schema="something")},
)
config = MagicMock()
response, schemas = responses.response_from_data(
status_code=400,
data=data,
schemas=Schemas(),
parent_name="parent",
config=config,
)
assert response == responses.Response(
status_code=400,
prop=prop,
source=JSON_SOURCE,
data=data,
)
property_from_data.assert_called_once_with(
name="response_400",
required=True,
data="something",
schemas=Schemas(),
parent_name="parent",
config=config,
)