Coverage for tests/test_tutorial/test_handling_errors/test_tutorial001.py: 100%
16 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi.testclient import TestClient 1abcde
3from docs_src.handling_errors.tutorial001 import app 1abcde
5client = TestClient(app) 1abcde
8def test_get_item(): 1abcde
9 response = client.get("/items/foo") 1abcde
10 assert response.status_code == 200, response.text 1abcde
11 assert response.json() == {"item": "The Foo Wrestlers"} 1abcde
14def test_get_item_not_found(): 1abcde
15 response = client.get("/items/bar") 1abcde
16 assert response.status_code == 404, response.text 1abcde
17 assert response.headers.get("x-error") is None 1abcde
18 assert response.json() == {"detail": "Item not found"} 1abcde
21def test_openapi_schema(): 1abcde
22 response = client.get("/openapi.json") 1abcde
23 assert response.status_code == 200, response.text 1abcde
24 assert response.json() == { 1abcde
25 "openapi": "3.1.0",
26 "info": {"title": "FastAPI", "version": "0.1.0"},
27 "paths": {
28 "/items/{item_id}": {
29 "get": {
30 "responses": {
31 "200": {
32 "description": "Successful Response",
33 "content": {"application/json": {"schema": {}}},
34 },
35 "422": {
36 "description": "Validation Error",
37 "content": {
38 "application/json": {
39 "schema": {
40 "$ref": "#/components/schemas/HTTPValidationError"
41 }
42 }
43 },
44 },
45 },
46 "summary": "Read Item",
47 "operationId": "read_item_items__item_id__get",
48 "parameters": [
49 {
50 "required": True,
51 "schema": {"title": "Item Id", "type": "string"},
52 "name": "item_id",
53 "in": "path",
54 }
55 ],
56 }
57 }
58 },
59 "components": {
60 "schemas": {
61 "ValidationError": {
62 "title": "ValidationError",
63 "required": ["loc", "msg", "type"],
64 "type": "object",
65 "properties": {
66 "loc": {
67 "title": "Location",
68 "type": "array",
69 "items": {
70 "anyOf": [{"type": "string"}, {"type": "integer"}]
71 },
72 },
73 "msg": {"title": "Message", "type": "string"},
74 "type": {"title": "Error Type", "type": "string"},
75 },
76 },
77 "HTTPValidationError": {
78 "title": "HTTPValidationError",
79 "type": "object",
80 "properties": {
81 "detail": {
82 "title": "Detail",
83 "type": "array",
84 "items": {"$ref": "#/components/schemas/ValidationError"},
85 }
86 },
87 },
88 }
89 },
90 }