Coverage for tests / test_tutorial / test_path_operation_advanced_configurations / test_tutorial004.py: 100%
19 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("tutorial004_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module( 1abc
18 f"docs_src.path_operation_advanced_configuration.{request.param}"
19 )
21 client = TestClient(mod.app) 1abc
22 client.headers.clear() 1abc
23 return client 1abc
26def test_query_params_str_validations(client: TestClient): 1abdc
27 response = client.post("/items/", json={"name": "Foo", "price": 42}) 1efg
28 assert response.status_code == 200, response.text 1efg
29 assert response.json() == { 1efg
30 "name": "Foo",
31 "price": 42,
32 "description": None,
33 "tax": None,
34 "tags": [],
35 }
38def test_openapi_schema(client: TestClient): 1abdc
39 response = client.get("/openapi.json") 1hij
40 assert response.status_code == 200, response.text 1hij
41 assert response.json() == snapshot( 1hij
42 {
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/items/": {
47 "post": {
48 "responses": {
49 "200": {
50 "description": "Successful Response",
51 "content": {
52 "application/json": {
53 "schema": {"$ref": "#/components/schemas/Item"}
54 }
55 },
56 },
57 "422": {
58 "description": "Validation Error",
59 "content": {
60 "application/json": {
61 "schema": {
62 "$ref": "#/components/schemas/HTTPValidationError"
63 }
64 }
65 },
66 },
67 },
68 "summary": "Create an item",
69 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item",
70 "operationId": "create_item_items__post",
71 "requestBody": {
72 "content": {
73 "application/json": {
74 "schema": {"$ref": "#/components/schemas/Item"}
75 }
76 },
77 "required": True,
78 },
79 }
80 }
81 },
82 "components": {
83 "schemas": {
84 "Item": {
85 "title": "Item",
86 "required": ["name", "price"],
87 "type": "object",
88 "properties": {
89 "name": {"title": "Name", "type": "string"},
90 "description": {
91 "title": "Description",
92 "anyOf": [{"type": "string"}, {"type": "null"}],
93 },
94 "price": {"title": "Price", "type": "number"},
95 "tax": {
96 "title": "Tax",
97 "anyOf": [{"type": "number"}, {"type": "null"}],
98 },
99 "tags": {
100 "title": "Tags",
101 "uniqueItems": True,
102 "type": "array",
103 "items": {"type": "string"},
104 "default": [],
105 },
106 },
107 },
108 "ValidationError": {
109 "title": "ValidationError",
110 "required": ["loc", "msg", "type"],
111 "type": "object",
112 "properties": {
113 "loc": {
114 "title": "Location",
115 "type": "array",
116 "items": {
117 "anyOf": [{"type": "string"}, {"type": "integer"}]
118 },
119 },
120 "msg": {"title": "Message", "type": "string"},
121 "type": {"title": "Error Type", "type": "string"},
122 "input": {"title": "Input"},
123 "ctx": {"title": "Context", "type": "object"},
124 },
125 },
126 "HTTPValidationError": {
127 "title": "HTTPValidationError",
128 "type": "object",
129 "properties": {
130 "detail": {
131 "title": "Detail",
132 "type": "array",
133 "items": {
134 "$ref": "#/components/schemas/ValidationError"
135 },
136 }
137 },
138 },
139 }
140 },
141 }
142 )