Coverage for tests / test_tutorial / test_path_params / test_tutorial002.py: 100%
16 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
1from fastapi.testclient import TestClient 1abcd
2from inline_snapshot import snapshot 1abcd
4from docs_src.path_params.tutorial002_py310 import app 1abcd
6client = TestClient(app) 1abcd
9def test_get_items(): 1abcd
10 response = client.get("/items/1") 1efg
11 assert response.status_code == 200, response.text 1efg
12 assert response.json() == {"item_id": 1} 1efg
15def test_get_items_invalid_id(): 1abcd
16 response = client.get("/items/item1") 1hij
17 assert response.status_code == 422, response.text 1hij
18 assert response.json() == { 1hij
19 "detail": [
20 {
21 "input": "item1",
22 "loc": ["path", "item_id"],
23 "msg": "Input should be a valid integer, unable to parse string as an integer",
24 "type": "int_parsing",
25 }
26 ]
27 }
30def test_openapi_schema(): 1abcd
31 response = client.get("/openapi.json") 1klm
32 assert response.status_code == 200, response.text 1klm
33 assert response.json() == snapshot( 1klm
34 {
35 "openapi": "3.1.0",
36 "info": {"title": "FastAPI", "version": "0.1.0"},
37 "paths": {
38 "/items/{item_id}": {
39 "get": {
40 "operationId": "read_item_items__item_id__get",
41 "parameters": [
42 {
43 "in": "path",
44 "name": "item_id",
45 "required": True,
46 "schema": {
47 "title": "Item Id",
48 "type": "integer",
49 },
50 },
51 ],
52 "responses": {
53 "200": {
54 "content": {
55 "application/json": {
56 "schema": {},
57 },
58 },
59 "description": "Successful Response",
60 },
61 "422": {
62 "content": {
63 "application/json": {
64 "schema": {
65 "$ref": "#/components/schemas/HTTPValidationError",
66 },
67 },
68 },
69 "description": "Validation Error",
70 },
71 },
72 "summary": "Read Item",
73 },
74 },
75 },
76 "components": {
77 "schemas": {
78 "HTTPValidationError": {
79 "properties": {
80 "detail": {
81 "items": {
82 "$ref": "#/components/schemas/ValidationError",
83 },
84 "title": "Detail",
85 "type": "array",
86 },
87 },
88 "title": "HTTPValidationError",
89 "type": "object",
90 },
91 "ValidationError": {
92 "properties": {
93 "ctx": {"title": "Context", "type": "object"},
94 "input": {"title": "Input"},
95 "loc": {
96 "items": {
97 "anyOf": [
98 {
99 "type": "string",
100 },
101 {
102 "type": "integer",
103 },
104 ],
105 },
106 "title": "Location",
107 "type": "array",
108 },
109 "msg": {
110 "title": "Message",
111 "type": "string",
112 },
113 "type": {
114 "title": "Error Type",
115 "type": "string",
116 },
117 },
118 "required": [
119 "loc",
120 "msg",
121 "type",
122 ],
123 "title": "ValidationError",
124 "type": "object",
125 },
126 },
127 },
128 }
129 )