Skip to content

Add example model step to FastAPI test setup#7

Merged
SupraSummus merged 4 commits intomainfrom
copilot/add-example-model-to-tests
Feb 6, 2026
Merged

Add example model step to FastAPI test setup#7
SupraSummus merged 4 commits intomainfrom
copilot/add-example-model-to-tests

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

Adds a new fastapi/35-example-model.sh step 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 an Example SQLAlchemy ORM model, async CRUD routes using Depends(get_db), a conftest.py with async DB fixtures (create_all/drop_all against real PostgreSQL), and async tests using httpx.AsyncClient
  • fastapi/test.sh — Sources the new step after everything.sh, before flake8/pytest run
  • .github/workflows/test.yaml — Passes DATABASE_URL env var for FastAPI tests to connect to the PostgreSQL service
  • NOT included in fastapi/everything.sh — exists solely for test validation of app extensibility
  • test_create_example POSTs via API, then verifies the row exists in PostgreSQL via select
  • test_list_examples inserts a row directly into the DB, then verifies it appears in the GET API response
  • All 4 tests pass against real PostgreSQL, flake8 clean
Original prompt

in fastapi etup create one extra step "example model" or something. do not include it in everything, but include it in tests setup. intention is to use it to tst that our app can easly be extendend with models and tested with that


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: SupraSummus <15822143+SupraSummus@users.noreply.github.com>
Copilot AI changed the title [WIP] Add example model for testing in FastAPI setup Add example model step to FastAPI test setup Feb 6, 2026
Copilot AI requested a review from SupraSummus February 6, 2026 15:13
Copy link
Member

@SupraSummus SupraSummus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 81 to 82
create_response = client.post("/examples", json={"name": "lookup test"})
item_id = create_response.json()["id"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup in this test should be replaced by directly creatng instance in db

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the POST setup with direct db insertion: _examples_db[999] = ExampleItem(id=999, name="lookup test"), then GET /examples/999. (13c846f)

cat >> "test_app.py" <<EOF


def test_create_example():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this test there should be assert that instnace exists in the db

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

router = APIRouter(prefix="/examples", tags=["examples"])

_examples_db: dict[int, "ExampleItem"] = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what? i want real postgres test, not some in-memory faking. please see how this is done in django scripts. @copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@SupraSummus SupraSummus marked this pull request as ready for review February 6, 2026 15:44
@SupraSummus SupraSummus merged commit 8bf3140 into main Feb 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants