Add example model step to FastAPI test setup#7
Conversation
Co-authored-by: SupraSummus <15822143+SupraSummus@users.noreply.github.com>
fastapi/35-example-model.sh
Outdated
| create_response = client.post("/examples", json={"name": "lookup test"}) | ||
| item_id = create_response.json()["id"] |
There was a problem hiding this comment.
setup in this test should be replaced by directly creatng instance in db
There was a problem hiding this comment.
Replaced the POST setup with direct db insertion: _examples_db[999] = ExampleItem(id=999, name="lookup test"), then GET /examples/999. (13c846f)
fastapi/35-example-model.sh
Outdated
| cat >> "test_app.py" <<EOF | ||
|
|
||
|
|
||
| def test_create_example(): |
There was a problem hiding this comment.
in this test there should be assert that instnace exists in the db
There was a problem hiding this comment.
Added assert data["id"] in _examples_db to verify the created instance exists in the db after POST. (13c846f)
Co-authored-by: SupraSummus <15822143+SupraSummus@users.noreply.github.com>
fastapi/35-example-model.sh
Outdated
|
|
||
| router = APIRouter(prefix="/examples", tags=["examples"]) | ||
|
|
||
| _examples_db: dict[int, "ExampleItem"] = {} |
There was a problem hiding this comment.
what? i want real postgres test, not some in-memory faking. please see how this is done in django scripts. @copilot
There was a problem hiding this comment.
Rewrote the entire step to use real PostgreSQL with SQLAlchemy. The example model is now a proper Example(Base) ORM model with async CRUD routes using Depends(get_db). The conftest creates/drops tables in a real PostgreSQL database, and tests verify data directly via select queries. Also updated the CI workflow to pass DATABASE_URL for the FastAPI tests. (a1e8532)
Co-authored-by: SupraSummus <15822143+SupraSummus@users.noreply.github.com>
Adds a new
fastapi/35-example-model.shstep that creates a real SQLAlchemy model with async CRUD routes tested against PostgreSQL, demonstrating that the scaffolded app can easily be extended with new models.fastapi/35-example-model.sh— Creates anExampleSQLAlchemy ORM model, async CRUD routes usingDepends(get_db), aconftest.pywith async DB fixtures (create_all/drop_allagainst real PostgreSQL), and async tests usinghttpx.AsyncClientfastapi/test.sh— Sources the new step aftereverything.sh, before flake8/pytest run.github/workflows/test.yaml— PassesDATABASE_URLenv var for FastAPI tests to connect to the PostgreSQL servicefastapi/everything.sh— exists solely for test validation of app extensibilitytest_create_examplePOSTs via API, then verifies the row exists in PostgreSQL viaselecttest_list_examplesinserts a row directly into the DB, then verifies it appears in the GET API responseOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.