Skip to content

Commit 9d43568

Browse files
committed
Add some tests for the async_endpoint_module template
1 parent 3908778 commit 9d43568

File tree

9 files changed

+164
-9
lines changed

9 files changed

+164
-9
lines changed

end_to_end_tests/golden-master/my_test_api_client/async_api/default.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
async def ping_ping_get(*, client: Client,) -> bool:
1111

1212
""" A quick check to see if the system is running """
13-
url = "{}/ping".format(client.base_url)
13+
url = "{}/ping".format(client.base_url,)
1414

1515
async with httpx.AsyncClient() as _client:
1616
response = await _client.get(url=url, headers=client.get_headers(),)

end_to_end_tests/golden-master/my_test_api_client/async_api/users.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def get_user_list(
1919
]:
2020

2121
""" Get a list of things """
22-
url = "{}/tests/".format(client.base_url)
22+
url = "{}/tests/".format(client.base_url,)
2323

2424
json_an_enum_value = []
2525
for an_enum_value_item_data in an_enum_value:
@@ -56,7 +56,7 @@ async def upload_file_tests_upload_post(
5656
]:
5757

5858
""" Upload a file """
59-
url = "{}/tests/upload".format(client.base_url)
59+
url = "{}/tests/upload".format(client.base_url,)
6060

6161
async with httpx.AsyncClient() as _client:
6262
response = await _client.post(url=url, headers=client.get_headers(), files=multipart_data.to_dict(),)

openapi_python_client/templates/async_endpoint_module.pyi

