Debian Codegraph @

tests/test_tutorial/test_handling_errors/test_tutorial003.py

raw
1 from fastapi.testclient import TestClient
2 from inline_snapshot import snapshot
3
4 from docs_src.handling_errors.tutorial003_py310 import app
5
6 client = TestClient(app)
7
8
9 def test_get():
10 response = client.get("/unicorns/shinny")
11 assert response.status_code == 200, response.text
12 assert response.json() == {"unicorn_name": "shinny"}
13
14
15 def test_get_exception():
16 response = client.get("/unicorns/yolo")
17 assert response.status_code == 418, response.text
18 assert response.json() == {
19 "message": "Oops! yolo did something. There goes a rainbow..."
20 }
21
22
23 def test_openapi_schema():
24 response = client.get("/openapi.json")
25 assert response.status_code == 200, response.text
26 assert response.json() == snapshot(
27 {
28 "openapi": "3.1.0",
29 "info": {"title": "FastAPI", "version": "0.1.0"},
30 "paths": {
31 "/unicorns/{name}": {
32 "get": {
33 "responses": {
34 "200": {
35 "description": "Successful Response",
36 "content": {"application/json": {"schema": {}}},
37 },
38 "422": {
39 "description": "Validation Error",
40 "content": {
41 "application/json": {
42 "schema": {
43 "$ref": "#/components/schemas/HTTPValidationError"
44 }
45 }
46 },
47 },
48 },
49 "summary": "Read Unicorn",
50 "operationId": "read_unicorn_unicorns__name__get",
51 "parameters": [
52 {
53 "required": True,
54 "schema": {"title": "Name", "type": "string"},
55 "name": "name",
56 "in": "path",
57 }
58 ],
59 }
60 }
61 },
62 "components": {
63 "schemas": {
64 "ValidationError": {
65 "title": "ValidationError",
66 "required": ["loc", "msg", "type"],
67 "type": "object",
68 "properties": {
69 "loc": {
70 "title": "Location",
71 "type": "array",
72 "items": {
73 "anyOf": [{"type": "string"}, {"type": "integer"}]
74 },
75 },
76 "msg": {"title": "Message", "type": "string"},
77 "type": {"title": "Error Type", "type": "string"},
78 "input": {"title": "Input"},
79 "ctx": {"title": "Context", "type": "object"},
80 },
81 },
82 "HTTPValidationError": {
83 "title": "HTTPValidationError",
84 "type": "object",
85 "properties": {
86 "detail": {
87 "title": "Detail",
88 "type": "array",
89 "items": {
90 "$ref": "#/components/schemas/ValidationError"
91 },
92 }
93 },
94 },
95 }
96 },
97 }
98 )