Coverage for tests/test_tutorial/test_fastapi/test_simple_hero_api/test_tutorial001.py: 100%
37 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from dirty_equals import IsDict 1ghijkl
2from fastapi.testclient import TestClient 1ghijkl
3from sqlmodel import create_engine 1ghijkl
4from sqlmodel.pool import StaticPool 1ghijkl
7def test_tutorial(clear_sqlmodel): 1ghijkl
8 from docs_src.tutorial.fastapi.simple_hero_api import tutorial001 as mod 1fabcde
10 mod.sqlite_url = "sqlite://" 1fabcde
11 mod.engine = create_engine( 1fabcde
12 mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
13 )
15 with TestClient(mod.app) as client: 1fabcde
16 hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"} 1fabcde
17 hero2_data = { 1abcde
18 "name": "Spider-Boy",
19 "secret_name": "Pedro Parqueador",
20 "id": 9000,
21 }
22 response = client.post("/heroes/", json=hero1_data) 1fabcde
23 data = response.json() 1fabcde
25 assert response.status_code == 200, response.text 1fabcde
26 assert data["name"] == hero1_data["name"] 1fabcde
27 assert data["secret_name"] == hero1_data["secret_name"] 1fabcde
28 assert data["id"] is not None 1fabcde
29 assert data["age"] is None 1fabcde
31 response = client.post("/heroes/", json=hero2_data) 1fabcde
32 data = response.json() 1fabcde
34 assert response.status_code == 200, response.text 1fabcde
35 assert data["name"] == hero2_data["name"] 1fabcde
36 assert data["secret_name"] == hero2_data["secret_name"] 1fabcde
37 assert data["id"] == hero2_data["id"], ( 1fabcde
38 "Up to this point it's still possible to "
39 "set the ID of the hero in the request"
40 )
41 assert data["age"] is None 1fabcde
43 response = client.get("/heroes/") 1fabcde
44 data = response.json() 1fabcde
46 assert response.status_code == 200, response.text 1fabcde
47 assert len(data) == 2 1fabcde
48 assert data[0]["name"] == hero1_data["name"] 1fabcde
49 assert data[0]["secret_name"] == hero1_data["secret_name"] 1fabcde
50 assert data[1]["name"] == hero2_data["name"] 1fabcde
51 assert data[1]["secret_name"] == hero2_data["secret_name"] 1fabcde
52 assert data[1]["id"] == hero2_data["id"] 1fabcde
54 response = client.get("/openapi.json") 1fabcde
56 assert response.status_code == 200, response.text 1fabcde
58 assert response.json() == { 1fabcde
59 "openapi": "3.1.0",
60 "info": {"title": "FastAPI", "version": "0.1.0"},
61 "paths": {
62 "/heroes/": {
63 "get": {
64 "summary": "Read Heroes",
65 "operationId": "read_heroes_heroes__get",
66 "responses": {
67 "200": {
68 "description": "Successful Response",
69 "content": {"application/json": {"schema": {}}},
70 }
71 },
72 },
73 "post": {
74 "summary": "Create Hero",
75 "operationId": "create_hero_heroes__post",
76 "requestBody": {
77 "content": {
78 "application/json": {
79 "schema": {"$ref": "#/components/schemas/Hero"}
80 }
81 },
82 "required": True,
83 },
84 "responses": {
85 "200": {
86 "description": "Successful Response",
87 "content": {"application/json": {"schema": {}}},
88 },
89 "422": {
90 "description": "Validation Error",
91 "content": {
92 "application/json": {
93 "schema": {
94 "$ref": "#/components/schemas/HTTPValidationError"
95 }
96 }
97 },
98 },
99 },
100 },
101 }
102 },
103 "components": {
104 "schemas": {
105 "HTTPValidationError": {
106 "title": "HTTPValidationError",
107 "type": "object",
108 "properties": {
109 "detail": {
110 "title": "Detail",
111 "type": "array",
112 "items": {
113 "$ref": "#/components/schemas/ValidationError"
114 },
115 }
116 },
117 },
118 "Hero": {
119 "title": "Hero",
120 "required": ["name", "secret_name"],
121 "type": "object",
122 "properties": {
123 "id": IsDict(
124 {
125 "title": "Id",
126 "anyOf": [{"type": "integer"}, {"type": "null"}],
127 }
128 )
129 | IsDict(
130 # TODO: remove when deprecating Pydantic v1
131 {"title": "Id", "type": "integer"}
132 ),
133 "name": {"title": "Name", "type": "string"},
134 "secret_name": {"title": "Secret Name", "type": "string"},
135 "age": IsDict(
136 {
137 "title": "Age",
138 "anyOf": [{"type": "integer"}, {"type": "null"}],
139 }
140 )
141 | IsDict(
142 # TODO: remove when deprecating Pydantic v1
143 {"title": "Age", "type": "integer"}
144 ),
145 },
146 },
147 "ValidationError": {
148 "title": "ValidationError",
149 "required": ["loc", "msg", "type"],
150 "type": "object",
151 "properties": {
152 "loc": {
153 "title": "Location",
154 "type": "array",
155 "items": {
156 "anyOf": [{"type": "string"}, {"type": "integer"}]
157 },
158 },
159 "msg": {"title": "Message", "type": "string"},
160 "type": {"title": "Error Type", "type": "string"},
161 },
162 },
163 }
164 },
165 }