+4-6
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ async def {{ endpoint.name | snakecase }}(
4444
{{ return_type(endpoint) }}
4545
""" {{ endpoint.description }} """
4646
url = "{}{{ endpoint.path }}".format(
47-
client.base_url
48-
{%- for parameter in endpoint.path_parameters -%}
49-
,{{parameter.name}}={{parameter.python_name}}
50-
{%- endfor -%}
47+
client.base_url,
48+
{% for parameter in endpoint.path_parameters %}
49+
{{parameter.name}}={{parameter.python_name}},
50+
{% endfor %}
5151
)
5252

5353
{{ query_params(endpoint) | indent(4) }}
54-
5554
{{ json_body(endpoint) | indent(4) }}
5655

5756
async with httpx.AsyncClient() as _client:
@@ -61,7 +60,6 @@ async def {{ endpoint.name | snakecase }}(
6160
{% if endpoint.form_body_reference %}
6261
data=asdict(form_data),
6362
{% endif %}
64-
6563
{% if endpoint.multipart_body_reference %}
6664
files=multipart_data.to_dict(),
6765
{% endif %}

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ exclude = '''
6666
| \.venv
6767
| \.mypy_cache
6868
| openapi_python_client/templates
69+
| tests/test_templates
6970
)/
7071
)
7172
'''
@@ -74,3 +75,4 @@ exclude = '''
7475
line_length = 120
7576
multi_line_output = 3
7677
include_trailing_comma = true
78+
skip = ["tests/test_templates"]

tests/test___init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ def test_load_config(mocker):
484484
fake_path = mocker.MagicMock(autospec=pathlib.Path)
485485

486486
from openapi_python_client import load_config
487+
from openapi_python_client.openapi_parser import reference
488+
489+
reference.class_overrides = {}
487490

488491
load_config(path=fake_path)
489492

tests/test_templates/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from dataclasses import asdict
2+
from typing import Any, Dict, List, Optional, Union, cast
3+
4+
import httpx
5+
6+
from ..client import AuthenticatedClient, Client
7+
from ..errors import ApiResponseError
8+
9+
import this
10+
from __future__ import braces
11+
12+
13+
async def pascal_case(
14+
*,
15+
client: Client,
16+
path_param_1: str,
17+
query_param_1: str,
18+
) -> str:
19+
20+
""" GET endpoint """
21+
url = "{}/get/{pathParam1}".format(
22+
client.base_url,
23+
pathParam1=path_param_1,
24+
)
25+
26+
params: Dict[str, Any] = {
27+
"queryParam": query_param_1,
28+
}
29+
async with httpx.AsyncClient() as _client:
30+
response = await _client.get(
31+
url=url,
32+
headers=client.get_headers(),
33+
params=params,
34+
)
35+
36+
if response.status_code == 200:
37+
return str(response.text)
38+
else:
39+
raise ApiResponseError(response=response)
40+
41+
42+
async def camel_case(
43+
*,
44+
client: AuthenticatedClient,
45+
form_data: FormBody,
46+
multipart_data: MultiPartBody,
47+
json_body: Json,
48+
) -> Union[
49+
str,
50+
int,
51+
]:
52+
53+
""" POST endpoint """
54+
url = "{}/post/".format(
55+
client.base_url,
56+
)
57+
58+
async with httpx.AsyncClient() as _client:
59+
response = await _client.post(
60+
url=url,
61+
headers=client.get_headers(),
62+
data=asdict(form_data),
63+
files=multipart_data.to_dict(),
64+
json=json_body,
65+
)
66+
67+
if response.status_code == 200:
68+
return str(response.text)
69+
if response.status_code == 201:
70+
return int(response.text)
71+
else:
72+
raise ApiResponseError(response=response)

tests/test_templates/conftest.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
from jinja2 import Environment, PackageLoader
3+
4+
5+
@pytest.fixture(scope="session")
6+
def env() -> Environment:
7+
from openapi_python_client import utils
8+
9+
TEMPLATE_FILTERS = {"snakecase": utils.snake_case}
10+
env = Environment(loader=PackageLoader("openapi_python_client"), trim_blocks=True, lstrip_blocks=True)
11+
env.filters.update(TEMPLATE_FILTERS)
12+
return env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from jinja2 import Template
5+
6+
7+
@pytest.fixture(scope="session")
8+
def template(env) -> Template:
9+
return env.get_template("async_endpoint_module.pyi")
10+
11+
12+
def test_async_endpoint_module(template, mocker):
13+
path_param = mocker.MagicMock(python_name="path_param_1")
14+
path_param.name = "pathParam1"
15+
path_param.to_string.return_value = "path_param_1: str"
16+
query_param = mocker.MagicMock(template=None, python_name="query_param_1")
17+
query_param.name = "queryParam"
18+
query_param.to_string.return_value = "query_param_1: str"
19+
get_response = mocker.MagicMock(status_code=200)
20+
get_response.return_string.return_value = "str"
21+
get_response.constructor.return_value = "str(response.text)"
22+
get_endpoint = mocker.MagicMock(
23+
requires_security=False,
24+
path_parameters=[path_param],
25+
query_parameters=[query_param],
26+
form_body_reference=None,
27+
multipart_body_reference=None,
28+
json_body=None,
29+
responses=[get_response],
30+
description="GET endpoint",
31+
path="/get/{pathParam1}",
32+
method="get",
33+
)
34+
get_endpoint.name = "PascalCase"
35+
36+
form_body_reference = mocker.MagicMock(class_name="FormBody")
37+
multipart_body_reference = mocker.MagicMock(class_name="MultiPartBody")
38+
json_body = mocker.MagicMock(template=None, python_name="json_body")
39+
json_body.get_type_string.return_value = "Json"
40+
post_response_1 = mocker.MagicMock(status_code=200)
41+
post_response_1.return_string.return_value = "str"
42+
post_response_1.constructor.return_value = "str(response.text)"
43+
post_response_2 = mocker.MagicMock(status_code=201)
44+
post_response_2.return_string.return_value = "int"
45+
post_response_2.constructor.return_value = "int(response.text)"
46+
post_endpoint = mocker.MagicMock(
47+
name="camelCase",
48+
requires_security=True,
49+
path_parameters=[],
50+
query_parameters=[],
51+
form_body_reference=form_body_reference,
52+
multipart_body_reference=multipart_body_reference,
53+
json_body=json_body,
54+
responses=[post_response_1, post_response_2],
55+
description="POST endpoint",
56+
path="/post/",
57+
method="post",
58+
)
59+
post_endpoint.name = "camelCase"
60+
61+
collection = mocker.MagicMock(
62+
relative_imports=["import this", "from __future__ import braces"], endpoints=[get_endpoint, post_endpoint],
63+
)
64+
65+
result = template.render(collection=collection)
66+
import black
67+
expected = (Path(__file__).parent / "async_endpoint_module.py").read_text()
68+
black.assert_equivalent(result, expected)

0 commit comments

Comments
 (0)