Debian Codegraph @

tests/test_tutorial/test_extra_models/test_tutorial003.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
18
19 client = TestClient(mod.app)
20 return client
21
22
23 def test_get_car(client: TestClient):
24 response = client.get("/items/item1")
25 assert response.status_code == 200, response.text
26 assert response.json() == {
27 "description": "All my friends drive a low rider",
28 "type": "car",
29 }
30
31
32 def test_get_plane(client: TestClient):
33 response = client.get("/items/item2")
34 assert response.status_code == 200, response.text
35 assert response.json() == {
36 "description": "Music is my aeroplane, it's my aeroplane",
37 "type": "plane",
38 "size": 5,
39 }
40
41
42 def test_openapi_schema(client: TestClient):
43 response = client.get("/openapi.json")
44 assert response.status_code == 200, response.text
45 assert response.json() == snapshot(
46 {
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/items/{item_id}": {
51 "get": {
52 "responses": {
53 "200": {
54 "description": "Successful Response",
55 "content": {
56 "application/json": {
57 "schema": {
58 "title": "Response Read Item Items Item Id Get",
59 "anyOf": [
60 {
61 "$ref": "#/components/schemas/PlaneItem"
62 },
63 {
64 "$ref": "#/components/schemas/CarItem"
65 },
66 ],
67 }
68 }
69 },
70 },
71 "422": {
72 "description": "Validation Error",
73 "content": {
74 "application/json": {
75 "schema": {
76 "$ref": "#/components/schemas/HTTPValidationError"
77 }
78 }
79 },
80 },
81 },
82 "summary": "Read Item",
83 "operationId": "read_item_items__item_id__get",
84 "parameters": [
85 {
86 "required": True,
87 "schema": {"title": "Item Id", "type": "string"},
88 "name": "item_id",
89 "in": "path",
90 }
91 ],
92 }
93 }
94 },
95 "components": {
96 "schemas": {
97 "PlaneItem": {
98 "title": "PlaneItem",
99 "required": ["description", "size"],
100 "type": "object",
101 "properties": {
102 "description": {"title": "Description", "type": "string"},
103 "type": {
104 "title": "Type",
105 "type": "string",
106 "default": "plane",
107 },
108 "size": {"title": "Size", "type": "integer"},
109 },
110 },
111 "CarItem": {
112 "title": "CarItem",
113 "required": ["description"],
114 "type": "object",
115 "properties": {
116 "description": {"title": "Description", "type": "string"},
117 "type": {
118 "title": "Type",
119 "type": "string",
120 "default": "car",
121 },
122 },
123 },
124 "ValidationError": {
125 "title": "ValidationError",
126 "required": ["loc", "msg", "type"],
127 "type": "object",
128 "properties": {
129 "ctx": {"title": "Context", "type": "object"},
130 "input": {"title": "Input"},
131 "loc": {
132 "title": "Location",
133 "type": "array",
134 "items": {
135 "anyOf": [{"type": "string"}, {"type": "integer"}]
136 },
137 },
138 "msg": {"title": "Message", "type": "string"},
139 "type": {"title": "Error Type", "type": "string"},
140 },
141 },
142 "HTTPValidationError": {
143 "title": "HTTPValidationError",
144 "type": "object",
145 "properties": {
146 "detail": {
147 "title": "Detail",
148 "type": "array",
149 "items": {
150 "$ref": "#/components/schemas/ValidationError"
151 },
152 }
153 },
154 },
155 }
156 },
157 }
158 )