Debian Codegraph @

tests/test_tutorial/test_body_nested_models/test_tutorial008.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="client",
10 params=[
11 pytest.param("tutorial008_py310"),
12 ],
13 )
14 def get_client(request: pytest.FixtureRequest):
15 mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}")
16
17 client = TestClient(mod.app)
18 return client
19
20
21 def test_post_body(client: TestClient):
22 data = [
23 {"url": "http://example.com/", "name": "Example"},
24 {"url": "http://fastapi.tiangolo.com/", "name": "FastAPI"},
25 ]
26 response = client.post("/images/multiple", json=data)
27 assert response.status_code == 200, response.text
28 assert response.json() == data
29
30
31 def test_post_invalid_list_item(client: TestClient):
32 data = [{"url": "not a valid url", "name": "Example"}]
33 response = client.post("/images/multiple", json=data)
34 assert response.status_code == 422, response.text
35 assert response.json() == {
36 "detail": [
37 {
38 "loc": ["body", 0, "url"],
39 "input": "not a valid url",
40 "msg": "Input should be a valid URL, relative URL without a base",
41 "type": "url_parsing",
42 "ctx": {"error": "relative URL without a base"},
43 },
44 ]
45 }
46
47
48 def test_post_not_a_list(client: TestClient):
49 data = {"url": "http://example.com/", "name": "Example"}
50 response = client.post("/images/multiple", json=data)
51 assert response.status_code == 422, response.text
52 assert response.json() == {
53 "detail": [
54 {
55 "loc": ["body"],
56 "input": {
57 "name": "Example",
58 "url": "http://example.com/",
59 },
60 "msg": "Input should be a valid list",
61 "type": "list_type",
62 }
63 ]
64 }
65
66
67 def test_openapi_schema(client: TestClient):
68 response = client.get("/openapi.json")
69 assert response.status_code == 200, response.text
70 assert response.json() == snapshot(
71 {
72 "openapi": "3.1.0",
73 "info": {"title": "FastAPI", "version": "0.1.0"},
74 "paths": {
75 "/images/multiple/": {
76 "post": {
77 "responses": {
78 "200": {
79 "description": "Successful Response",
80 "content": {"application/json": {"schema": {}}},
81 },
82 "422": {
83 "description": "Validation Error",
84 "content": {
85 "application/json": {
86 "schema": {
87 "$ref": "#/components/schemas/HTTPValidationError"
88 }
89 }
90 },
91 },
92 },
93 "summary": "Create Multiple Images",
94 "operationId": "create_multiple_images_images_multiple__post",
95 "requestBody": {
96 "content": {
97 "application/json": {
98 "schema": {
99 "title": "Images",
100 "type": "array",
101 "items": {"$ref": "#/components/schemas/Image"},
102 }
103 }
104 },
105 "required": True,
106 },
107 }
108 }
109 },
110 "components": {
111 "schemas": {
112 "Image": {
113 "properties": {
114 "url": {
115 "title": "Url",
116 "type": "string",
117 "format": "uri",
118 "maxLength": 2083,
119 "minLength": 1,
120 },
121 "name": {
122 "title": "Name",
123 "type": "string",
124 },
125 },
126 "required": ["url", "name"],
127 "title": "Image",
128 "type": "object",
129 },
130 "ValidationError": {
131 "title": "ValidationError",
132 "required": ["loc", "msg", "type"],
133 "type": "object",
134 "properties": {
135 "loc": {
136 "title": "Location",
137 "type": "array",
138 "items": {
139 "anyOf": [{"type": "string"}, {"type": "integer"}]
140 },
141 },
142 "msg": {"title": "Message", "type": "string"},
143 "type": {"title": "Error Type", "type": "string"},
144 "input": {"title": "Input"},
145 "ctx": {"title": "Context", "type": "object"},
146 },
147 },
148 "HTTPValidationError": {
149 "title": "HTTPValidationError",
150 "type": "object",
151 "properties": {
152 "detail": {
153 "title": "Detail",
154 "type": "array",
155 "items": {
156 "$ref": "#/components/schemas/ValidationError"
157 },
158 }
159 },
160 },
161 }
162 },
163 }
164 )