Skip to content

Commit 5d1d41e

Browse files
committed
Add json tests, swap out orjson for standard json
#3
1 parent d593eff commit 5d1d41e

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

openapi_python_client/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
""" Generate modern Python clients from OpenAPI """
2+
import json
23
from pathlib import Path
34
from typing import Any, Dict, Optional
45

5-
import orjson
66
import requests
77
from jinja2 import Environment, PackageLoader
88

@@ -18,14 +18,16 @@ def main(*, url: Optional[str], path: Optional[str]) -> None:
1818

1919
def _get_json(*, url: Optional[str], path: Optional[str]) -> Dict[str, Any]:
2020
json_bytes: bytes
21-
if url is not None:
21+
if url is not None and path is not None:
22+
raise ValueError("Provide URL or Path, not both.")
23+
elif url is not None:
2224
response = requests.get(url)
2325
json_bytes = response.content
2426
elif path is not None:
2527
json_bytes = Path(path).read_bytes()
2628
else:
2729
raise ValueError("No URL or Path provided")
28-
return orjson.loads(json_bytes)
30+
return json.loads(json_bytes)
2931

3032

3133
def _build_project(*, openapi: OpenAPI) -> None:

poetry.lock

Lines changed: 1 addition & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ include = ["CHANGELOG.md", "openapi_python_client/py.typed"]
1717
[tool.poetry.dependencies]
1818
python = "==3.*,>=3.8.0"
1919
requests = "^2.22.0"
20-
orjson = "^2.4.0"
2120
jinja2 = "^2.11.1"
2221
stringcase = "^1.2.0"
2322
click = "^7.0"

tests/test___init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import pytest
2+
3+
14
def test_main(mocker):
25
data_dict = mocker.MagicMock()
36
_get_json = mocker.patch("openapi_python_client._get_json", return_value=data_dict)
@@ -14,3 +17,61 @@ def test_main(mocker):
1417
_get_json.assert_called_once_with(url=url, path=path)
1518
from_dict.assert_called_once_with(data_dict)
1619
_build_project.assert_called_once_with(openapi=openapi)
20+
21+
22+
class TestGetJson:
23+
def test__get_json_no_url_or_path(self, mocker):
24+
get = mocker.patch("requests.get")
25+
Path = mocker.patch("pathlib.Path")
26+
loads = mocker.patch("json.loads")
27+
28+
from openapi_python_client import _get_json
29+
30+
with pytest.raises(ValueError):
31+
_get_json(url=None, path=None)
32+
33+
get.assert_not_called()
34+
Path.assert_not_called()
35+
loads.assert_not_called()
36+
37+
def test__get_json_url_and_path(self, mocker):
38+
get = mocker.patch("requests.get")
39+
Path = mocker.patch("pathlib.Path")
40+
loads = mocker.patch("json.loads")
41+
42+
from openapi_python_client import _get_json
43+
44+
with pytest.raises(ValueError):
45+
_get_json(url=mocker.MagicMock(), path=mocker.MagicMock())
46+
47+
get.assert_not_called()
48+
Path.assert_not_called()
49+
loads.assert_not_called()
50+
51+
def test__get_json_url_no_path(self, mocker):
52+
get = mocker.patch("requests.get")
53+
Path = mocker.patch("pathlib.Path")
54+
loads = mocker.patch("json.loads")
55+
56+
from openapi_python_client import _get_json
57+
58+
url = mocker.MagicMock()
59+
_get_json(url=url, path=None)
60+
61+
get.assert_called_once_with(url)
62+
Path.assert_not_called()
63+
loads.assert_called_once_with(get().content)
64+
65+
def test__get_json_path_no_url(self, mocker):
66+
get = mocker.patch("requests.get")
67+
Path = mocker.patch("pathlib.Path")
68+
loads = mocker.patch("json.loads")
69+
70+
from openapi_python_client import _get_json
71+
72+
path = mocker.MagicMock()
73+
_get_json(url=None, path=path)
74+
75+
get.assert_not_called()
76+
Path.assert_called_once_with(path)
77+
loads.assert_called_once_with(Path().read_bytes())

0 commit comments

Comments
 (0)