Debian Codegraph @

tests/test_tutorial/test_openapi_webhooks/test_tutorial001.py

raw
1 from fastapi.routing import APIRoute
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4
5 from docs_src.openapi_webhooks.tutorial001_py310 import app
6
7 client = TestClient(app)
8
9
10 def test_get():
11 response = client.get("/users/")
12 assert response.status_code == 200, response.text
13 assert response.json() == ["Rick", "Morty"]
14
15
16 def test_dummy_webhook():
17 # Just for coverage
18 route = app.webhooks.routes[0]
19 assert isinstance(route, APIRoute)
20 route.endpoint({})
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 "/users/": {
32 "get": {
33 "summary": "Read Users",
34 "operationId": "read_users_users__get",
35 "responses": {
36 "200": {
37 "description": "Successful Response",
38 "content": {"application/json": {"schema": {}}},
39 }
40 },
41 }
42 }
43 },
44 "webhooks": {
45 "new-subscription": {
46 "post": {
47 "summary": "New Subscription",
48 "description": "When a new user subscribes to your service we'll send you a POST request with this\ndata to the URL that you register for the event `new-subscription` in the dashboard.",
49 "operationId": "new_subscriptionnew_subscription_post",
50 "requestBody": {
51 "content": {
52 "application/json": {
53 "schema": {
54 "$ref": "#/components/schemas/Subscription"
55 }
56 }
57 },
58 "required": True,
59 },
60 "responses": {
61 "200": {
62 "description": "Successful Response",
63 "content": {"application/json": {"schema": {}}},
64 },
65 "422": {
66 "description": "Validation Error",
67 "content": {
68 "application/json": {
69 "schema": {
70 "$ref": "#/components/schemas/HTTPValidationError"
71 }
72 }
73 },
74 },
75 },
76 }
77 }
78 },
79 "components": {
80 "schemas": {
81 "HTTPValidationError": {
82 "properties": {
83 "detail": {
84 "items": {
85 "$ref": "#/components/schemas/ValidationError"
86 },
87 "type": "array",
88 "title": "Detail",
89 }
90 },
91 "type": "object",
92 "title": "HTTPValidationError",
93 },
94 "Subscription": {
95 "properties": {
96 "username": {"type": "string", "title": "Username"},
97 "monthly_fee": {"type": "number", "title": "Monthly Fee"},
98 "start_date": {
99 "type": "string",
100 "format": "date-time",
101 "title": "Start Date",
102 },
103 },
104 "type": "object",
105 "required": ["username", "monthly_fee", "start_date"],
106 "title": "Subscription",
107 },
108 "ValidationError": {
109 "properties": {
110 "ctx": {"title": "Context", "type": "object"},
111 "input": {"title": "Input"},
112 "loc": {
113 "items": {
114 "anyOf": [{"type": "string"}, {"type": "integer"}]
115 },
116 "type": "array",
117 "title": "Location",
118 },
119 "msg": {"type": "string", "title": "Message"},
120 "type": {"type": "string", "title": "Error Type"},
121 },
122 "type": "object",
123 "required": ["loc", "msg", "type"],
124 "title": "ValidationError",
125 },
126 }
127 },
128 }
129 )