From 87aaebe475b52050ef18ebe02dc1f24af344d03c Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 10:23:39 +0100 Subject: [PATCH 1/4] Join the `session` into the outer `transaction` With "create_savepoint", commits inside a test case only release a savepoint, so the final rollback no longer warns --- tests/conftest.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 276199a..e09637c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,6 @@ in the CLI layer need access to the database. """ import os -import warnings import pytest import sqlalchemy as sa @@ -92,7 +91,8 @@ def db_session(db_connection): # Create a session bound to the same connection as the `transaction`. # Using any other session would not result in the roll back. - session = orm.sessionmaker()(bind=db_connection) + # Commits inside a test case then only release a savepoint. + session = orm.Session(bind=db_connection, join_transaction_mode="create_savepoint") # Monkey patch the package's global `session` object, # which is used heavily in the code base. @@ -104,8 +104,7 @@ def db_session(db_connection): finally: session.close() - with warnings.catch_warnings(record=True): - transaction.rollback() + transaction.rollback() # Import the fixtures from the `fake_data` sub-package. From ee5a372b2bf73958e3088a1e02d2192b50f06988 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 10:40:27 +0100 Subject: [PATCH 2/4] Link new objects from the parent Since SQLAlchemy 2.0, only the parent's end cascades a new object into the session' --- src/velofy/db/grids.py | 8 ++++++-- tests/db/fake_data/fixture_makers.py | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/velofy/db/grids.py b/src/velofy/db/grids.py index f035174..dae6013 100644 --- a/src/velofy/db/grids.py +++ b/src/velofy/db/grids.py @@ -74,7 +74,9 @@ class Grid(meta.Base): Returns: grid: including `grid.pixels` with the associated `city.addresses` """ - grid = cls(city=city, side_length=side_length) + grid = cls(side_length=side_length) + + city.grids.append(grid) # `Pixel`s grouped by `.n_x`-`.n_y` coordinates. pixels = {} @@ -102,7 +104,9 @@ class Grid(meta.Base): n_x, n_y = address.x // side_length, address.y // side_length # ... and create a new `Pixel` object if necessary. if (n_x, n_y) not in pixels: - pixels[(n_x, n_y)] = db.Pixel(grid=grid, n_x=n_x, n_y=n_y) + pixels[(n_x, n_y)] = db.Pixel(n_x=n_x, n_y=n_y) + grid.pixels.append(pixels[(n_x, n_y)]) + pixel = pixels[(n_x, n_y)] # Create an association between the `address` and `pixel`. diff --git a/tests/db/fake_data/fixture_makers.py b/tests/db/fake_data/fixture_makers.py index 570e63c..571e506 100644 --- a/tests/db/fake_data/fixture_makers.py +++ b/tests/db/fake_data/fixture_makers.py @@ -13,7 +13,11 @@ def make_address(city): def func(**kwargs): """Create an `Address` object in the `city`.""" - return factories.AddressFactory.build(city=city, **kwargs) + address = factories.AddressFactory.build(**kwargs) + + city.addresses.append(address) + + return address return func @@ -58,7 +62,11 @@ def make_restaurant(make_address): if address is None: address = make_address() - return factories.RestaurantFactory.build(address=address, **kwargs) + restaurant = factories.RestaurantFactory.build(**kwargs) + + address.restaurants.append(restaurant) + + return restaurant return func @@ -93,15 +101,18 @@ def make_order(make_address, make_courier, make_customer, make_restaurant): if courier is None: courier = make_courier() - return factory_cls.build( + order = factory_cls.build( customer=make_customer(), # assume a unique `Customer` per order - restaurant=restaurant, courier=courier, pickup_address=restaurant.address, # no `Address` history delivery_address=make_address(), # unique `Customer` => new `Address` **kwargs, ) + restaurant.orders.append(order) + + return order + return func From e01421bb960d7b420bc26781419148828088d157 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 10:54:22 +0100 Subject: [PATCH 3/4] Close the connection after running the migrations The pooled `connection` stayed open until garbage collection, which emits a `ResourceWarning` --- migrations/env.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/env.py b/migrations/env.py index 5b090b4..49d1ac8 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -31,7 +31,8 @@ def include_object(obj, _name, type_, _reflected, _compare_to): return True -engine = sa.create_engine(velofy_config.DATABASE_URI) +# Without a `pool`, the `connection` really closes after the migrations +engine = sa.create_engine(velofy_config.DATABASE_URI, poolclass=sa.pool.NullPool) test_schema = f"_{velofy_config.CLEAN_SCHEMA}" if os.getenv("TESTING") else "" From c3198ecc488ed3e938594cac4b0a3b80f6684de2 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 30 Nov 2026 10:56:46 +0100 Subject: [PATCH 4/4] Turn all warnings into errors New warnings, such as deprecations, then fail right away instead of piling up. The ignore for `patsy` is obsolete. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index aa013c8..9de9cb9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -409,7 +409,7 @@ env = [ ] filterwarnings = [ - "ignore:::patsy.*", + "error", # Turn all warnings into errors ] markers = [