Coverage for tests / test_tutorial / test_additional_responses / test_tutorial002.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
2import os 1abcd
3import shutil 1abcd
5import pytest 1abcd
6from fastapi.testclient import TestClient 1abcd
7from inline_snapshot import snapshot 1abcd
9from tests.utils import needs_py310, workdir_lock 1abcd
12@pytest.fixture( 1abcd
13 name="client",
14 params=[
15 pytest.param("tutorial002_py310", marks=needs_py310),
16 ],
17)
18def get_client(request: pytest.FixtureRequest): 1abcd
19 mod = importlib.import_module(f"docs_src.additional_responses.{request.param}") 1ijklmnopqrs
21 client = TestClient(mod.app) 1ijklmnopqrs
22 client.headers.clear() 1ijklmnopqrs
23 return client 1ijklmnopqrs
26def test_path_operation(client: TestClient): 1abcd
27 response = client.get("/items/foo") 1tuvw
28 assert response.status_code == 200, response.text 1tuvw
29 assert response.json() == {"id": "foo", "value": "there goes my hero"} 1tuvw
32@workdir_lock 1abcd
33def test_path_operation_img(client: TestClient): 1abcd
34 shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") 1efgh
35 response = client.get("/items/foo?img=1") 1efgh
36 assert response.status_code == 200, response.text 1efgh
37 assert response.headers["Content-Type"] == "image/png" 1efgh
38 assert len(response.content) 1efgh
39 os.remove("./image.png") 1efgh
42def test_openapi_schema(client: TestClient): 1abcd
43 response = client.get("/openapi.json") 1xyzA
44 assert response.status_code == 200, response.text 1xyzA
45 assert response.json() == snapshot( 1xyzA
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": "Return the JSON item or an image.",
55 "content": {
56 "image/png": {},
57 "application/json": {
58 "schema": {"$ref": "#/components/schemas/Item"}
59 },
60 },
61 },
62 "422": {
63 "description": "Validation Error",
64 "content": {
65 "application/json": {
66 "schema": {
67 "$ref": "#/components/schemas/HTTPValidationError"
68 }
69 }
70 },
71 },
72 },
73 "summary": "Read Item",
74 "operationId": "read_item_items__item_id__get",
75 "parameters": [
76 {
77 "required": True,
78 "schema": {"title": "Item Id", "type": "string"},
79 "name": "item_id",
80 "in": "path",
81 },
82 {
83 "required": False,
84 "schema": {
85 "anyOf": [{"type": "boolean"}, {"type": "null"}],
86 "title": "Img",
87 },
88 "name": "img",
89 "in": "query",
90 },
91 ],
92 }
93 }
94 },
95 "components": {
96 "schemas": {
97 "Item": {
98 "title": "Item",
99 "required": ["id", "value"],
100 "type": "object",
101 "properties": {
102 "id": {"title": "Id", "type": "string"},
103 "value": {"title": "Value", "type": "string"},
104 },
105 },
106 "ValidationError": {
107 "title": "ValidationError",
108 "required": ["loc", "msg", "type"],
109 "type": "object",
110 "properties": {
111 "loc": {
112 "title": "Location",
113 "type": "array",
114 "items": {
115 "anyOf": [{"type": "string"}, {"type": "integer"}]
116 },
117 },
118 "msg": {"title": "Message", "type": "string"},
119 "type": {"title": "Error Type", "type": "string"},
120 "input": {"title": "Input"},
121 "ctx": {"title": "Context", "type": "object"},
122 },
123 },
124 "HTTPValidationError": {
125 "title": "HTTPValidationError",
126 "type": "object",
127 "properties": {
128 "detail": {
129 "title": "Detail",
130 "type": "array",
131 "items": {
132 "$ref": "#/components/schemas/ValidationError"
133 },
134 }
135 },
136 },
137 }
138 },
139 }
140 )