Skip to content

Commit 018d47b

Browse files
committed
test: test alt json decoder
1 parent ad8f601 commit 018d47b

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

tests/test_config.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88
from ruamel.yaml import YAML as _YAML
99

10-
from openapi_python_client.config import ConfigFile
10+
from openapi_python_client.config import Config, ConfigFile, JSONDecoder, MetaType
1111

1212

1313
class YAML(_YAML):
@@ -59,3 +59,22 @@ def test_load_from_path(tmp_path: Path, filename, dump, relative) -> None:
5959
assert config.project_name_override == "project-name"
6060
assert config.package_name_override == "package_name"
6161
assert config.package_version_override == "package_version"
62+
63+
64+
@pytest.mark.parametrize("json", [None, "orjson", "ujson"])
65+
def test_config_with_alt_json(json):
66+
config = Config.from_sources(
67+
ConfigFile(alt_json_decoder=json),
68+
MetaType.POETRY,
69+
document_source=Path("openapi.yaml"),
70+
file_encoding="utf-8",
71+
overwrite=False,
72+
output_path=None,
73+
)
74+
config.alt_json_decoder = json
75+
if json is None:
76+
assert config.alt_json_decoder is None
77+
elif json == "orjson":
78+
assert config.alt_json_decoder == JSONDecoder.ORJSON
79+
elif json == "ujson":
80+
assert config.alt_json_decoder == JSONDecoder.UJSON

tests/test_parser/test_responses.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import pytest
44

55
import openapi_python_client.schema as oai
6+
from openapi_python_client.config import JSONDecoder
67
from openapi_python_client.parser import responses
78
from openapi_python_client.parser.errors import ParseError, PropertyError
89
from openapi_python_client.parser.properties import Schemas
910
from openapi_python_client.parser.responses import (
11+
ALT_JSON_SOURCE,
1012
JSON_SOURCE,
1113
NONE_SOURCE,
1214
HTTPStatusPattern,
@@ -184,6 +186,43 @@ def test_response_from_data_reference(mocker, any_property_factory):
184186
)
185187

186188

189+
def test_response_with_alt_decoder(mocker, any_property_factory):
190+
prop = any_property_factory()
191+
property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas()))
192+
data = oai.Response.model_construct(
193+
description="",
194+
content={"application/json": oai.MediaType.model_construct(media_type_schema="something")},
195+
)
196+
config = MagicMock()
197+
config.content_type_overrides = {}
198+
config.alt_json_decoder = JSONDecoder.ORJSON
199+
status_code = HTTPStatusPattern(pattern="400", code_range=(400, 400))
200+
201+
response, schemas = responses.response_from_data(
202+
status_code=status_code,
203+
data=data,
204+
schemas=Schemas(),
205+
responses={},
206+
parent_name="parent",
207+
config=config,
208+
)
209+
210+
assert response == responses.Response(
211+
status_code=status_code,
212+
prop=prop,
213+
source=ALT_JSON_SOURCE,
214+
data=data,
215+
)
216+
property_from_data.assert_called_once_with(
217+
name="response_400",
218+
required=True,
219+
data="something",
220+
schemas=Schemas(),
221+
parent_name="parent",
222+
config=config,
223+
)
224+
225+
187226
@pytest.mark.parametrize(
188227
"ref_string,expected_error_string",
189228
[

0 commit comments

Comments
 (0)