From c1f04c65aaa8529e3d01586d1fb2a60fe71a749f Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 23:17:29 +0100 Subject: [PATCH] Count rows with `select()` in the tests `Session.query()` is the legacy API in SQLAlchemy 2.x. --- tests/console/test_gridify.py | 5 +++-- tests/db/replay/test_orders.py | 14 ++++++++++---- tests/db/test_addresses.py | 8 ++++++-- tests/db/test_addresses_addresses.py | 24 ++++++++++++------------ tests/db/test_addresses_pixels.py | 14 ++++++++++++-- tests/db/test_cities.py | 5 +++-- tests/db/test_couriers.py | 9 +++++++-- tests/db/test_customer.py | 9 +++++++-- tests/db/test_forecasts.py | 8 ++++++-- tests/db/test_grids.py | 4 ++-- tests/db/test_orders.py | 12 ++++++------ tests/db/test_pixels.py | 4 ++-- tests/db/test_restaurants.py | 10 ++++++++-- tests/forecasts/test_models.py | 13 ++++++++++--- 14 files changed, 94 insertions(+), 45 deletions(-) diff --git a/tests/console/test_gridify.py b/tests/console/test_gridify.py index f730951..8a7af2a 100644 --- a/tests/console/test_gridify.py +++ b/tests/console/test_gridify.py @@ -1,6 +1,7 @@ """Integration test for the `velofy.console.gridify` module.""" import pytest +import sqlalchemy as sa import velofy from velofy import db @@ -50,5 +51,5 @@ def test_two_pixels_with_two_addresses( assert result.exit_code == 0 - assert db_session.query(db.Grid).count() == 1 - assert db_session.query(db.Pixel).count() == 2 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Grid)) == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Pixel)) == 2 diff --git a/tests/db/replay/test_orders.py b/tests/db/replay/test_orders.py index 967ae0b..55beb6c 100644 --- a/tests/db/replay/test_orders.py +++ b/tests/db/replay/test_orders.py @@ -31,12 +31,18 @@ class TestConstraints: def test_insert_into_into_database(self, db_session, replayed_order): """Insert an instance into the (empty) database.""" - assert db_session.query(db.ReplayedOrder).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.ReplayedOrder)) + == 0 + ) db_session.add(replayed_order) db_session.commit() - assert db_session.query(db.ReplayedOrder).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.ReplayedOrder)) + == 1 + ) def test_delete_a_referenced_simulation(self, db_session, replayed_order): """Remove a record that is referenced with a FK.""" @@ -202,12 +208,12 @@ class TestConstraints: dispatch_at=scheduled_delivery_at + dt.timedelta(hours=1), ) - assert db_session.query(db.Order).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 0 db_session.add(replay_pre_order) db_session.commit() - assert db_session.query(db.Order).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 1 def test_scheduled_order_too_late(self, db_session, make_replay_order): """Insert an instance with invalid data.""" diff --git a/tests/db/test_addresses.py b/tests/db/test_addresses.py index 9057657..759d39e 100644 --- a/tests/db/test_addresses.py +++ b/tests/db/test_addresses.py @@ -29,12 +29,16 @@ class TestConstraints: def test_insert_into_database(self, db_session, address): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Address).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Address)) == 0 + ) db_session.add(address) db_session.commit() - assert db_session.query(db.Address).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Address)) == 1 + ) def test_delete_a_referenced_address(self, db_session, address, make_address): """Remove a record that is referenced with a FK.""" diff --git a/tests/db/test_addresses_addresses.py b/tests/db/test_addresses_addresses.py index a9a0dbf..267554f 100644 --- a/tests/db/test_addresses_addresses.py +++ b/tests/db/test_addresses_addresses.py @@ -60,12 +60,12 @@ class TestConstraints: def test_insert_into_database(self, db_session, path): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Path).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 0 db_session.add(path) db_session.commit() - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 def test_delete_a_referenced_first_address(self, db_session, path): """Remove a record that is referenced with a FK.""" @@ -222,11 +222,11 @@ class TestFromAddresses: another_address, ): """Test instantiation of a new `Path` instance.""" - assert db_session.query(db.Path).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 0 db.Path.from_addresses(address, another_address) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 def test_make_the_same_path_instance_twice( self, @@ -235,15 +235,15 @@ class TestFromAddresses: another_address, ): """Test instantiation of a new `Path` instance.""" - assert db_session.query(db.Path).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 0 db.Path.from_addresses(address, another_address) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 db.Path.from_addresses(another_address, address) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 def test_structure_of_return_value(self, address, another_address): """Test instantiation of a new `Path` instance.""" @@ -323,11 +323,11 @@ class TestFromAddresses: order, ): """Test instantiation of a new `Path` instance.""" - assert db_session.query(db.Path).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 0 db.Path.from_order(order) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 def test_make_the_same_path_instance_from_order_twice( self, @@ -335,15 +335,15 @@ class TestFromAddresses: order, ): """Test instantiation of a new `Path` instance.""" - assert db_session.query(db.Path).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 0 db.Path.from_order(order) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 db.Path.from_order(order) - assert db_session.query(db.Path).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Path)) == 1 def test_structure_of_return_value_from_order(self, order): """Test instantiation of a new `Path` instance.""" diff --git a/tests/db/test_addresses_pixels.py b/tests/db/test_addresses_pixels.py index 6fa46d9..90af9ed 100644 --- a/tests/db/test_addresses_pixels.py +++ b/tests/db/test_addresses_pixels.py @@ -43,12 +43,22 @@ class TestConstraints: def test_insert_into_database(self, db_session, assoc): """Insert an instance into the (empty) database.""" - assert db_session.query(db.AddressPixelAssociation).count() == 0 + assert ( + db_session.scalar( + sa.select(sa.func.count()).select_from(db.AddressPixelAssociation), + ) + == 0 + ) db_session.add(assoc) db_session.commit() - assert db_session.query(db.AddressPixelAssociation).count() == 1 + assert ( + db_session.scalar( + sa.select(sa.func.count()).select_from(db.AddressPixelAssociation), + ) + == 1 + ) def test_delete_a_referenced_address(self, db_session, assoc): """Remove a record that is referenced with a FK.""" diff --git a/tests/db/test_cities.py b/tests/db/test_cities.py index 85a2a99..cc7df6a 100644 --- a/tests/db/test_cities.py +++ b/tests/db/test_cities.py @@ -1,6 +1,7 @@ """Test the ORM's `City` model.""" import pytest +import sqlalchemy as sa from velofy import db from velofy.db import utils @@ -27,12 +28,12 @@ class TestConstraints: def test_insert_into_database(self, db_session, city): """Insert an instance into the (empty) database.""" - assert db_session.query(db.City).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.City)) == 0 db_session.add(city) db_session.commit() - assert db_session.query(db.City).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.City)) == 1 class TestProperties: diff --git a/tests/db/test_couriers.py b/tests/db/test_couriers.py index 531750d..d66a5fe 100644 --- a/tests/db/test_couriers.py +++ b/tests/db/test_couriers.py @@ -1,6 +1,7 @@ """Test the ORM's `Courier` model.""" import pytest +import sqlalchemy as sa from sqlalchemy import exc as sa_exc from velofy import db @@ -27,12 +28,16 @@ class TestConstraints: def test_insert_into_database(self, db_session, courier): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Courier).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Courier)) == 0 + ) db_session.add(courier) db_session.commit() - assert db_session.query(db.Courier).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Courier)) == 1 + ) def test_invalid_vehicle(self, db_session, courier): """Insert an instance with invalid data.""" diff --git a/tests/db/test_customer.py b/tests/db/test_customer.py index 761ed31..ec2ba2c 100644 --- a/tests/db/test_customer.py +++ b/tests/db/test_customer.py @@ -1,6 +1,7 @@ """Test the ORM's `Customer` model.""" import pytest +import sqlalchemy as sa from velofy import db @@ -26,9 +27,13 @@ class TestConstraints: def test_insert_into_database(self, db_session, customer): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Customer).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Customer)) == 0 + ) db_session.add(customer) db_session.commit() - assert db_session.query(db.Customer).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Customer)) == 1 + ) diff --git a/tests/db/test_forecasts.py b/tests/db/test_forecasts.py index 7bf7abf..4720870 100644 --- a/tests/db/test_forecasts.py +++ b/tests/db/test_forecasts.py @@ -63,12 +63,16 @@ class TestConstraints: def test_insert_into_database(self, db_session, forecast): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Forecast).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Forecast)) == 0 + ) db_session.add(forecast) db_session.commit() - assert db_session.query(db.Forecast).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Forecast)) == 1 + ) def test_delete_a_referenced_pixel(self, db_session, forecast): """Remove a record that is referenced with a FK.""" diff --git a/tests/db/test_grids.py b/tests/db/test_grids.py index a8bf845..c797e2e 100644 --- a/tests/db/test_grids.py +++ b/tests/db/test_grids.py @@ -28,12 +28,12 @@ class TestConstraints: def test_insert_into_database(self, db_session, grid): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Grid).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Grid)) == 0 db_session.add(grid) db_session.commit() - assert db_session.query(db.Grid).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Grid)) == 1 def test_delete_a_referenced_city(self, db_session, grid): """Remove a record that is referenced with a FK.""" diff --git a/tests/db/test_orders.py b/tests/db/test_orders.py index 3691cdd..f350057 100644 --- a/tests/db/test_orders.py +++ b/tests/db/test_orders.py @@ -34,23 +34,23 @@ class TestConstraints: """Insert an instance into the (empty) database.""" assert order.ad_hoc is True - assert db_session.query(db.Order).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 0 db_session.add(order) db_session.commit() - assert db_session.query(db.Order).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 1 def test_insert_scheduled_order_into_into_database(self, db_session, pre_order): """Insert an instance into the (empty) database.""" assert pre_order.ad_hoc is False - assert db_session.query(db.Order).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 0 db_session.add(pre_order) db_session.commit() - assert db_session.query(db.Order).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 1 def test_delete_a_referenced_customer(self, db_session, order): """Remove a record that is referenced with a FK.""" @@ -211,12 +211,12 @@ class TestConstraints: scheduled_delivery_at=dt.datetime(*test_config.DATE, 11, 45), ) - assert db_session.query(db.Order).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 0 db_session.add(pre_order) db_session.commit() - assert db_session.query(db.Order).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Order)) == 1 def test_scheduled_order_too_late(self, db_session, make_order): """Insert an instance with invalid data.""" diff --git a/tests/db/test_pixels.py b/tests/db/test_pixels.py index 636328b..9231836 100644 --- a/tests/db/test_pixels.py +++ b/tests/db/test_pixels.py @@ -28,12 +28,12 @@ class TestConstraints: def test_insert_into_database(self, db_session, pixel): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Pixel).count() == 0 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Pixel)) == 0 db_session.add(pixel) db_session.commit() - assert db_session.query(db.Pixel).count() == 1 + assert db_session.scalar(sa.select(sa.func.count()).select_from(db.Pixel)) == 1 def test_delete_a_referenced_grid(self, db_session, pixel): """Remove a record that is referenced with a FK.""" diff --git a/tests/db/test_restaurants.py b/tests/db/test_restaurants.py index a2d6b03..8737dac 100644 --- a/tests/db/test_restaurants.py +++ b/tests/db/test_restaurants.py @@ -28,12 +28,18 @@ class TestConstraints: def test_insert_into_database(self, db_session, restaurant): """Insert an instance into the (empty) database.""" - assert db_session.query(db.Restaurant).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Restaurant)) + == 0 + ) db_session.add(restaurant) db_session.commit() - assert db_session.query(db.Restaurant).count() == 1 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Restaurant)) + == 1 + ) def test_delete_a_referenced_address(self, db_session, restaurant): """Remove a record that is referenced with a FK.""" diff --git a/tests/forecasts/test_models.py b/tests/forecasts/test_models.py index 48de8c1..6d68e72 100644 --- a/tests/forecasts/test_models.py +++ b/tests/forecasts/test_models.py @@ -4,6 +4,7 @@ import typing as t import pandas as pd import pytest +import sqlalchemy as sa from tests import config as test_config from velofy import db @@ -173,7 +174,9 @@ class TestGenericForecastingModelProperties: """`*Model.make_forecast()` caches the `Forecast` object.""" model = model_cls(order_history=order_history) - assert db_session.query(db.Forecast).count() == 0 + assert ( + db_session.scalar(sa.select(sa.func.count()).select_from(db.Forecast)) == 0 + ) result1 = model.make_forecast( pixel=pixel, @@ -181,7 +184,9 @@ class TestGenericForecastingModelProperties: train_horizon=test_config.LONG_TRAIN_HORIZON, ) - n_cached_forecasts = db_session.query(db.Forecast).count() + n_cached_forecasts = db_session.scalar( + sa.select(sa.func.count()).select_from(db.Forecast), + ) assert n_cached_forecasts >= 1 result2 = model.make_forecast( @@ -190,6 +195,8 @@ class TestGenericForecastingModelProperties: train_horizon=test_config.LONG_TRAIN_HORIZON, ) - assert n_cached_forecasts == db_session.query(db.Forecast).count() + assert n_cached_forecasts == db_session.scalar( + sa.select(sa.func.count()).select_from(db.Forecast), + ) assert result1 == result2