Skip to content

Commit f00f0de

Browse files
authored
✅ Refactor 2 tests, for consistency and simplification (fastapi#9504)
✅ Refactor tests, for consistency and simplification
1 parent 028e7ca commit f00f0de

2 files changed

Lines changed: 185 additions & 89 deletions

File tree

tests/test_request_body_parameters_media_type.py

Lines changed: 137 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,146 @@ async def create_shop(
3333
pass # pragma: no cover
3434

3535

36-
create_product_request_body = {
37-
"content": {
38-
"application/vnd.api+json": {
39-
"schema": {"$ref": "#/components/schemas/Body_create_product_products_post"}
40-
}
41-
},
42-
"required": True,
43-
}
44-
45-
create_shop_request_body = {
46-
"content": {
47-
"application/vnd.api+json": {
48-
"schema": {"$ref": "#/components/schemas/Body_create_shop_shops_post"}
49-
}
50-
},
51-
"required": True,
52-
}
53-
5436
client = TestClient(app)
5537

5638

5739
def test_openapi_schema():
5840
response = client.get("/openapi.json")
5941
assert response.status_code == 200, response.text
60-
openapi_schema = response.json()
61-
assert (
62-
openapi_schema["paths"]["/products"]["post"]["requestBody"]
63-
== create_product_request_body
64-
)
65-
assert (
66-
openapi_schema["paths"]["/shops"]["post"]["requestBody"]
67-
== create_shop_request_body
68-
)
42+
# insert_assert(response.json())
43+
assert response.json() == {
44+
"openapi": "3.0.2",
45+
"info": {"title": "FastAPI", "version": "0.1.0"},
46+
"paths": {
47+
"/products": {
48+
"post": {
49+
"summary": "Create Product",
50+
"operationId": "create_product_products_post",
51+
"requestBody": {
52+
"content": {
53+
"application/vnd.api+json": {
54+
"schema": {
55+
"$ref": "#/components/schemas/Body_create_product_products_post"
56+
}
57+
}
58+
},
59+
"required": True,
60+
},
61+
"responses": {
62+
"200": {
63+
"description": "Successful Response",
64+
"content": {"application/json": {"schema": {}}},
65+
},
66+
"422": {
67+
"description": "Validation Error",
68+
"content": {
69+
"application/json": {
70+
"schema": {
71+
"$ref": "#/components/schemas/HTTPValidationError"
72+
}
73+
}
74+
},
75+
},
76+
},
77+
}
78+
},
79+
"/shops": {
80+
"post": {
81+
"summary": "Create Shop",
82+
"operationId": "create_shop_shops_post",
83+
"requestBody": {
84+
"content": {
85+
"application/vnd.api+json": {
86+
"schema": {
87+
"$ref": "#/components/schemas/Body_create_shop_shops_post"
88+
}
89+
}
90+
},
91+
"required": True,
92+
},
93+
"responses": {
94+
"200": {
95+
"description": "Successful Response",
96+
"content": {"application/json": {"schema": {}}},
97+
},
98+
"422": {
99+
"description": "Validation Error",
100+
"content": {
101+
"application/json": {
102+
"schema": {
103+
"$ref": "#/components/schemas/HTTPValidationError"
104+
}
105+
}
106+
},
107+
},
108+
},
109+
}
110+
},
111+
},
112+
"components": {
113+
"schemas": {
114+
"Body_create_product_products_post": {
115+
"title": "Body_create_product_products_post",
116+
"required": ["data"],
117+
"type": "object",
118+
"properties": {"data": {"$ref": "#/components/schemas/Product"}},
119+
},
120+
"Body_create_shop_shops_post": {
121+
"title": "Body_create_shop_shops_post",
122+
"required": ["data"],
123+
"type": "object",
124+
"properties": {
125+
"data": {"$ref": "#/components/schemas/Shop"},
126+
"included": {
127+
"title": "Included",
128+
"type": "array",
129+
"items": {"$ref": "#/components/schemas/Product"},
130+
"default": [],
131+
},
132+
},
133+
},
134+
"HTTPValidationError": {
135+
"title": "HTTPValidationError",
136+
"type": "object",
137+
"properties": {
138+
"detail": {
139+
"title": "Detail",
140+
"type": "array",
141+
"items": {"$ref": "#/components/schemas/ValidationError"},
142+
}
143+
},
144+
},
145+
"Product": {
146+
"title": "Product",
147+
"required": ["name", "price"],
148+
"type": "object",
149+
"properties": {
150+
"name": {"title": "Name", "type": "string"},
151+
"price": {"title": "Price", "type": "number"},
152+
},
153+
},
154+
"Shop": {
155+
"title": "Shop",
156+
"required": ["name"],
157+
"type": "object",
158+
"properties": {"name": {"title": "Name", "type": "string"}},
159+
},
160+
"ValidationError": {
161+
"title": "ValidationError",
162+
"required": ["loc", "msg", "type"],
163+
"type": "object",
164+
"properties": {
165+
"loc": {
166+
"title": "Location",
167+
"type": "array",
168+
"items": {
169+
"anyOf": [{"type": "string"}, {"type": "integer"}]
170+
},
171+
},
172+
"msg": {"title": "Message", "type": "string"},
173+
"type": {"title": "Error Type", "type": "string"},
174+
},
175+
},
176+
}
177+
},
178+
}
Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,9 @@
1-
from copy import deepcopy
2-
31
from fastapi.testclient import TestClient
42

53
from docs_src.dataclasses.tutorial002 import app
64

75
client = TestClient(app)
86

9-
openapi_schema = {
10-
"openapi": "3.0.2",
11-
"info": {"title": "FastAPI", "version": "0.1.0"},
12-
"paths": {
13-
"/items/next": {
14-
"get": {
15-
"summary": "Read Next Item",
16-
"operationId": "read_next_item_items_next_get",
17-
"responses": {
18-
"200": {
19-
"description": "Successful Response",
20-
"content": {
21-
"application/json": {
22-
"schema": {"$ref": "#/components/schemas/Item"}
23-
}
24-
},
25-
}
26-
},
27-
}
28-
}
29-
},
30-
"components": {
31-
"schemas": {
32-
"Item": {
33-
"title": "Item",
34-
"required": ["name", "price"],
35-
"type": "object",
36-
"properties": {
37-
"name": {"title": "Name", "type": "string"},
38-
"price": {"title": "Price", "type": "number"},
39-
"tags": {
40-
"title": "Tags",
41-
"type": "array",
42-
"items": {"type": "string"},
43-
},
44-
"description": {"title": "Description", "type": "string"},
45-
"tax": {"title": "Tax", "type": "number"},
46-
},
47-
}
48-
}
49-
},
50-
}
51-
52-
53-
def test_openapi_schema():
54-
response = client.get("/openapi.json")
55-
assert response.status_code == 200
56-
# TODO: remove this once Pydantic 1.9 is released
57-
# Ref: https://github.com/pydantic/pydantic/pull/2557
58-
data = response.json()
59-
alternative_data1 = deepcopy(data)
60-
alternative_data2 = deepcopy(data)
61-
alternative_data1["components"]["schemas"]["Item"]["required"] = ["name", "price"]
62-
alternative_data2["components"]["schemas"]["Item"]["required"] = [
63-
"name",
64-
"price",
65-
"tags",
66-
]
67-
assert alternative_data1 == openapi_schema or alternative_data2 == openapi_schema
68-
697

708
def test_get_item():
719
response = client.get("/items/next")
@@ -77,3 +15,51 @@ def test_get_item():
7715
"tags": ["breater"],
7816
"tax": None,
7917
}
18+
19+
20+
def test_openapi_schema():
21+
response = client.get("/openapi.json")
22+
assert response.status_code == 200
23+
data = response.json()
24+
assert data == {
25+
"openapi": "3.0.2",
26+
"info": {"title": "FastAPI", "version": "0.1.0"},
27+
"paths": {
28+
"/items/next": {
29+
"get": {
30+
"summary": "Read Next Item",
31+
"operationId": "read_next_item_items_next_get",
32+
"responses": {
33+
"200": {
34+
"description": "Successful Response",
35+
"content": {
36+
"application/json": {
37+
"schema": {"$ref": "#/components/schemas/Item"}
38+
}
39+
},
40+
}
41+
},
42+
}
43+
}
44+
},
45+
"components": {
46+
"schemas": {
47+
"Item": {
48+
"title": "Item",
49+
"required": ["name", "price"],
50+
"type": "object",
51+
"properties": {
52+
"name": {"title": "Name", "type": "string"},
53+
"price": {"title": "Price", "type": "number"},
54+
"tags": {
55+
"title": "Tags",
56+
"type": "array",
57+
"items": {"type": "string"},
58+
},
59+
"description": {"title": "Description", "type": "string"},
60+
"tax": {"title": "Tax", "type": "number"},
61+
},
62+
}
63+
}
64+
},
65+
}

0 commit comments

Comments
 (0)