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