Replace the legacy Session.query() API #17

Merged
alexander merged 11 commits from sqlalchemy-select into develop 2026-12-02 00:38:24 +01:00
Owner
  • Count rows with select() in the tests
  • Query with select() in velofy.console, Grid, Path, City, Restaurant, Customer, Pixel, and the forecasting models
  • Count with .scalar_one() in velofy.console.gridify
  • Request the db_session fixture in test_restaurants_with_db
  • Create the engine and the session on first use (scoped_session, like in Flask-SQLAlchemy)

Behavioral notes: select() does not deduplicate single entities from a JOIN; so, Grid.gridify() calls .unique() explicitly. Subqueries now run in one statement instead of two.

- Count rows with `select()` in the tests - Query with `select()` in `velofy.console`, `Grid`, `Path`, `City`, `Restaurant`, `Customer`, `Pixel`, and the forecasting models - Count with `.scalar_one()` in `velofy.console.gridify` - Request the `db_session` fixture in `test_restaurants_with_db` - Create the engine and the session on first use (`scoped_session`, like in Flask-SQLAlchemy) Behavioral notes: `select()` does not deduplicate single entities from a `JOIN`; so, `Grid.gridify()` calls `.unique()` explicitly. Subqueries now run in one statement instead of two.
`Session.query()` is the legacy API in SQLAlchemy 2.x.
`Session.query()` is the legacy API in SQLAlchemy 2.x.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

Unlike `Query`, `select()` does not deduplicate single entities from a `JOIN`;
so, `Grid.gridify()` calls `.unique()` explicitly.

The `addresses_mock` fixtures mock the new call chain.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

The addresses with restaurants are now selected with a subquery in one statement.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

The delivery addresses are now selected with a subquery in one statement.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

The delivery and pickup addresses are now selected with subqueries in one statement each.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

The addresses with restaurants are now selected with a subquery in one statement.

The `_restaurants_mock` fixture mocks the new call chain.
`Session.query()` is the legacy API in SQLAlchemy 2.x.

`.one_or_none()` expresses that the unique constraint
on the five filtered columns allows at most one cached `Forecast`.
A `COUNT` always returns exactly one row; `.scalar_one()`
is typed as `int` and is used for all other counts as well.
The test queries the database but relied on the
global `db.session` that the previous test had left behind.
Create the engine and the session on first use
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m47s
fc185fa3c0
The global `db.session` is now a `scoped_session`,
like, for example, in Flask-SQLAlchemy.

Importing the package no longer creates an engine;
so, the placeholder for the tests is obsolete.

The `db_session` fixture replaces `db.session` with `monkeypatch`
such that it is reset after each test.

Tests without the fixture get a placeholder that fails loudly
instead of opening a real session,
which would block the `DROP SCHEMA` at the end of the test run.
alexander merged commit 2b1dd6af35 into develop 2026-12-02 00:38:24 +01:00
alexander deleted branch sqlalchemy-select 2026-12-02 00:38:24 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
alexander/velofy!17
No description provided.