Clean up the comments and lint the notebooks #22
12 changed files with 39 additions and 59 deletions
Reword the comments in the test suite
Each comment is one sentence or two short ones joined by "=>", without a final period.
commit
bca4250b4d
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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, ...",
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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)),
|
||||
|
|
|
|||
Loading…
Reference in a new issue