Debian Codegraph @

tests/test_tutorial/test_dataclasses/test_tutorial001.py

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