Coverage for tests / test_tutorial / test_extra_models / test_tutorial003.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.extra_models.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23def test_get_car(client: TestClient): 1abdc
24 response = client.get("/items/item1") 1efg
25 assert response.status_code == 200, response.text 1efg
26 assert response.json() == { 1efg
27 "description": "All my friends drive a low rider",
28 "type": "car",
29 }
32def test_get_plane(client: TestClient): 1abdc
33 response = client.get("/items/item2") 1hij
34 assert response.status_code == 200, response.text 1hij
35 assert response.json() == { 1hij
36 "description": "Music is my aeroplane, it's my aeroplane",
37 "type": "plane",
38 "size": 5,
39 }
42def test_openapi_schema(client: TestClient): 1abdc
43 response = client.get("/openapi.json") 1klm
44 assert response.status_code == 200, response.text 1klm
45 assert response.json() == snapshot( 1klm
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 )