Coverage for tests / test_tutorial / test_dataclasses / test_tutorial001.py: 100%
23 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 tests.utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial001_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.dataclasses_.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 client.headers.clear() 1abc
21 return client 1abc
24def test_post_item(client: TestClient): 1abdc
25 response = client.post("/items/", json={"name": "Foo", "price": 3}) 1efg
26 assert response.status_code == 200 1efg
27 assert response.json() == { 1efg
28 "name": "Foo",
29 "price": 3,
30 "description": None,
31 "tax": None,
32 }
35def test_post_invalid_item(client: TestClient): 1abdc
36 response = client.post("/items/", json={"name": "Foo", "price": "invalid price"}) 1hij
37 assert response.status_code == 422 1hij
38 assert response.json() == { 1hij
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 }
50def test_openapi_schema(client: TestClient): 1abdc
51 response = client.get("/openapi.json") 1klm
52 assert response.status_code == 200 1klm
53 assert response.json() == snapshot( 1klm
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 )