Skip to content

Commit d19109b

Browse files
committed
Add tests for Endpoint
#3
1 parent c8d7f11 commit d19109b

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

openapi_python_client/openapi_parser/openapi.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ class Endpoint:
115115
@staticmethod
116116
def parse_request_form_body(body: Dict[str, Any], /) -> Optional[Reference]:
117117
""" Return form_body_reference """
118-
form_body_reference = None
119118
body_content = body["content"]
120119
form_body = body_content.get("application/x-www-form-urlencoded")
121120
if form_body:
122-
form_body_reference = Reference(form_body["schema"]["$ref"])
123-
return form_body_reference
121+
return Reference(form_body["schema"]["$ref"])
122+
return None
124123

125124
@staticmethod
126125
def parse_request_json_body(body: Dict[str, Any], /) -> Optional[Property]:

tests/test_openapi_parser/test_openapi.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,47 @@ def test_from_dict(self, mocker):
181181
relative_imports={import_string_from_reference()},
182182
description=in_data["description"],
183183
)
184+
185+
186+
class TestEndpoint:
187+
def test_parse_request_form_body(self, mocker):
188+
ref = mocker.MagicMock()
189+
body = {"content": {"application/x-www-form-urlencoded": {"schema": {"$ref": ref}}}}
190+
Reference = mocker.patch(f"{MODULE_NAME}.Reference")
191+
192+
from openapi_python_client.openapi_parser.openapi import Endpoint
193+
194+
result = Endpoint.parse_request_form_body(body)
195+
196+
Reference.assert_called_once_with(ref)
197+
assert result == Reference()
198+
199+
def test_parse_request_form_body_no_data(self):
200+
body = {"content": {}}
201+
202+
from openapi_python_client.openapi_parser.openapi import Endpoint
203+
204+
result = Endpoint.parse_request_form_body(body)
205+
206+
assert result is None
207+
208+
def test_parse_request_json_body(self, mocker):
209+
schema = mocker.MagicMock()
210+
body = {"content": {"application/json": {"schema": schema}}}
211+
property_from_dict = mocker.patch(f"{MODULE_NAME}.property_from_dict")
212+
213+
from openapi_python_client.openapi_parser.openapi import Endpoint
214+
215+
result = Endpoint.parse_request_json_body(body)
216+
217+
property_from_dict.assert_called_once_with("json_body", required=True, data=schema)
218+
assert result == property_from_dict()
219+
220+
def test_parse_request_json_body_no_data(self):
221+
body = {"content": {}}
222+
223+
from openapi_python_client.openapi_parser.openapi import Endpoint
224+
225+
result = Endpoint.parse_request_json_body(body)
226+
227+
assert result is None

0 commit comments

Comments
 (0)