|
|
@ -1,5 +1,4 @@ |
|
|
import importlib |
|
|
import importlib |
|
|
import warnings |
|
|
|
|
|
|
|
|
|
|
|
import pytest |
|
|
import pytest |
|
|
from dirty_equals import IsInt |
|
|
from dirty_equals import IsInt |
|
|
@ -30,9 +29,6 @@ def clear_sqlmodel(): |
|
|
) |
|
|
) |
|
|
def get_client(request: pytest.FixtureRequest): |
|
|
def get_client(request: pytest.FixtureRequest): |
|
|
clear_sqlmodel() |
|
|
clear_sqlmodel() |
|
|
# TODO: remove when updating SQL tutorial to use new lifespan API |
|
|
|
|
|
with warnings.catch_warnings(record=True): |
|
|
|
|
|
warnings.simplefilter("always") |
|
|
|
|
|
mod = importlib.import_module(f"docs_src.sql_databases.{request.param}") |
|
|
mod = importlib.import_module(f"docs_src.sql_databases.{request.param}") |
|
|
clear_sqlmodel() |
|
|
clear_sqlmodel() |
|
|
importlib.reload(mod) |
|
|
importlib.reload(mod) |
|
|
@ -48,10 +44,6 @@ def get_client(request: pytest.FixtureRequest): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_crud_app(client: TestClient): |
|
|
def test_crud_app(client: TestClient): |
|
|
# TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor |
|
|
|
|
|
# this if using obj.model_validate becomes independent of Pydantic v2 |
|
|
|
|
|
with warnings.catch_warnings(record=True): |
|
|
|
|
|
warnings.simplefilter("always") |
|
|
|
|
|
# No heroes before creating |
|
|
# No heroes before creating |
|
|
response = client.get("heroes/") |
|
|
response = client.get("heroes/") |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.status_code == 200, response.text |
|
|
@ -68,20 +60,14 @@ def test_crud_app(client: TestClient): |
|
|
}, |
|
|
}, |
|
|
) |
|
|
) |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.json() == snapshot( |
|
|
assert response.json() == snapshot({"age": 30, "id": IsInt(), "name": "Dead Pond"}) |
|
|
{"age": 30, "id": IsInt(), "name": "Dead Pond"} |
|
|
assert response.json()["id"] != 9000, "The ID should be generated by the database" |
|
|
) |
|
|
|
|
|
assert response.json()["id"] != 9000, ( |
|
|
|
|
|
"The ID should be generated by the database" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Read a hero |
|
|
# Read a hero |
|
|
hero_id = response.json()["id"] |
|
|
hero_id = response.json()["id"] |
|
|
response = client.get(f"/heroes/{hero_id}") |
|
|
response = client.get(f"/heroes/{hero_id}") |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.status_code == 200, response.text |
|
|
assert response.json() == snapshot( |
|
|
assert response.json() == snapshot({"name": "Dead Pond", "age": 30, "id": IsInt()}) |
|
|
{"name": "Dead Pond", "age": 30, "id": IsInt()} |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
# Read all heroes |
|
|
# Read all heroes |
|
|
# Create more heroes first |
|
|
# Create more heroes first |
|
|
|