Skip to content

Commit b2b11d4

Browse files
committed
test: add tests for default responses
1 parent 0ff1d4e commit b2b11d4

5 files changed

Lines changed: 226 additions & 1 deletion

File tree

end_to_end_tests/baseline_openapi_3.0.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,37 @@
961961
}
962962
}
963963
},
964+
"/responses/default": {
965+
"post": {
966+
"tags": [
967+
"responses"
968+
],
969+
"summary": "Default Response",
970+
"operationId": "default_response",
971+
"responses": {
972+
"200": {
973+
"description": "Text response",
974+
"content": {
975+
"text/plain": {
976+
"schema": {
977+
"type": "string"
978+
}
979+
}
980+
}
981+
},
982+
"default": {
983+
"description": "Default response",
984+
"content": {
985+
"text/plain": {
986+
"schema": {
987+
"type": "string"
988+
}
989+
}
990+
}
991+
}
992+
}
993+
}
994+
},
964995
"/responses/reference": {
965996
"get": {
966997
"tags": [

end_to_end_tests/baseline_openapi_3.1.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,37 @@ info:
944944
}
945945
}
946946
},
947+
"/responses/default": {
948+
"post": {
949+
"tags": [
950+
"responses"
951+
],
952+
"summary": "Default Response",
953+
"operationId": "default_response",
954+
"responses": {
955+
"200": {
956+
"description": "Text response",
957+
"content": {
958+
"text/plain": {
959+
"schema": {
960+
"type": "string"
961+
}
962+
}
963+
}
964+
},
965+
"default": {
966+
"description": "Default response",
967+
"content": {
968+
"text/plain": {
969+
"schema": {
970+
"type": "string"
971+
}
972+
}
973+
}
974+
}
975+
}
976+
}
977+
},
947978
"/responses/reference": {
948979
"get": {
949980
"tags": [

end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/responses/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import types
44

5-
from . import post_responses_unions_simple_before_complex, reference_response, text_response
5+
from . import default_response, post_responses_unions_simple_before_complex, reference_response, text_response
66

77

88
class ResponsesEndpoints:
@@ -20,6 +20,13 @@ def text_response(cls) -> types.ModuleType:
2020
"""
2121
return text_response
2222

23+
@classmethod
24+
def default_response(cls) -> types.ModuleType:
25+
"""
26+
Default Response
27+
"""
28+
return default_response
29+
2330
@classmethod
2431
def reference_response(cls) -> types.ModuleType:
2532
"""

end_to_end_tests/functional_tests/generated_code_execution/test_defaults.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import uuid
3+
from typing import Union
34

45
from end_to_end_tests.functional_tests.helpers import (
56
with_generated_client_fixture,
@@ -112,3 +113,41 @@ def test_enum_default(self, MyEnum, MyModel):
112113
class TestLiteralEnumDefaults:
113114
def test_default_value(self, MyModel):
114115
assert MyModel().enum_prop == "A"
116+
117+
@with_generated_client_fixture(
118+
"""
119+
tags:
120+
- name: service1
121+
paths:
122+
"/simple":
123+
get:
124+
operationId: getSimpleThing
125+
description: Get a simple thing.
126+
responses:
127+
"200":
128+
description: Success!
129+
content:
130+
application/json:
131+
schema:
132+
$ref: "#/components/schemas/GoodResponse"
133+
default:
134+
description: Failure!
135+
content:
136+
application/json:
137+
schema:
138+
$ref: "#/components/schemas/ErrorResponse"
139+
tags:
140+
- service1
141+
components:
142+
schemas:
143+
GoodResponse:
144+
type: object
145+
ErrorResponse:
146+
type: object
147+
""",
148+
config="literal_enums: true",
149+
)
150+
@with_generated_code_imports(".api.service1.get_simple_thing._parse_response", ".models.GoodResponse", ".models.ErrorResponse")
151+
class TestDefaultResponseCode:
152+
def test_default_response(self, _parse_response, GoodResponse, ErrorResponse):
153+
assert _parse_response.__annotations__['return'] == Union[GoodResponse, ErrorResponse]
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from http import HTTPStatus
2+
from typing import Any, Optional, Union
3+
4+
import httpx
5+
6+
from ...client import AuthenticatedClient, Client
7+
from ...types import Response
8+
9+
10+
def _get_kwargs() -> dict[str, Any]:
11+
_kwargs: dict[str, Any] = {
12+
"method": "post",
13+
"url": "/responses/default",
14+
}
15+
16+
return _kwargs
17+
18+
19+
def _parse_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> str:
20+
if response.status_code == 200:
21+
response_200 = response.text
22+
return response_200
23+
response_default = response.text
24+
return response_default
25+
26+
27+
def _build_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Response[str]:
28+
return Response(
29+
status_code=HTTPStatus(response.status_code),
30+
content=response.content,
31+
headers=response.headers,
32+
parsed=_parse_response(client=client, response=response),
33+
)
34+
35+
36+
def sync_detailed(
37+
*,
38+
client: Union[AuthenticatedClient, Client],
39+
) -> Response[str]:
40+
"""Default Response
41+
42+
Raises:
43+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
44+
httpx.TimeoutException: If the request takes longer than Client.timeout.
45+
46+
Returns:
47+
Response[str]
48+
"""
49+
50+
kwargs = _get_kwargs()
51+
52+
response = client.get_httpx_client().request(
53+
**kwargs,
54+
)
55+
56+
return _build_response(client=client, response=response)
57+
58+
59+
def sync(
60+
*,
61+
client: Union[AuthenticatedClient, Client],
62+
) -> Optional[str]:
63+
"""Default Response
64+
65+
Raises:
66+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
67+
httpx.TimeoutException: If the request takes longer than Client.timeout.
68+
69+
Returns:
70+
str
71+
"""
72+
73+
return sync_detailed(
74+
client=client,
75+
).parsed
76+
77+
78+
async def asyncio_detailed(
79+
*,
80+
client: Union[AuthenticatedClient, Client],
81+
) -> Response[str]:
82+
"""Default Response
83+
84+
Raises:
85+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
86+
httpx.TimeoutException: If the request takes longer than Client.timeout.
87+
88+
Returns:
89+
Response[str]
90+
"""
91+
92+
kwargs = _get_kwargs()
93+
94+
response = await client.get_async_httpx_client().request(**kwargs)
95+
96+
return _build_response(client=client, response=response)
97+
98+
99+
async def asyncio(
100+
*,
101+
client: Union[AuthenticatedClient, Client],
102+
) -> Optional[str]:
103+
"""Default Response
104+
105+
Raises:
106+
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
107+
httpx.TimeoutException: If the request takes longer than Client.timeout.
108+
109+
Returns:
110+
str
111+
"""
112+
113+
return (
114+
await asyncio_detailed(
115+
client=client,
116+
)
117+
).parsed

0 commit comments

Comments
 (0)