From bca4250b4d9512dde4f67996f21f4c4bc9c18387 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Thu, 3 Dec 2026 10:52:09 +0100 Subject: [PATCH] Reword the comments in the test suite Each comment is one sentence or two short ones joined by "=>", without a final period. --- tests/config.py | 9 ++++----- tests/conftest.py | 9 ++++----- tests/console/test_gridify.py | 9 +++------ tests/db/fake_data/factories/addresses.py | 6 ++---- tests/db/fake_data/factories/orders/ad_hoc.py | 8 ++++---- tests/db/replay/test_orders.py | 8 +++----- tests/db/test_addresses_addresses.py | 5 ++--- tests/db/test_grids.py | 6 +++--- tests/db/test_orders.py | 19 +++++++------------ tests/forecasts/conftest.py | 9 +++------ .../forecasts/timify/test_aggregate_orders.py | 8 +++----- .../forecasts/timify/test_avg_daily_demand.py | 2 +- 12 files changed, 39 insertions(+), 59 deletions(-) diff --git a/tests/config.py b/tests/config.py index 64c0817..304ced3 100644 --- a/tests/config.py +++ b/tests/config.py @@ -13,8 +13,8 @@ DATE = (YEAR, MONTH, DAY) # The hour when most test cases take place NOON = 12 -# `START` and `END` constitute a 57-day time span, 8 full weeks plus 1 day. -# That implies a maximum `train_horizon` of `8` as that needs full 7-day weeks. +# `START` and `END` span 57 days, i.e., 8 full weeks plus 1 day +# => The maximum `train_horizon` is `8` as it needs full 7-day weeks START = dt.datetime(YEAR, MONTH, DAY, pkg_config.SERVICE_START, 0) _end = START + dt.timedelta(days=56) # `56` as `START` is not included END = dt.datetime(_end.year, _end.month, _end.day, pkg_config.SERVICE_END, 0) @@ -23,9 +23,8 @@ END = dt.datetime(_end.year, _end.month, _end.day, pkg_config.SERVICE_END, 0) LONG_TIME_STEP = 60 SHORT_TIME_STEP = 30 TIME_STEPS = (SHORT_TIME_STEP, LONG_TIME_STEP) -# The `frequency` of vertical time series is the number of days in a week, 7, -# times the number of time steps per day. With 12 operating hours (11 am - 11 pm) -# the `frequency`s are 84 and 168 for the `LONG/SHORT_TIME_STEP`s. +# Vertical `frequency`s are 7 days times the time steps per day (12 operating hours) +# => 84 and 168 for the `LONG_TIME_STEP` and `SHORT_TIME_STEP` VERTICAL_FREQUENCY_LONG = 7 * 12 VERTICAL_FREQUENCY_SHORT = 7 * 24 diff --git a/tests/conftest.py b/tests/conftest.py index e861199..30740d4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -54,8 +54,8 @@ def db_connection(request): This ensures that Alembic's migration files are consistent. """ - # We need a fresh database connection for each of the two `params`. - # Otherwise, the first test of the parameter run second will fail. + # Each of the two `params` needs a fresh database connection + # => Otherwise, the first test of the second parameter run fails engine = sa.create_engine(pkg_config.DATABASE_URI) connection = engine.connect() @@ -98,9 +98,8 @@ def db_session(db_connection, monkeypatch): # that is rolled back at the end of each test case transaction = db_connection.begin() - # Create a session bound to the same connection as the `transaction`. - # Using any other session would not result in the roll back. - # Commits inside a test case then only release a savepoint. + # The session joins the `transaction`'s connection to be rolled back with it + # => Commits inside a test case only release a savepoint session = orm.Session(bind=db_connection, join_transaction_mode="create_savepoint") # Monkey patch the package's global `session` object, diff --git a/tests/console/test_gridify.py b/tests/console/test_gridify.py index 13bfba1..75b3d69 100644 --- a/tests/console/test_gridify.py +++ b/tests/console/test_gridify.py @@ -24,15 +24,12 @@ def test_two_pixels_with_two_addresses( `tests.db.test_grids.test_two_pixels_with_two_addresses`. The difference is that the result is written to the database. """ - # Create two `Address` objects in distinct `Pixel`s. - # One `Address` in the lower-left `Pixel`, ... + # Two `Address`es in distinct `Pixel`s: one in the lower-left `Pixel`, ... address1 = make_address(latitude=48.8357377, longitude=2.2517412) - # ... and another one in the upper-right one. + # ... and another one in the upper-right one address2 = make_address(latitude=48.8898312, longitude=2.4357622) - # Locate a `Restaurant` at the two `Address`es - # and place one `Order` for each of them - # so that the `Address`es are used as `Order.pickup_address`es + # `Restaurant`s with one `Order` each make the `Address`es pickup addresses restaurant1 = make_restaurant(address=address1) restaurant2 = make_restaurant(address=address2) order1 = make_order(restaurant=restaurant1) diff --git a/tests/db/fake_data/factories/addresses.py b/tests/db/fake_data/factories/addresses.py index 594ea5b..f123d04 100644 --- a/tests/db/fake_data/factories/addresses.py +++ b/tests/db/fake_data/factories/addresses.py @@ -20,10 +20,8 @@ class AddressFactory(alchemy.SQLAlchemyModelFactory): id = factory.Sequence(lambda num: num) created_at = factory.LazyFunction(utils.early_in_the_morning) - # When testing, all addresses are considered primary ones. - # As non-primary addresses have no different behavior - # and the property is only kept from the original dataset for - # completeness sake, that is ok to do. + # Non-primary addresses behave no differently in the tests + # => Treat all addresses as primary ones primary_id = factory.LazyAttribute(lambda obj: obj.id) # Mimic a Google Maps Place ID with just random characters diff --git a/tests/db/fake_data/factories/orders/ad_hoc.py b/tests/db/fake_data/factories/orders/ad_hoc.py index 7d125a1..b2af0a9 100644 --- a/tests/db/fake_data/factories/orders/ad_hoc.py +++ b/tests/db/fake_data/factories/orders/ad_hoc.py @@ -72,8 +72,8 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory): id = factory.Sequence(lambda num: num) # customer -> set by the `make_order` fixture for better control - # Attributes regarding the specialization of an `Order`: ad-hoc or scheduled. - # Ad-hoc `Order`s are placed between 11.45 and 14.15. + # Attributes regarding the specialization of an `Order`: ad-hoc or scheduled + # Ad-hoc `Order`s are placed between 11.45 and 14.15 placed_at = factory.LazyFunction( lambda: ( dt.datetime(*test_config.DATE, 11, 45) @@ -88,8 +88,8 @@ class AdHocOrderFactory(alchemy.SQLAlchemyModelFactory): lambda obj: obj.placed_at + dt.timedelta(minutes=45), ) - # Attributes regarding the cancellation of an `Order`. - # May be overwritten with the `cancel_before_pickup` or `cancel_after_pickup` flags. + # Attributes regarding the cancellation of an `Order` + # May be overwritten with the `cancel_before_pickup` or `cancel_after_pickup` flags cancelled = False cancelled_at = None cancelled_at_corrected = None diff --git a/tests/db/replay/test_orders.py b/tests/db/replay/test_orders.py index d7c497a..356cc66 100644 --- a/tests/db/replay/test_orders.py +++ b/tests/db/replay/test_orders.py @@ -298,13 +298,11 @@ class TestConstraints: Scheduled orders must be at least 30 minutes into the future. """ - # Create an ad-hoc order first and then make that a scheduled order. - # This way, it is less work to keep the timestamps consistent. + # Turning an ad-hoc order into a scheduled one keeps the timestamps consistent replay_pre_order = make_replay_order(scheduled=False) - # Make the `scheduled_delivery_at` the quarter of an hour - # following the next quarter of an hour (i.e., the timestamp - # is between 15 and 30 minutes into the future) + # Schedule the delivery at the quarter of an hour after the next one + # => It is 15 to 30 minutes into the future replay_pre_order.ad_hoc = False replay_pre_order.actual.ad_hoc = False minutes_to_next_quarter = 15 - (replay_pre_order.placed_at.minute % 15) diff --git a/tests/db/test_addresses_addresses.py b/tests/db/test_addresses_addresses.py index 313f669..81e774a 100644 --- a/tests/db/test_addresses_addresses.py +++ b/tests/db/test_addresses_addresses.py @@ -383,9 +383,8 @@ class TestSyncWithGoogleMaps: "copyrights": "Map data ©2021", "legs": [ { - # We choose an artificially high distance of `9999` - # so that the synced bicycle distance is longer - # than the randomized air distance (i.e., fake data) for sure + # An artificially high distance of `9999` ensures + # that the synced bicycle distance exceeds the fake air distance "distance": {"text": "3.0 km", "value": 9999}, "duration": {"text": "10 mins", "value": 596}, "end_address": "13 Place Paul et Jean Paul Avisseau, ...", diff --git a/tests/db/test_grids.py b/tests/db/test_grids.py index b5cd4cb..9ff73fd 100644 --- a/tests/db/test_grids.py +++ b/tests/db/test_grids.py @@ -115,8 +115,8 @@ class TestGridification: """ addresses_mock.return_value = [] - # The chosen `side_length` would result in one `Pixel` if there were orders. - # `+1` as otherwise there would be a second pixel in one direction. + # One `Pixel` would cover the whole `city` if there were orders + # => `+1` as otherwise there would be a second one in one direction side_length = max(city.total_x, city.total_y) + 1 result = db.Grid.gridify(city=city, side_length=side_length) @@ -203,7 +203,7 @@ class TestGridification: addresses_mock.return_value = [ # One `Address` in the lower-left `Pixel`, ... make_address(latitude=48.8357377, longitude=2.2517412), - # ... and another one in the upper-right one. + # ... and another one in the upper-right one make_address(latitude=48.8898312, longitude=2.4357622), ] diff --git a/tests/db/test_orders.py b/tests/db/test_orders.py index b62d57a..b5bd5c2 100644 --- a/tests/db/test_orders.py +++ b/tests/db/test_orders.py @@ -100,11 +100,8 @@ class TestConstraints: with pytest.raises(sa_exc.IntegrityError, match="fk_orders_to_restaurants"): db_session.commit() - # Here should be a test to check deletion of a referenced pickup address, - # so `test_delete_a_referenced_pickup_address(self, db_session, order)`. - # The corresponding "fk_orders_to_addresses_on_pickup_address_id" constraint - # is very hard to test in isolation as the above "fk_orders_to_restaurants_..." - # constraint ensures its integrity already. + # No test for "fk_orders_to_addresses_on_pickup_address_id" as the + # "fk_orders_to_restaurants_..." constraint already ensures its integrity def test_delete_a_referenced_delivery_address(self, db_session, order): """Remove a record that is referenced with a FK.""" @@ -289,13 +286,11 @@ class TestConstraints: Scheduled orders must be at least 30 minutes into the future. """ - # Create an ad-hoc order first and then make that a scheduled order. - # This way, it is less work to keep the timestamps consistent. + # Turning an ad-hoc order into a scheduled one keeps the timestamps consistent pre_order = make_order(scheduled=False) - # Make the `scheduled_delivery_at` the quarter of an hour - # following the next quarter of an hour (i.e., the timestamp - # is between 15 and 30 minutes into the future) + # Schedule the delivery at the quarter of an hour after the next one + # => It is 15 to 30 minutes into the future pre_order.ad_hoc = False minutes_to_next_quarter = 15 - (pre_order.placed_at.minute % 15) pre_order.scheduled_delivery_at = ( @@ -1187,8 +1182,8 @@ def test_make_random_orders( else: cancel_before_pickup, cancel_after_pickup = False, False - # Write all the generated objects to the database. - # This should already trigger an `IntegrityError` if the data are flawed. + # Writing all generated objects to the database checks their integrity + # => An `IntegrityError` here means the fake data are flawed order = make_order( scheduled=scheduled, restaurant=restaurant, diff --git a/tests/forecasts/conftest.py b/tests/forecasts/conftest.py index e11492d..79c0136 100644 --- a/tests/forecasts/conftest.py +++ b/tests/forecasts/conftest.py @@ -27,8 +27,7 @@ def horizontal_datetime_index(): index = pd.Index(gen) index.name = "start_at" - # Sanity check. - # `+1` as both the `START` and `END` day are included. + # `n_days` includes both the `START` and `END` day => `+1` n_days = (test_config.END - test_config.START).days + 1 assert len(index) == n_days @@ -63,8 +62,7 @@ def vertical_datetime_index(): index = pd.Index(gen) index.name = "start_at" - # Sanity check: n_days * n_number_of_opening_hours. - # `+1` as both the `START` and `END` day are included. + # `n_days` includes both the `START` and `END` day => `+1` n_days = (test_config.END - test_config.START).days + 1 assert len(index) == n_days * 12 @@ -122,8 +120,7 @@ def order_totals(good_pixel_id): df = pd.DataFrame(data={"n_orders": 1}, index=index) - # Sanity check: n_pixels * n_time_steps_per_day * n_days. - # `+1` as both the `START` and `END` day are included. + # `n_days` includes both the `START` and `END` day => `+1` n_days = (test_config.END - test_config.START).days + 1 assert len(df) == 2 * 12 * n_days diff --git a/tests/forecasts/timify/test_aggregate_orders.py b/tests/forecasts/timify/test_aggregate_orders.py index 42a56eb..9b17a99 100644 --- a/tests/forecasts/timify/test_aggregate_orders.py +++ b/tests/forecasts/timify/test_aggregate_orders.py @@ -125,8 +125,7 @@ class TestAggregateOrders: result = oh.aggregate_orders() - # Even though there are only 10 orders, there are 12 rows in the `DataFrame`. - # That is so as `0`s are filled in for hours without any demand at the end. + # `0`s fill the hours without demand at the end => 12 rows from 10 orders assert len(result) == 12 assert result["n_orders"].min() == 0 assert result["n_orders"].max() == 1 @@ -241,8 +240,7 @@ class TestAggregateOrders: result = oh.aggregate_orders() - # The resulting `DataFrame` has 24 rows for the 24 30-minute time steps. - # The rows' values are `0` and `1` alternating. + # 24 rows for the 24 30-minute time steps, alternating between `0` and `1` assert len(result) == 24 assert result["n_orders"].min() == 0 assert result["n_orders"].max() == 1 @@ -314,7 +312,7 @@ class TestAggregateOrders: """A persisted `Grid` with two `Pixel` objects.""" # One `Address` in the lower-left `Pixel`, ... address1 = make_address(latitude=48.8357377, longitude=2.2517412) - # ... and another one in the upper-right one. + # ... and another one in the upper-right one address2 = make_address(latitude=48.8898312, longitude=2.4357622) addresses_mock.return_value = [address1, address2] diff --git a/tests/forecasts/timify/test_avg_daily_demand.py b/tests/forecasts/timify/test_avg_daily_demand.py index 669dd23..2ed131c 100644 --- a/tests/forecasts/timify/test_avg_daily_demand.py +++ b/tests/forecasts/timify/test_avg_daily_demand.py @@ -104,7 +104,7 @@ class TestChooseTacticalModel: data = order_history._data data.loc[:, "n_orders"] = 1 - # ... and we set three additional time steps per day to `0`. + # ... and we set three additional time steps per day to `0` data.loc[ # all `Pixel`s, all `Order`s in time steps starting at 11 am (slice(None), slice(data.index.levels[1][0], None, 12)),