MCPcopy Index your code
hub / github.com/fastapi/fastapi / test_crud_app

Function test_crud_app

tests/test_tutorial/test_sql_databases/test_tutorial001.py:53–146  ·  view source on GitHub ↗
(client: TestClient)

Source from the content-addressed store, hash-verified

51
52
53def test_crud_app(client: TestClient):
54 # TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
55 # this if using obj.model_validate becomes independent of Pydantic v2
56 with warnings.catch_warnings(record=True):
57 warnings.simplefilter("always")
58 # No heroes before creating
59 response = client.get("heroes/")
60 assert response.status_code == 200, response.text
61 assert response.json() == []
62
63 # Create a hero
64 response = client.post(
65 "/heroes/",
66 json={
67 "id": 999,
68 "name": "Dead Pond",
69 "age": 30,
70 "secret_name": "Dive Wilson",
71 },
72 )
73 assert response.status_code == 200, response.text
74 assert response.json() == snapshot(
75 {"age": 30, "secret_name": "Dive Wilson", "id": 999, "name": "Dead Pond"}
76 )
77
78 # Read a hero
79 hero_id = response.json()["id"]
80 response = client.get(f"/heroes/{hero_id}")
81 assert response.status_code == 200, response.text
82 assert response.json() == snapshot(
83 {"name": "Dead Pond", "age": 30, "id": 999, "secret_name": "Dive Wilson"}
84 )
85
86 # Read all heroes
87 # Create more heroes first
88 response = client.post(
89 "/heroes/",
90 json={"name": "Spider-Boy", "age": 18, "secret_name": "Pedro Parqueador"},
91 )
92 assert response.status_code == 200, response.text
93 response = client.post(
94 "/heroes/", json={"name": "Rusty-Man", "secret_name": "Tommy Sharp"}
95 )
96 assert response.status_code == 200, response.text
97
98 response = client.get("/heroes/")
99 assert response.status_code == 200, response.text
100 assert response.json() == snapshot(
101 [
102 {
103 "name": "Dead Pond",
104 "age": 30,
105 "id": IsInt(),
106 "secret_name": "Dive Wilson",
107 },
108 {
109 "name": "Spider-Boy",
110 "age": 18,

Callers

nothing calls this directly

Calls 3

getMethod · 0.45
postMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…