Skip to content

Commit 2363e75

Browse files
committed
Add tests for responses
#3
1 parent e0c4df9 commit 2363e75

File tree

3 files changed

+128
-19
lines changed

3 files changed

+128
-19
lines changed

openapi_python_client/openapi_parser/responses.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, Dict, Literal, TypedDict, Union, cast
2+
from typing import Any, Dict, Literal, TypedDict, Union
33

44
from .reference import Reference
55

@@ -11,7 +11,6 @@ class Response:
1111
""" Describes a single response for an endpoint """
1212

1313
status_code: int
14-
content_type: ContentType
1514

1615
def return_string(self) -> str:
1716
""" How this Response should be represented as a return type """
@@ -65,13 +64,6 @@ def constructor(self) -> str:
6564
return "response.text"
6665

6766

68-
@dataclass
69-
class EmptyResponse(Response):
70-
""" Response has no payload """
71-
72-
pass
73-
74-
7567
_openapi_types_to_python_type_strings = {
7668
"string": "str",
7769
"number": "float",
@@ -103,15 +95,11 @@ def response_from_dict(*, status_code: int, data: _ResponseDict) -> Response:
10395
schema_data = data["content"][content_type]["schema"]
10496

10597
if "$ref" in schema_data:
106-
return RefResponse(
107-
status_code=status_code, content_type=content_type, reference=Reference(schema_data["$ref"]),
108-
)
98+
return RefResponse(status_code=status_code, reference=Reference(schema_data["$ref"]),)
10999
if "type" not in schema_data:
110-
return EmptyResponse(status_code=status_code, content_type=content_type,)
100+
return Response(status_code=status_code)
111101
if schema_data["type"] == "array":
112-
return ListRefResponse(
113-
status_code=status_code, content_type=content_type, reference=Reference(schema_data["items"]["$ref"]),
114-
)
102+
return ListRefResponse(status_code=status_code, reference=Reference(schema_data["items"]["$ref"]),)
115103
if schema_data["type"] == "string":
116-
return StringResponse(status_code=status_code, content_type=content_type)
104+
return StringResponse(status_code=status_code)
117105
raise ValueError(f"Cannot parse response of type {schema_data['type']}")

tests/test_openapi_parser/test_openapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ def test__add_responses(self, mocker):
296296
)
297297
ref_1 = Reference(ref="ref_1")
298298
ref_2 = Reference(ref="ref_2")
299-
response_1 = RefResponse(status_code=200, content_type="application/json", reference=ref_1)
300-
response_2 = RefResponse(status_code=404, content_type="application/json", reference=ref_2)
299+
response_1 = RefResponse(status_code=200, reference=ref_1)
300+
response_2 = RefResponse(status_code=404, reference=ref_2)
301301
response_from_dict = mocker.patch(f"{MODULE_NAME}.response_from_dict", side_effect=[response_1, response_2])
302302
import_string_from_reference = mocker.patch(
303303
f"{MODULE_NAME}.import_string_from_reference", side_effect=["import_1", "import_2"]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import pytest
2+
3+
MODULE_NAME = "openapi_python_client.openapi_parser.responses"
4+
5+
6+
class TestResponse:
7+
def test_return_string(self):
8+
from openapi_python_client.openapi_parser.responses import Response
9+
10+
r = Response(200)
11+
12+
assert r.return_string() == "None"
13+
14+
def test_constructor(self):
15+
from openapi_python_client.openapi_parser.responses import Response
16+
17+
r = Response(200)
18+
19+
assert r.constructor() == "None"
20+
21+
22+
class TestListRefResponse:
23+
def test_return_string(self, mocker):
24+
from openapi_python_client.openapi_parser.responses import ListRefResponse
25+
26+
r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
27+
28+
assert r.return_string() == "List[SuperCoolClass]"
29+
30+
def test_constructor(self, mocker):
31+
from openapi_python_client.openapi_parser.responses import ListRefResponse
32+
33+
r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass"))
34+
35+
assert r.constructor() == "[SuperCoolClass.from_dict(item) for item in response.json()]"
36+
37+
38+
class TestStringResponse:
39+
def test_return_string(self):
40+
from openapi_python_client.openapi_parser.responses import StringResponse
41+
42+
r = StringResponse(200)
43+
44+
assert r.return_string() == "str"
45+
46+
def test_constructor(self):
47+
from openapi_python_client.openapi_parser.responses import StringResponse
48+
49+
r = StringResponse(200)
50+
51+
assert r.constructor() == "response.text"
52+
53+
54+
class TestResponseFromDict:
55+
def test_response_from_dict_no_content(self):
56+
from openapi_python_client.openapi_parser.responses import response_from_dict
57+
58+
with pytest.raises(ValueError):
59+
response_from_dict(status_code=200, data={})
60+
61+
def test_response_from_dict_unsupported_content_type(self):
62+
from openapi_python_client.openapi_parser.responses import response_from_dict
63+
64+
with pytest.raises(ValueError):
65+
response_from_dict(status_code=200, data={"content": {"not/real": {}}})
66+
67+
def test_response_from_dict_ref(self, mocker):
68+
ref = mocker.MagicMock()
69+
status_code = mocker.MagicMock(autospec=int)
70+
data = {"content": {"application/json": {"schema": {"$ref": ref}}}}
71+
Reference = mocker.patch(f"{MODULE_NAME}.Reference")
72+
RefResponse = mocker.patch(f"{MODULE_NAME}.RefResponse")
73+
from openapi_python_client.openapi_parser.responses import response_from_dict
74+
75+
response = response_from_dict(status_code=status_code, data=data)
76+
77+
Reference.assert_called_once_with(ref)
78+
RefResponse.assert_called_once_with(status_code=status_code, reference=Reference())
79+
assert response == RefResponse()
80+
81+
def test_response_from_dict_empty(self, mocker):
82+
status_code = mocker.MagicMock(autospec=int)
83+
data = {"content": {"application/json": {"schema": {}}}}
84+
Response = mocker.patch(f"{MODULE_NAME}.Response")
85+
from openapi_python_client.openapi_parser.responses import response_from_dict
86+
87+
response = response_from_dict(status_code=status_code, data=data)
88+
89+
Response.assert_called_once_with(status_code=status_code)
90+
assert response == Response()
91+
92+
def test_response_from_dict_array(self, mocker):
93+
ref = mocker.MagicMock()
94+
status_code = mocker.MagicMock(autospec=int)
95+
data = {"content": {"application/json": {"schema": {"type": "array", "items": {"$ref": ref}}}}}
96+
Reference = mocker.patch(f"{MODULE_NAME}.Reference")
97+
ListRefResponse = mocker.patch(f"{MODULE_NAME}.ListRefResponse")
98+
from openapi_python_client.openapi_parser.responses import response_from_dict
99+
100+
response = response_from_dict(status_code=status_code, data=data)
101+
102+
Reference.assert_called_once_with(ref)
103+
ListRefResponse.assert_called_once_with(status_code=status_code, reference=Reference())
104+
assert response == ListRefResponse()
105+
106+
def test_response_from_dict_string(self, mocker):
107+
status_code = mocker.MagicMock(autospec=int)
108+
data = {"content": {"text/html": {"schema": {"type": "string"}}}}
109+
StringResponse = mocker.patch(f"{MODULE_NAME}.StringResponse")
110+
from openapi_python_client.openapi_parser.responses import response_from_dict
111+
112+
response = response_from_dict(status_code=status_code, data=data)
113+
114+
StringResponse.assert_called_once_with(status_code=status_code)
115+
assert response == StringResponse()
116+
117+
def test_response_from_dict_unsupported_type(self):
118+
from openapi_python_client.openapi_parser.responses import response_from_dict
119+
120+
with pytest.raises(ValueError):
121+
response_from_dict(status_code=200, data={"content": {"application/json": {"schema": {"type": "blah"}}}})

0 commit comments

Comments
 (0)