MCPcopy
hub / github.com/fastapi/fastapi / test_crud_app

Function test_crud_app

tests/test_tutorial/test_sql_databases/test_tutorial002.py:53–150  ·  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": 9000,
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, "id": IsInt(), "name": "Dead Pond"}
76 )
77 assert response.json()["id"] != 9000, (
78 "The ID should be generated by the database"
79 )
80
81 # Read a hero
82 hero_id = response.json()["id"]
83 response = client.get(f"/heroes/{hero_id}")
84 assert response.status_code == 200, response.text
85 assert response.json() == snapshot(
86 {"name": "Dead Pond", "age": 30, "id": IsInt()}
87 )
88
89 # Read all heroes
90 # Create more heroes first
91 response = client.post(
92 "/heroes/",
93 json={"name": "Spider-Boy", "age": 18, "secret_name": "Pedro Parqueador"},
94 )
95 assert response.status_code == 200, response.text
96 response = client.post(
97 "/heroes/", json={"name": "Rusty-Man", "secret_name": "Tommy Sharp"}
98 )
99 assert response.status_code == 200, response.text
100
101 response = client.get("/heroes/")
102 assert response.status_code == 200, response.text
103 assert response.json() == snapshot(
104 [
105 {"name": "Dead Pond", "age": 30, "id": IsInt()},
106 {"name": "Spider-Boy", "age": 18, "id": IsInt()},
107 {"name": "Rusty-Man", "age": None, "id": IsInt()},
108 ]
109 )
110

Callers

nothing calls this directly

Calls 4

getMethod · 0.45
postMethod · 0.45
patchMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…