Create the engine and the session on first use
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m47s

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.
This commit is contained in:
Alexander Hess 2026-12-02 00:19:19 +01:00
commit fc185fa3c0
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -0,0 +1,23 @@
"""Test the connection utils in `velofy.db.connection`."""
import sqlalchemy as sa
from velofy import config as pkg_config
from velofy.db import connection
def test_engine_uses_the_configured_database():
"""`get_engine()` connects to the `DATABASE_URI`."""
result = connection.get_engine()
assert result.url == sa.make_url(pkg_config.DATABASE_URI)
def test_session_is_bound_to_the_engine():
"""The `session` proxy creates sessions bound to `get_engine()`."""
session = connection.session()
try:
assert session.get_bind() is connection.get_engine()
finally:
connection.session.remove()