Coverage for tests / test_tutorial / test_separate_openapi_schemas / test_tutorial002.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("tutorial002_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest) -> TestClient: 1abdc
17 mod = importlib.import_module(f"docs_src.separate_openapi_schemas.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23def test_create_item(client: TestClient) -> None: 1abdc
24 response = client.post("/items/", json={"name": "Foo"}) 1efg
25 assert response.status_code == 200, response.text 1efg
26 assert response.json() == {"name": "Foo", "description": None} 1efg
29def test_read_items(client: TestClient) -> None: 1abdc
30 response = client.get("/items/") 1hij
31 assert response.status_code == 200, response.text 1hij
32 assert response.json() == [ 1hij
33 {
34 "name": "Portal Gun",
35 "description": "Device to travel through the multi-rick-verse",
36 },
37 {"name": "Plumbus", "description": None},
38 ]
41def test_openapi_schema(client: TestClient) -> None: 1abdc
42 response = client.get("/openapi.json") 1klm
43 assert response.status_code == 200, response.text 1klm
44 assert response.json() == snapshot( 1klm
45 {
46 "openapi": "3.1.0",
47 "info": {"title": "FastAPI", "version": "0.1.0"},
48 "paths": {
49 "/items/": {
50 "get": {
51 "summary": "Read Items",
52 "operationId": "read_items_items__get",
53 "responses": {
54 "200": {
55 "description": "Successful Response",
56 "content": {
57 "application/json": {
58 "schema": {
59 "items": {
60 "$ref": "#/components/schemas/Item"
61 },
62 "type": "array",
63 "title": "Response Read Items Items Get",
64 }
65 }
66 },
67 }
68 },
69 },
70 "post": {
71 "summary": "Create Item",
72 "operationId": "create_item_items__post",
73 "requestBody": {
74 "content": {
75 "application/json": {
76 "schema": {"$ref": "#/components/schemas/Item"}
77 }
78 },
79 "required": True,
80 },
81 "responses": {
82 "200": {
83 "description": "Successful Response",
84 "content": {"application/json": {"schema": {}}},
85 },
86 "422": {
87 "description": "Validation Error",
88 "content": {
89 "application/json": {
90 "schema": {
91 "$ref": "#/components/schemas/HTTPValidationError"
92 }
93 }
94 },
95 },
96 },
97 },
98 }
99 },
100 "components": {
101 "schemas": {
102 "HTTPValidationError": {
103 "properties": {
104 "detail": {
105 "items": {
106 "$ref": "#/components/schemas/ValidationError"
107 },
108 "type": "array",
109 "title": "Detail",
110 }
111 },
112 "type": "object",
113 "title": "HTTPValidationError",
114 },
115 "Item": {
116 "properties": {
117 "name": {"type": "string", "title": "Name"},
118 "description": {
119 "anyOf": [{"type": "string"}, {"type": "null"}],
120 "title": "Description",
121 },
122 },
123 "type": "object",
124 "required": ["name"],
125 "title": "Item",
126 },
127 "ValidationError": {
128 "properties": {
129 "ctx": {"title": "Context", "type": "object"},
130 "input": {"title": "Input"},
131 "loc": {
132 "items": {
133 "anyOf": [{"type": "string"}, {"type": "integer"}]
134 },
135 "type": "array",
136 "title": "Location",
137 },
138 "msg": {"type": "string", "title": "Message"},
139 "type": {"type": "string", "title": "Error Type"},
140 },
141 "type": "object",
142 "required": ["loc", "msg", "type"],
143 "title": "ValidationError",
144 },
145 }
146 },
147 }
148 )