Debian Codegraph @

tests/test_tutorial/test_path_operation_configurations/test_tutorial005.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial005_py310", marks=needs_py310),
14 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(
18 f"docs_src.path_operation_configuration.{request.param}"
19 )
20
21 client = TestClient(mod.app)
22 return client
23
24
25 def test_query_params_str_validations(client: TestClient):
26 response = client.post("/items/", json={"name": "Foo", "price": 42})
27 assert response.status_code == 200, response.text
28 assert response.json() == {
29 "name": "Foo",
30 "price": 42,
31 "description": None,
32 "tax": None,
33 "tags": [],
34 }
35
36
37 def test_openapi_schema(client: TestClient):
38 response = client.get("/openapi.json")
39 assert response.status_code == 200, response.text
40 assert response.json() == snapshot(
41 {
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/items/": {
46 "post": {
47 "responses": {
48 "200": {
49 "description": "The created item",
50 "content": {
51 "application/json": {
52 "schema": {"$ref": "#/components/schemas/Item"}
53 }
54 },
55 },
56 "422": {
57 "description": "Validation Error",
58 "content": {
59 "application/json": {
60 "schema": {
61 "$ref": "#/components/schemas/HTTPValidationError"
62 }
63 }
64 },
65 },
66 },
67 "summary": "Create an item",
68 "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",
69 "operationId": "create_item_items__post",
70 "requestBody": {
71 "content": {
72 "application/json": {
73 "schema": {"$ref": "#/components/schemas/Item"}
74 }
75 },
76 "required": True,
77 },
78 }
79 }
80 },
81 "components": {
82 "schemas": {
83 "Item": {
84 "title": "Item",
85 "required": ["name", "price"],
86 "type": "object",
87 "properties": {
88 "name": {"title": "Name", "type": "string"},
89 "description": {
90 "title": "Description",
91 "anyOf": [{"type": "string"}, {"type": "null"}],
92 },
93 "price": {"title": "Price", "type": "number"},
94 "tax": {
95 "title": "Tax",
96 "anyOf": [{"type": "number"}, {"type": "null"}],
97 },
98 "tags": {
99 "title": "Tags",
100 "uniqueItems": True,
101 "type": "array",
102 "items": {"type": "string"},
103 "default": [],
104 },
105 },
106 },
107 "ValidationError": {
108 "title": "ValidationError",
109 "required": ["loc", "msg", "type"],
110 "type": "object",
111 "properties": {
112 "loc": {
113 "title": "Location",
114 "type": "array",
115 "items": {
116 "anyOf": [{"type": "string"}, {"type": "integer"}]
117 },
118 },
119 "msg": {"title": "Message", "type": "string"},
120 "type": {"title": "Error Type", "type": "string"},
121 "input": {"title": "Input"},
122 "ctx": {"title": "Context", "type": "object"},
123 },
124 },
125 "HTTPValidationError": {
126 "title": "HTTPValidationError",
127 "type": "object",
128 "properties": {
129 "detail": {
130 "title": "Detail",
131 "type": "array",
132 "items": {
133 "$ref": "#/components/schemas/ValidationError"
134 },
135 }
136 },
137 },
138 }
139 },
140 }
141 )