Coverage for tests / test_tutorial / test_openapi_callbacks / test_tutorial001.py: 100%
25 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
2from types import ModuleType 1abdc
4import pytest 1abdc
5from fastapi.testclient import TestClient 1abdc
6from inline_snapshot import snapshot 1abdc
8from tests.utils import needs_py310 1abdc
11@pytest.fixture( 1abdc
12 name="mod",
13 params=[
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 ],
16)
17def get_mod(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.openapi_callbacks.{request.param}") 1abc
19 return mod 1abc
22@pytest.fixture(name="client") 1abdc
23def get_client(mod: ModuleType): 1abdc
24 client = TestClient(mod.app) 1abc
25 client.headers.clear() 1abc
26 return client 1abc
29def test_get(client: TestClient): 1abdc
30 response = client.post( 1efg
31 "/invoices/", json={"id": "fooinvoice", "customer": "John", "total": 5.3}
32 )
33 assert response.status_code == 200, response.text 1efg
34 assert response.json() == {"msg": "Invoice received"} 1efg
37def test_dummy_callback(mod: ModuleType): 1abdc
38 # Just for coverage
39 mod.invoice_notification({}) 1klm
42def test_openapi_schema(client: TestClient): 1abdc
43 response = client.get("/openapi.json") 1hij
44 assert response.status_code == 200, response.text 1hij
45 assert response.json() == snapshot( 1hij
46 {
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/invoices/": {
51 "post": {
52 "summary": "Create Invoice",
53 "description": 'Create an invoice.\n\nThis will (let\'s imagine) let the API user (some external developer) create an\ninvoice.\n\nAnd this path operation will:\n\n* Send the invoice to the client.\n* Collect the money from the client.\n* Send a notification back to the API user (the external developer), as a callback.\n * At this point is that the API will somehow send a POST request to the\n external API with the notification of the invoice event\n (e.g. "payment successful").',
54 "operationId": "create_invoice_invoices__post",
55 "parameters": [
56 {
57 "required": False,
58 "schema": {
59 "anyOf": [
60 {
61 "type": "string",
62 "format": "uri",
63 "minLength": 1,
64 "maxLength": 2083,
65 },
66 {"type": "null"},
67 ],
68 "title": "Callback Url",
69 },
70 "name": "callback_url",
71 "in": "query",
72 }
73 ],
74 "requestBody": {
75 "content": {
76 "application/json": {
77 "schema": {"$ref": "#/components/schemas/Invoice"}
78 }
79 },
80 "required": True,
81 },
82 "responses": {
83 "200": {
84 "description": "Successful Response",
85 "content": {"application/json": {"schema": {}}},
86 },
87 "422": {
88 "description": "Validation Error",
89 "content": {
90 "application/json": {
91 "schema": {
92 "$ref": "#/components/schemas/HTTPValidationError"
93 }
94 }
95 },
96 },
97 },
98 "callbacks": {
99 "invoice_notification": {
100 "{$callback_url}/invoices/{$request.body.id}": {
101 "post": {
102 "summary": "Invoice Notification",
103 "operationId": "invoice_notification__callback_url__invoices___request_body_id__post",
104 "requestBody": {
105 "required": True,
106 "content": {
107 "application/json": {
108 "schema": {
109 "$ref": "#/components/schemas/InvoiceEvent"
110 }
111 }
112 },
113 },
114 "responses": {
115 "200": {
116 "description": "Successful Response",
117 "content": {
118 "application/json": {
119 "schema": {
120 "$ref": "#/components/schemas/InvoiceEventReceived"
121 }
122 }
123 },
124 },
125 "422": {
126 "description": "Validation Error",
127 "content": {
128 "application/json": {
129 "schema": {
130 "$ref": "#/components/schemas/HTTPValidationError"
131 }
132 }
133 },
134 },
135 },
136 }
137 }
138 }
139 },
140 }
141 }
142 },
143 "components": {
144 "schemas": {
145 "HTTPValidationError": {
146 "title": "HTTPValidationError",
147 "type": "object",
148 "properties": {
149 "detail": {
150 "title": "Detail",
151 "type": "array",
152 "items": {
153 "$ref": "#/components/schemas/ValidationError"
154 },
155 }
156 },
157 },
158 "Invoice": {
159 "title": "Invoice",
160 "required": ["id", "customer", "total"],
161 "type": "object",
162 "properties": {
163 "id": {"title": "Id", "type": "string"},
164 "title": {
165 "title": "Title",
166 "anyOf": [{"type": "string"}, {"type": "null"}],
167 },
168 "customer": {"title": "Customer", "type": "string"},
169 "total": {"title": "Total", "type": "number"},
170 },
171 },
172 "InvoiceEvent": {
173 "title": "InvoiceEvent",
174 "required": ["description", "paid"],
175 "type": "object",
176 "properties": {
177 "description": {"title": "Description", "type": "string"},
178 "paid": {"title": "Paid", "type": "boolean"},
179 },
180 },
181 "InvoiceEventReceived": {
182 "title": "InvoiceEventReceived",
183 "required": ["ok"],
184 "type": "object",
185 "properties": {"ok": {"title": "Ok", "type": "boolean"}},
186 },
187 "ValidationError": {
188 "title": "ValidationError",
189 "required": ["loc", "msg", "type"],
190 "type": "object",
191 "properties": {
192 "loc": {
193 "title": "Location",
194 "type": "array",
195 "items": {
196 "anyOf": [{"type": "string"}, {"type": "integer"}]
197 },
198 },
199 "msg": {"title": "Message", "type": "string"},
200 "type": {"title": "Error Type", "type": "string"},
201 "input": {"title": "Input"},
202 "ctx": {"title": "Context", "type": "object"},
203 },
204 },
205 }
206 },
207 }
208 )