Debian Codegraph @

tests/test_tutorial/test_extra_models/test_tutorial001_tutorial002.py

raw
1 import importlib
2
3 import pytest
4 from dirty_equals import IsList
5 from fastapi.testclient import TestClient
6 from inline_snapshot import snapshot
7
8 from ...utils import needs_py310
9
10
11 @pytest.fixture(
12 name="client",
13 params=[
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 pytest.param("tutorial002_py310", marks=needs_py310),
16 ],
17 )
18 def get_client(request: pytest.FixtureRequest):
19 mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
20
21 client = TestClient(mod.app)
22 return client
23
24
25 def test_post(client: TestClient):
26 response = client.post(
27 "/user/",
28 json={
29 "username": "johndoe",
30 "password": "secret",
31 "email": "[email protected]",
32 "full_name": "John Doe",
33 },
34 )
35 assert response.status_code == 200, response.text
36 assert response.json() == {
37 "username": "johndoe",
38 "email": "[email protected]",
39 "full_name": "John Doe",
40 }
41
42
43 def test_openapi_schema(client: TestClient):
44 response = client.get("/openapi.json")
45 assert response.status_code == 200, response.text
46 assert response.json() == snapshot(
47 {
48 "openapi": "3.1.0",
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "paths": {
51 "/user/": {
52 "post": {
53 "responses": {
54 "200": {
55 "description": "Successful Response",
56 "content": {
57 "application/json": {
58 "schema": {
59 "$ref": "#/components/schemas/UserOut",
60 }
61 }
62 },
63 },
64 "422": {
65 "description": "Validation Error",
66 "content": {
67 "application/json": {
68 "schema": {
69 "$ref": "#/components/schemas/HTTPValidationError"
70 }
71 }
72 },
73 },
74 },
75 "summary": "Create User",
76 "operationId": "create_user_user__post",
77 "requestBody": {
78 "content": {
79 "application/json": {
80 "schema": {"$ref": "#/components/schemas/UserIn"}
81 }
82 },
83 "required": True,
84 },
85 }
86 }
87 },
88 "components": {
89 "schemas": {
90 "UserIn": {
91 "title": "UserIn",
92 "required": IsList(
93 "username", "password", "email", check_order=False
94 ),
95 "type": "object",
96 "properties": {
97 "username": {"title": "Username", "type": "string"},
98 "password": {"title": "Password", "type": "string"},
99 "email": {
100 "title": "Email",
101 "type": "string",
102 "format": "email",
103 },
104 "full_name": {
105 "title": "Full Name",
106 "anyOf": [{"type": "string"}, {"type": "null"}],
107 },
108 },
109 },
110 "UserOut": {
111 "title": "UserOut",
112 "required": ["username", "email"],
113 "type": "object",
114 "properties": {
115 "username": {"title": "Username", "type": "string"},
116 "email": {
117 "title": "Email",
118 "type": "string",
119 "format": "email",
120 },
121 "full_name": {
122 "title": "Full Name",
123 "anyOf": [{"type": "string"}, {"type": "null"}],
124 },
125 },
126 },
127 "ValidationError": {
128 "title": "ValidationError",
129 "required": ["loc", "msg", "type"],
130 "type": "object",
131 "properties": {
132 "loc": {
133 "title": "Location",
134 "type": "array",
135 "items": {
136 "anyOf": [{"type": "string"}, {"type": "integer"}]
137 },
138 },
139 "msg": {"title": "Message", "type": "string"},
140 "type": {"title": "Error Type", "type": "string"},
141 "input": {"title": "Input"},
142 "ctx": {"title": "Context", "type": "object"},
143 },
144 },
145 "HTTPValidationError": {
146 "title": "HTTPValidationError",
147 "type": "object",
148 "properties": {
149 "detail": {
150 "title": "Detail",
151 "type": "array",
152 "items": {
153 "$ref": "#/components/schemas/ValidationError"
154 },
155 }
156 },
157 },
158 }
159 },
160 }
161 )