Merge branch 'release-0.3.0' into develop
This commit is contained in:
commit
915aa4d3b4
93 changed files with 12302 additions and 2083 deletions
24
.github/workflows/tests.yml
vendored
24
.github/workflows/tests.yml
vendored
|
|
@ -1,7 +1,8 @@
|
|||
name: CI
|
||||
on: push
|
||||
jobs:
|
||||
tests:
|
||||
fast-tests:
|
||||
name: fast (without R)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
|
@ -10,5 +11,22 @@ jobs:
|
|||
python-version: 3.8
|
||||
architecture: x64
|
||||
- run: pip install nox==2020.5.24
|
||||
- run: pip install poetry==1.0.10
|
||||
- run: nox
|
||||
- run: pip install poetry==1.1.4
|
||||
- run: nox -s format lint ci-tests-fast safety docs
|
||||
slow-tests:
|
||||
name: slow (with R)
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
R_LIBS: .r_libs
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.8
|
||||
architecture: x64
|
||||
- run: mkdir .r_libs
|
||||
- run: sudo apt-get install r-base r-base-dev libcurl4-openssl-dev libxml2-dev patchelf
|
||||
- run: R -e "install.packages('forecast')"
|
||||
- run: pip install nox==2020.5.24
|
||||
- run: pip install poetry==1.1.4
|
||||
- run: nox -s ci-tests-slow
|
||||
|
|
|
|||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "research/papers/demand-forecasting"]
|
||||
path = research/papers/demand-forecasting
|
||||
url = git@github.com:webartifex/urban-meal-delivery-demand-forecasting.git
|
||||
|
|
@ -16,7 +16,7 @@ that iteratively build on each other.
|
|||
### Data Cleaning
|
||||
|
||||
The UDP provided its raw data as a PostgreSQL dump.
|
||||
This [notebook](https://nbviewer.jupyter.org/github/webartifex/urban-meal-delivery/blob/develop/notebooks/00_clean_data.ipynb)
|
||||
This [notebook](https://nbviewer.jupyter.org/github/webartifex/urban-meal-delivery/blob/develop/research/clean_data.ipynb)
|
||||
cleans the data extensively
|
||||
and maps them onto the [ORM models](https://github.com/webartifex/urban-meal-delivery/tree/develop/src/urban_meal_delivery/db)
|
||||
defined in the `urban-meal-delivery` package
|
||||
|
|
@ -25,7 +25,7 @@ and contains all source code to drive the analyses.
|
|||
|
||||
Due to a non-disclosure agreement with the UDP,
|
||||
neither the raw nor the cleaned data are published as of now.
|
||||
However, previews of the data can be seen throughout the [notebooks/](https://github.com/webartifex/urban-meal-delivery/tree/develop/notebooks) folders.
|
||||
However, previews of the data can be seen throughout the [research/](https://github.com/webartifex/urban-meal-delivery/tree/develop/research) folder.
|
||||
|
||||
|
||||
### Real-time Demand Forecasting
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import urban_meal_delivery as umd
|
|||
|
||||
project = umd.__pkg_name__
|
||||
author = umd.__author__
|
||||
copyright = f'2020, {author}' # pylint:disable=redefined-builtin
|
||||
copyright = f'2020, {author}'
|
||||
version = release = umd.__version__
|
||||
|
||||
extensions = [
|
||||
|
|
|
|||
|
|
@ -21,7 +21,11 @@ log_config.fileConfig(context.config.config_file_name)
|
|||
|
||||
def include_object(obj, _name, type_, _reflected, _compare_to):
|
||||
"""Only include the clean schema into --autogenerate migrations."""
|
||||
if type_ in {'table', 'column'} and obj.schema != umd_config.DATABASE_SCHEMA:
|
||||
if ( # noqa:WPS337
|
||||
type_ in {'table', 'column'}
|
||||
and hasattr(obj, 'schema') # noqa:WPS421 => fix for rare edge case
|
||||
and obj.schema != umd_config.CLEAN_SCHEMA
|
||||
):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -107,13 +107,13 @@ def upgrade():
|
|||
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
|
||||
sa.Column('primary_id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('place_id', sa.Unicode(length=120), nullable=False), # noqa:WPS432
|
||||
sa.Column('place_id', sa.Unicode(length=120), nullable=False),
|
||||
sa.Column('latitude', postgresql.DOUBLE_PRECISION(), nullable=False),
|
||||
sa.Column('longitude', postgresql.DOUBLE_PRECISION(), nullable=False),
|
||||
sa.Column('city_id', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('city', sa.Unicode(length=25), nullable=False), # noqa:WPS432
|
||||
sa.Column('city', sa.Unicode(length=25), nullable=False),
|
||||
sa.Column('zip_code', sa.Integer(), nullable=False),
|
||||
sa.Column('street', sa.Unicode(length=80), nullable=False), # noqa:WPS432
|
||||
sa.Column('street', sa.Unicode(length=80), nullable=False),
|
||||
sa.Column('floor', sa.SmallInteger(), nullable=True),
|
||||
sa.CheckConstraint(
|
||||
'-180 <= longitude AND longitude <= 180',
|
||||
|
|
@ -192,7 +192,7 @@ def upgrade():
|
|||
'restaurants',
|
||||
sa.Column('id', sa.SmallInteger(), autoincrement=False, nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('name', sa.Unicode(length=45), nullable=False), # noqa:WPS432
|
||||
sa.Column('name', sa.Unicode(length=45), nullable=False),
|
||||
sa.Column('address_id', sa.Integer(), nullable=False),
|
||||
sa.Column('estimated_prep_duration', sa.SmallInteger(), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,167 @@
|
|||
"""Add pixel grid.
|
||||
|
||||
Revision: #888e352d7526 at 2021-01-02 18:11:02
|
||||
Revises: #f11cd76d2f45
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = '888e352d7526'
|
||||
down_revision = 'f11cd76d2f45'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision 888e352d7526."""
|
||||
op.create_table(
|
||||
'grids',
|
||||
sa.Column('id', sa.SmallInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column('city_id', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('side_length', sa.SmallInteger(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_grids')),
|
||||
sa.ForeignKeyConstraint(
|
||||
['city_id'],
|
||||
[f'{config.CLEAN_SCHEMA}.cities.id'],
|
||||
name=op.f('fk_grids_to_cities_via_city_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
'city_id', 'side_length', name=op.f('uq_grids_on_city_id_side_length'),
|
||||
),
|
||||
# This `UniqueConstraint` is needed by the `addresses_pixels` table below.
|
||||
sa.UniqueConstraint('id', 'city_id', name=op.f('uq_grids_on_id_city_id')),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'pixels',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('grid_id', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('n_x', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('n_y', sa.SmallInteger(), nullable=False),
|
||||
sa.CheckConstraint('0 <= n_x', name=op.f('ck_pixels_on_n_x_is_positive')),
|
||||
sa.CheckConstraint('0 <= n_y', name=op.f('ck_pixels_on_n_y_is_positive')),
|
||||
sa.ForeignKeyConstraint(
|
||||
['grid_id'],
|
||||
[f'{config.CLEAN_SCHEMA}.grids.id'],
|
||||
name=op.f('fk_pixels_to_grids_via_grid_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_pixels')),
|
||||
sa.UniqueConstraint(
|
||||
'grid_id', 'n_x', 'n_y', name=op.f('uq_pixels_on_grid_id_n_x_n_y'),
|
||||
),
|
||||
sa.UniqueConstraint('id', 'grid_id', name=op.f('uq_pixels_on_id_grid_id')),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
op.create_index(
|
||||
op.f('ix_pixels_on_grid_id'),
|
||||
'pixels',
|
||||
['grid_id'],
|
||||
unique=False,
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_index(
|
||||
op.f('ix_pixels_on_n_x'),
|
||||
'pixels',
|
||||
['n_x'],
|
||||
unique=False,
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_index(
|
||||
op.f('ix_pixels_on_n_y'),
|
||||
'pixels',
|
||||
['n_y'],
|
||||
unique=False,
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
# This `UniqueConstraint` is needed by the `addresses_pixels` table below.
|
||||
op.create_unique_constraint(
|
||||
'uq_addresses_on_id_city_id',
|
||||
'addresses',
|
||||
['id', 'city_id'],
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'addresses_pixels',
|
||||
sa.Column('address_id', sa.Integer(), nullable=False),
|
||||
sa.Column('city_id', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('grid_id', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('pixel_id', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
['address_id', 'city_id'],
|
||||
[
|
||||
f'{config.CLEAN_SCHEMA}.addresses.id',
|
||||
f'{config.CLEAN_SCHEMA}.addresses.city_id',
|
||||
],
|
||||
name=op.f('fk_addresses_pixels_to_addresses_via_address_id_city_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['grid_id', 'city_id'],
|
||||
[
|
||||
f'{config.CLEAN_SCHEMA}.grids.id',
|
||||
f'{config.CLEAN_SCHEMA}.grids.city_id',
|
||||
],
|
||||
name=op.f('fk_addresses_pixels_to_grids_via_grid_id_city_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
['pixel_id', 'grid_id'],
|
||||
[
|
||||
f'{config.CLEAN_SCHEMA}.pixels.id',
|
||||
f'{config.CLEAN_SCHEMA}.pixels.grid_id',
|
||||
],
|
||||
name=op.f('fk_addresses_pixels_to_pixels_via_pixel_id_grid_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.PrimaryKeyConstraint(
|
||||
'address_id', 'pixel_id', name=op.f('pk_addresses_pixels'),
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
'address_id',
|
||||
'grid_id',
|
||||
name=op.f('uq_addresses_pixels_on_address_id_grid_id'),
|
||||
),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision f11cd76d2f45."""
|
||||
op.drop_table('addresses_pixels', schema=config.CLEAN_SCHEMA)
|
||||
op.drop_constraint(
|
||||
'uq_addresses_on_id_city_id',
|
||||
'addresses',
|
||||
type_=None,
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_index(
|
||||
op.f('ix_pixels_on_n_y'), table_name='pixels', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_index(
|
||||
op.f('ix_pixels_on_n_x'), table_name='pixels', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_index(
|
||||
op.f('ix_pixels_on_grid_id'), table_name='pixels', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_table('pixels', schema=config.CLEAN_SCHEMA)
|
||||
op.drop_table('grids', schema=config.CLEAN_SCHEMA)
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
"""Add demand forecasting.
|
||||
|
||||
Revision: #e40623e10405 at 2021-01-06 19:55:56
|
||||
Revises: #888e352d7526
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = 'e40623e10405'
|
||||
down_revision = '888e352d7526'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision e40623e10405."""
|
||||
op.create_table(
|
||||
'forecasts',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('pixel_id', sa.Integer(), nullable=False),
|
||||
sa.Column('start_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('time_step', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('training_horizon', sa.SmallInteger(), nullable=False),
|
||||
sa.Column('method', sa.Unicode(length=20), nullable=False),
|
||||
sa.Column('prediction', postgresql.DOUBLE_PRECISION(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_forecasts')),
|
||||
sa.ForeignKeyConstraint(
|
||||
['pixel_id'],
|
||||
[f'{config.CLEAN_SCHEMA}.pixels.id'],
|
||||
name=op.f('fk_forecasts_to_pixels_via_pixel_id'),
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"""
|
||||
NOT (
|
||||
EXTRACT(HOUR FROM start_at) < 11
|
||||
OR
|
||||
EXTRACT(HOUR FROM start_at) > 22
|
||||
)
|
||||
""",
|
||||
name=op.f('ck_forecasts_on_start_at_must_be_within_operating_hours'),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
'CAST(EXTRACT(MINUTES FROM start_at) AS INTEGER) % 15 = 0',
|
||||
name=op.f('ck_forecasts_on_start_at_minutes_must_be_quarters_of_the_hour'),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
'CAST(EXTRACT(MICROSECONDS FROM start_at) AS INTEGER) % 1000000 = 0',
|
||||
name=op.f('ck_forecasts_on_start_at_allows_no_microseconds'),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
'EXTRACT(SECONDS FROM start_at) = 0',
|
||||
name=op.f('ck_forecasts_on_start_at_allows_no_seconds'),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
'time_step > 0', name=op.f('ck_forecasts_on_time_step_must_be_positive'),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
'training_horizon > 0',
|
||||
name=op.f('ck_forecasts_on_training_horizon_must_be_positive'),
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
'pixel_id',
|
||||
'start_at',
|
||||
'time_step',
|
||||
'training_horizon',
|
||||
'method',
|
||||
name=op.f(
|
||||
'uq_forecasts_on_pixel_id_start_at_time_step_training_horizon_method',
|
||||
),
|
||||
),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_index(
|
||||
op.f('ix_forecasts_on_pixel_id'),
|
||||
'forecasts',
|
||||
['pixel_id'],
|
||||
unique=False,
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision 888e352d7526."""
|
||||
op.drop_table('forecasts', schema=config.CLEAN_SCHEMA)
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
"""Add confidence intervals to forecasts.
|
||||
|
||||
Revision: #26711cd3f9b9 at 2021-01-20 16:08:21
|
||||
Revises: #e40623e10405
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = '26711cd3f9b9'
|
||||
down_revision = 'e40623e10405'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision 26711cd3f9b9."""
|
||||
op.alter_column(
|
||||
'forecasts', 'method', new_column_name='model', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.add_column(
|
||||
'forecasts',
|
||||
sa.Column('low80', postgresql.DOUBLE_PRECISION(), nullable=True),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.add_column(
|
||||
'forecasts',
|
||||
sa.Column('high80', postgresql.DOUBLE_PRECISION(), nullable=True),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.add_column(
|
||||
'forecasts',
|
||||
sa.Column('low95', postgresql.DOUBLE_PRECISION(), nullable=True),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.add_column(
|
||||
'forecasts',
|
||||
sa.Column('high95', postgresql.DOUBLE_PRECISION(), nullable=True),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_check_constraint(
|
||||
op.f('ck_forecasts_on_ci_upper_and_lower_bounds'),
|
||||
'forecasts',
|
||||
"""
|
||||
NOT (
|
||||
low80 IS NULL AND high80 IS NOT NULL
|
||||
OR
|
||||
low80 IS NOT NULL AND high80 IS NULL
|
||||
OR
|
||||
low95 IS NULL AND high95 IS NOT NULL
|
||||
OR
|
||||
low95 IS NOT NULL AND high95 IS NULL
|
||||
)
|
||||
""",
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_check_constraint(
|
||||
op.f('prediction_must_be_within_ci'),
|
||||
'forecasts',
|
||||
"""
|
||||
NOT (
|
||||
prediction < low80
|
||||
OR
|
||||
prediction < low95
|
||||
OR
|
||||
prediction > high80
|
||||
OR
|
||||
prediction > high95
|
||||
)
|
||||
""",
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_check_constraint(
|
||||
op.f('ci_upper_bound_greater_than_lower_bound'),
|
||||
'forecasts',
|
||||
"""
|
||||
NOT (
|
||||
low80 > high80
|
||||
OR
|
||||
low95 > high95
|
||||
)
|
||||
""",
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_check_constraint(
|
||||
op.f('ci95_must_be_wider_than_ci80'),
|
||||
'forecasts',
|
||||
"""
|
||||
NOT (
|
||||
low80 < low95
|
||||
OR
|
||||
high80 > high95
|
||||
)
|
||||
""",
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision e40623e10405."""
|
||||
op.alter_column(
|
||||
'forecasts', 'model', new_column_name='method', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_column(
|
||||
'forecasts', 'low80', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_column(
|
||||
'forecasts', 'high80', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_column(
|
||||
'forecasts', 'low95', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_column(
|
||||
'forecasts', 'high95', schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
|
@ -0,0 +1,398 @@
|
|||
"""Remove orders from restaurants with invalid location ...
|
||||
|
||||
... and also de-duplicate a couple of redundant addresses.
|
||||
|
||||
Revision: #e86290e7305e at 2021-01-23 15:56:59
|
||||
Revises: #26711cd3f9b9
|
||||
|
||||
1) Remove orders
|
||||
|
||||
Some restaurants have orders to be picked up at an address that
|
||||
not their primary address. That is ok if that address is the location
|
||||
of a second franchise. However, for a small number of restaurants
|
||||
there is only exactly one order at that other address that often is
|
||||
located far away from the restaurant's primary location. It looks
|
||||
like a restaurant signed up with some invalid location that was then
|
||||
corrected into the primary one.
|
||||
|
||||
Use the following SQL statement to obtain a list of these locations
|
||||
before this migration is run:
|
||||
|
||||
SELECT
|
||||
orders.pickup_address_id,
|
||||
COUNT(*) AS n_orders,
|
||||
MIN(placed_at) as first_order_at,
|
||||
MAX(placed_at) as last_order_at
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
LEFT OUTER JOIN
|
||||
{config.CLEAN_SCHEMA}.restaurants
|
||||
ON orders.restaurant_id = restaurants.id
|
||||
WHERE
|
||||
orders.pickup_address_id <> restaurants.address_id
|
||||
GROUP BY
|
||||
pickup_address_id;
|
||||
|
||||
50 orders with such weird pickup addresses are removed with this migration.
|
||||
|
||||
|
||||
2) De-duplicate addresses
|
||||
|
||||
Five restaurants have two pickup addresses that are actually the same location.
|
||||
|
||||
The following SQL statement shows them before this migration is run:
|
||||
|
||||
SELECT
|
||||
orders.restaurant_id,
|
||||
restaurants.name,
|
||||
restaurants.address_id AS primary_address_id,
|
||||
addresses.id AS address_id,
|
||||
addresses.street,
|
||||
COUNT(*) AS n_orders
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
LEFT OUTER JOIN
|
||||
{config.CLEAN_SCHEMA}.addresses ON orders.pickup_address_id = addresses.id
|
||||
LEFT OUTER JOIN
|
||||
{config.CLEAN_SCHEMA}.restaurants ON orders.restaurant_id = restaurants.id
|
||||
WHERE
|
||||
orders.restaurant_id IN (
|
||||
SELECT
|
||||
restaurant_id
|
||||
FROM (
|
||||
SELECT DISTINCT
|
||||
restaurant_id,
|
||||
pickup_address_id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
) AS restaurant_locations
|
||||
GROUP BY
|
||||
restaurant_id
|
||||
HAVING
|
||||
COUNT(pickup_address_id) > 1
|
||||
)
|
||||
GROUP BY
|
||||
orders.restaurant_id,
|
||||
restaurants.name,
|
||||
restaurants.address_id,
|
||||
addresses.id,
|
||||
addresses.street
|
||||
ORDER BY
|
||||
orders.restaurant_id,
|
||||
restaurants.name,
|
||||
restaurants.address_id,
|
||||
addresses.id,
|
||||
addresses.street;
|
||||
|
||||
|
||||
3) Remove addresses without any association
|
||||
|
||||
After steps 1) and 2) some addresses are not associated with a restaurant any more.
|
||||
|
||||
The following SQL statement lists them before this migration is run:
|
||||
|
||||
SELECT
|
||||
id,
|
||||
street,
|
||||
zip_code,
|
||||
city
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
WHERE
|
||||
id NOT IN (
|
||||
SELECT DISTINCT
|
||||
pickup_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
delivery_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.restaurants
|
||||
);
|
||||
|
||||
4) Ensure every `Restaurant` has exactly one `Address`.
|
||||
|
||||
Replace the current `ForeignKeyConstraint` to from `Order` to `Restaurant`
|
||||
with one that also includes the `Order.pickup_address_id`.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from alembic import op
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = 'e86290e7305e'
|
||||
down_revision = '26711cd3f9b9'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision e86290e7305e."""
|
||||
# 1) Remove orders
|
||||
op.execute(
|
||||
f"""
|
||||
DELETE
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
WHERE pickup_address_id IN (
|
||||
SELECT
|
||||
orders.pickup_address_id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
LEFT OUTER JOIN
|
||||
{config.CLEAN_SCHEMA}.restaurants
|
||||
ON orders.restaurant_id = restaurants.id
|
||||
WHERE
|
||||
orders.pickup_address_id <> restaurants.address_id
|
||||
GROUP BY
|
||||
orders.pickup_address_id
|
||||
HAVING
|
||||
COUNT(*) = 1
|
||||
);
|
||||
""",
|
||||
)
|
||||
|
||||
# 2) De-duplicate addresses
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 353
|
||||
WHERE
|
||||
pickup_address_id = 548916;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 4850
|
||||
WHERE
|
||||
pickup_address_id = 6415;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 16227
|
||||
WHERE
|
||||
pickup_address_id = 44627;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 44458
|
||||
WHERE
|
||||
pickup_address_id = 534543;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 289997
|
||||
WHERE
|
||||
pickup_address_id = 309525;
|
||||
""",
|
||||
)
|
||||
|
||||
# 3) Remove addresses
|
||||
op.execute(
|
||||
f"""
|
||||
DELETE
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.addresses_pixels
|
||||
WHERE
|
||||
address_id NOT IN (
|
||||
SELECT DISTINCT
|
||||
pickup_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
delivery_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.restaurants
|
||||
);
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 302883
|
||||
WHERE
|
||||
primary_id = 43526;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 47597
|
||||
WHERE
|
||||
primary_id = 43728;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 159631
|
||||
WHERE
|
||||
primary_id = 43942;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 275651
|
||||
WHERE
|
||||
primary_id = 44759;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 156685
|
||||
WHERE
|
||||
primary_id = 50599;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
SET
|
||||
primary_id = 480206
|
||||
WHERE
|
||||
primary_id = 51774;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
DELETE
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
WHERE
|
||||
id NOT IN (
|
||||
SELECT DISTINCT
|
||||
pickup_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
delivery_address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
UNION
|
||||
SELECT DISTINCT
|
||||
address_id AS id
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.restaurants
|
||||
);
|
||||
""",
|
||||
)
|
||||
|
||||
# 4) Ensure every `Restaurant` has only one `Order.pickup_address`.
|
||||
op.execute(
|
||||
f"""
|
||||
UPDATE
|
||||
{config.CLEAN_SCHEMA}.orders
|
||||
SET
|
||||
pickup_address_id = 53733
|
||||
WHERE
|
||||
pickup_address_id = 54892;
|
||||
""",
|
||||
)
|
||||
op.execute(
|
||||
f"""
|
||||
DELETE
|
||||
FROM
|
||||
{config.CLEAN_SCHEMA}.addresses
|
||||
WHERE
|
||||
id = 54892;
|
||||
""",
|
||||
)
|
||||
op.create_unique_constraint(
|
||||
'uq_restaurants_on_id_address_id',
|
||||
'restaurants',
|
||||
['id', 'address_id'],
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_foreign_key(
|
||||
op.f('fk_orders_to_restaurants_via_restaurant_id_pickup_address_id'),
|
||||
'orders',
|
||||
'restaurants',
|
||||
['restaurant_id', 'pickup_address_id'],
|
||||
['id', 'address_id'],
|
||||
source_schema=config.CLEAN_SCHEMA,
|
||||
referent_schema=config.CLEAN_SCHEMA,
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
)
|
||||
op.drop_constraint(
|
||||
'fk_orders_to_restaurants_via_restaurant_id',
|
||||
'orders',
|
||||
type_='foreignkey',
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision 26711cd3f9b9."""
|
||||
op.create_foreign_key(
|
||||
op.f('fk_orders_to_restaurants_via_restaurant_id'),
|
||||
'orders',
|
||||
'restaurants',
|
||||
['restaurant_id'],
|
||||
['id'],
|
||||
source_schema=config.CLEAN_SCHEMA,
|
||||
referent_schema=config.CLEAN_SCHEMA,
|
||||
onupdate='RESTRICT',
|
||||
ondelete='RESTRICT',
|
||||
)
|
||||
op.drop_constraint(
|
||||
'fk_orders_to_restaurants_via_restaurant_id_pickup_address_id',
|
||||
'orders',
|
||||
type_='foreignkey',
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.drop_constraint(
|
||||
'uq_restaurants_on_id_address_id',
|
||||
'restaurants',
|
||||
type_='unique',
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
"""Store actuals with forecast.
|
||||
|
||||
Revision: #c2af85bada01 at 2021-01-29 11:13:15
|
||||
Revises: #e86290e7305e
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = 'c2af85bada01'
|
||||
down_revision = 'e86290e7305e'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision c2af85bada01."""
|
||||
op.add_column(
|
||||
'forecasts',
|
||||
sa.Column('actual', sa.SmallInteger(), nullable=False),
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
op.create_check_constraint(
|
||||
op.f('ck_forecasts_on_actuals_must_be_non_negative'),
|
||||
'forecasts',
|
||||
'actual >= 0',
|
||||
schema=config.CLEAN_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision e86290e7305e."""
|
||||
op.drop_column('forecasts', 'actual', schema=config.CLEAN_SCHEMA)
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
"""Rename `Forecast.training_horizon` into `.train_horizon`.
|
||||
|
||||
Revision: #8bfb928a31f8 at 2021-02-02 12:55:09
|
||||
Revises: #c2af85bada01
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from alembic import op
|
||||
|
||||
from urban_meal_delivery import configuration
|
||||
|
||||
|
||||
revision = '8bfb928a31f8'
|
||||
down_revision = 'c2af85bada01'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Upgrade to revision 8bfb928a31f8."""
|
||||
op.execute(
|
||||
f"""
|
||||
ALTER TABLE
|
||||
{config.CLEAN_SCHEMA}.forecasts
|
||||
RENAME COLUMN
|
||||
training_horizon
|
||||
TO
|
||||
train_horizon;
|
||||
""",
|
||||
) # noqa:WPS355
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Downgrade to revision c2af85bada01."""
|
||||
op.execute(
|
||||
f"""
|
||||
ALTER TABLE
|
||||
{config.CLEAN_SCHEMA}.forecasts
|
||||
RENAME COLUMN
|
||||
train_horizon
|
||||
TO
|
||||
training_horizon;
|
||||
""",
|
||||
) # noqa:WPS355
|
||||
191
noxfile.py
191
noxfile.py
|
|
@ -17,7 +17,7 @@ as unified tasks to assure the quality of the source code:
|
|||
that are then interpreted as the paths the formatters and linters work
|
||||
on recursively
|
||||
|
||||
- "lint" (flake8, mypy, pylint): same as "format"
|
||||
- "lint" (flake8, mypy): same as "format"
|
||||
|
||||
- "test" (pytest, xdoctest):
|
||||
|
||||
|
|
@ -25,26 +25,6 @@ as unified tasks to assure the quality of the source code:
|
|||
+ accepts extra arguments, e.g., `poetry run nox -s test -- --no-cov`,
|
||||
that are passed on to `pytest` and `xdoctest` with no changes
|
||||
=> may be paths or options
|
||||
|
||||
|
||||
GitHub Actions implements the following CI workflow:
|
||||
|
||||
- "format", "lint", and "test" as above
|
||||
|
||||
- "safety": check if dependencies contain known security vulnerabilites
|
||||
|
||||
- "docs": build the documentation with sphinx
|
||||
|
||||
|
||||
The pre-commit framework invokes the following tasks:
|
||||
|
||||
- before any commit:
|
||||
|
||||
+ "format" and "lint" as above
|
||||
+ "fix-branch-references": replace branch references with the current one
|
||||
|
||||
- before merges: run the entire "test-suite" independent of the file changes
|
||||
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
|
@ -92,7 +72,7 @@ nox.options.envdir = '.cache/nox'
|
|||
# Avoid accidental successes if the environment is not set up properly.
|
||||
nox.options.error_on_external_run = True
|
||||
|
||||
# Run only CI related checks by default.
|
||||
# Run only local checks by default.
|
||||
nox.options.sessions = (
|
||||
'format',
|
||||
'lint',
|
||||
|
|
@ -141,7 +121,7 @@ def format_(session):
|
|||
|
||||
@nox.session(python=PYTHON)
|
||||
def lint(session):
|
||||
"""Lint source files with flake8, mypy, and pylint.
|
||||
"""Lint source files with flake8 and mypy.
|
||||
|
||||
If no extra arguments are provided, all source files are linted.
|
||||
Otherwise, they are interpreted as paths the linters work on recursively.
|
||||
|
|
@ -158,7 +138,6 @@ def lint(session):
|
|||
'flake8-expression-complexity',
|
||||
'flake8-pytest-style',
|
||||
'mypy',
|
||||
'pylint',
|
||||
'wemake-python-styleguide',
|
||||
)
|
||||
|
||||
|
|
@ -182,18 +161,6 @@ def lint(session):
|
|||
else:
|
||||
session.log('No paths to be checked with mypy')
|
||||
|
||||
# Ignore errors where pylint cannot import a third-party package due its
|
||||
# being run in an isolated environment. For the same reason, pylint is
|
||||
# also not able to determine the correct order of imports.
|
||||
# One way to fix this is to install all develop dependencies in this nox
|
||||
# session, which we do not do. The whole point of static linting tools is
|
||||
# to not rely on any package be importable at runtime. Instead, these
|
||||
# imports are validated implicitly when the test suite is run.
|
||||
session.run('pylint', '--version')
|
||||
session.run(
|
||||
'pylint', '--disable=import-error', '--disable=wrong-import-order', *locations,
|
||||
)
|
||||
|
||||
|
||||
@nox.session(python=PYTHON)
|
||||
def test(session):
|
||||
|
|
@ -222,33 +189,71 @@ def test(session):
|
|||
session.run('poetry', 'install', '--no-dev', external=True)
|
||||
_install_packages(
|
||||
session,
|
||||
'Faker',
|
||||
'factory-boy',
|
||||
'geopy',
|
||||
'packaging',
|
||||
'pytest',
|
||||
'pytest-cov',
|
||||
'pytest-env',
|
||||
'pytest-mock',
|
||||
'pytest-randomly',
|
||||
'xdoctest[optional]',
|
||||
)
|
||||
|
||||
session.run('pytest', '--version')
|
||||
|
||||
# When the CI server runs the slow tests, we only execute the R related
|
||||
# test cases that require the slow installation of R and some packages.
|
||||
if session.env.get('_slow_ci_tests'):
|
||||
session.run(
|
||||
'pytest', '--randomly-seed=4287', '-m', 'r and not db', PYTEST_LOCATION,
|
||||
)
|
||||
|
||||
# In the "ci-tests-slow" session, we do not run any test tool
|
||||
# other than pytest. So, xdoctest, for example, is only run
|
||||
# locally or in the "ci-tests-fast" session.
|
||||
return
|
||||
|
||||
# When the CI server executes pytest, no database is available.
|
||||
# Therefore, the CI server does not measure coverage.
|
||||
elif session.env.get('_fast_ci_tests'):
|
||||
pytest_args = (
|
||||
'--randomly-seed=4287',
|
||||
'-m',
|
||||
'not (db or r)',
|
||||
PYTEST_LOCATION,
|
||||
)
|
||||
|
||||
# When pytest is executed in the local develop environment,
|
||||
# both R and a database are available.
|
||||
# Therefore, we require 100% coverage.
|
||||
else:
|
||||
pytest_args = (
|
||||
'--cov',
|
||||
'--no-cov-on-fail',
|
||||
'--cov-branch',
|
||||
'--cov-fail-under=100',
|
||||
'--cov-report=term-missing:skip-covered',
|
||||
'--randomly-seed=4287',
|
||||
PYTEST_LOCATION,
|
||||
)
|
||||
|
||||
# Interpret extra arguments as options for pytest.
|
||||
# They are "dropped" by the hack in the pre_merge() function
|
||||
# if this function is run within the "pre-merge" session.
|
||||
# They are "dropped" by the hack in the test_suite() function
|
||||
# if this function is run within the "test-suite" session.
|
||||
posargs = () if session.env.get('_drop_posargs') else session.posargs
|
||||
|
||||
args = posargs or (
|
||||
'--cov',
|
||||
'--no-cov-on-fail',
|
||||
'--cov-branch',
|
||||
'--cov-fail-under=100',
|
||||
'--cov-report=term-missing:skip-covered',
|
||||
'-k',
|
||||
'not e2e',
|
||||
PYTEST_LOCATION,
|
||||
)
|
||||
session.run('pytest', '--version')
|
||||
session.run('pytest', *args)
|
||||
session.run('pytest', *(posargs or pytest_args))
|
||||
|
||||
# For xdoctest, the default arguments are different from pytest.
|
||||
args = posargs or [PACKAGE_IMPORT_NAME]
|
||||
|
||||
# The "TESTING" environment variable forces the global `engine`, `connection`,
|
||||
# and `session` objects to be set to `None` and avoid any database connection.
|
||||
# For pytest above this is not necessary as pytest sets this variable itself.
|
||||
session.env['TESTING'] = 'true'
|
||||
|
||||
session.run('xdoctest', '--version')
|
||||
session.run('xdoctest', '--quiet', *args) # --quiet => less verbose output
|
||||
|
||||
|
|
@ -292,6 +297,10 @@ def docs(session):
|
|||
session.run('poetry', 'install', '--no-dev', external=True)
|
||||
_install_packages(session, 'sphinx', 'sphinx-autodoc-typehints')
|
||||
|
||||
# The "TESTING" environment variable forces the global `engine`, `connection`,
|
||||
# and `session` objects to be set to `None` and avoid any database connection.
|
||||
session.env['TESTING'] = 'true'
|
||||
|
||||
session.run('sphinx-build', DOCS_SRC, DOCS_BUILD)
|
||||
# Verify all external links return 200 OK.
|
||||
session.run('sphinx-build', '-b', 'linkcheck', DOCS_SRC, DOCS_BUILD)
|
||||
|
|
@ -299,11 +308,63 @@ def docs(session):
|
|||
print(f'Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html') # noqa:WPS421
|
||||
|
||||
|
||||
@nox.session(name='ci-tests-fast', python=PYTHON)
|
||||
def fast_ci_tests(session):
|
||||
"""Fast tests run by the GitHub Actions CI server.
|
||||
|
||||
These regards all test cases NOT involving R via `rpy2`.
|
||||
|
||||
Also, coverage is not measured as full coverage can only be
|
||||
achieved by running the tests in the local develop environment
|
||||
that has access to a database.
|
||||
"""
|
||||
# Re-using an old environment is not so easy here as the "test" session
|
||||
# runs `poetry install --no-dev`, which removes previously installed packages.
|
||||
if session.virtualenv.reuse_existing:
|
||||
raise RuntimeError(
|
||||
'The "ci-tests-fast" session must be run without the "-r" option',
|
||||
)
|
||||
|
||||
# Little hack to pass arguments to the "test" session.
|
||||
session.env['_fast_ci_tests'] = 'true'
|
||||
|
||||
# Cannot use session.notify() to trigger the "test" session
|
||||
# as that would create a new Session object without the flag
|
||||
# in the env(ironment).
|
||||
test(session)
|
||||
|
||||
|
||||
@nox.session(name='ci-tests-slow', python=PYTHON)
|
||||
def slow_ci_tests(session):
|
||||
"""Slow tests run by the GitHub Actions CI server.
|
||||
|
||||
These regards all test cases involving R via `rpy2`.
|
||||
They are slow as the CI server needs to install R and some packages
|
||||
first, which takes a couple of minutes.
|
||||
|
||||
Also, coverage is not measured as full coverage can only be
|
||||
achieved by running the tests in the local develop environment
|
||||
that has access to a database.
|
||||
"""
|
||||
# Re-using an old environment is not so easy here as the "test" session
|
||||
# runs `poetry install --no-dev`, which removes previously installed packages.
|
||||
if session.virtualenv.reuse_existing:
|
||||
raise RuntimeError(
|
||||
'The "ci-tests-slow" session must be run without the "-r" option',
|
||||
)
|
||||
|
||||
# Little hack to pass arguments to the "test" session.
|
||||
session.env['_slow_ci_tests'] = 'true'
|
||||
|
||||
# Cannot use session.notify() to trigger the "test" session
|
||||
# as that would create a new Session object without the flag
|
||||
# in the env(ironment).
|
||||
test(session)
|
||||
|
||||
|
||||
@nox.session(name='test-suite', python=PYTHON)
|
||||
def test_suite(session):
|
||||
"""Run the entire test suite.
|
||||
|
||||
Intended to be run as a pre-commit hook.
|
||||
"""Run the entire test suite as a pre-commit hook.
|
||||
|
||||
Ignores the paths passed in by the pre-commit framework
|
||||
and runs the entire test suite.
|
||||
|
|
@ -322,13 +383,12 @@ def test_suite(session):
|
|||
|
||||
# Cannot use session.notify() to trigger the "test" session
|
||||
# as that would create a new Session object without the flag
|
||||
# in the env(ironment). Instead, run the test() function within
|
||||
# the "pre-merge" session.
|
||||
# in the env(ironment).
|
||||
test(session)
|
||||
|
||||
|
||||
@nox.session(name='fix-branch-references', python=PYTHON, venv_backend='none')
|
||||
def fix_branch_references(session): # noqa:WPS210
|
||||
def fix_branch_references(session): # noqa:WPS210,WPS231
|
||||
"""Replace branch references with the current branch.
|
||||
|
||||
Intended to be run as a pre-commit hook.
|
||||
|
|
@ -336,9 +396,15 @@ def fix_branch_references(session): # noqa:WPS210
|
|||
Many files in the project (e.g., README.md) contain links to resources
|
||||
on github.com or nbviewer.jupyter.org that contain branch labels.
|
||||
|
||||
This task rewrites these links such that they contain the branch reference
|
||||
of the current branch. If the branch is only a temporary one that is to be
|
||||
merged into the 'main' branch, all references are adjusted to 'main' as well.
|
||||
This task rewrites these links such that they contain branch references
|
||||
that make sense given the context:
|
||||
|
||||
- If the branch is only a temporary one that is to be merged into
|
||||
the 'main' branch, all references are adjusted to 'main' as well.
|
||||
|
||||
- If the branch is not named after a default branch in the GitFlow
|
||||
model, it is interpreted as a feature branch and the references
|
||||
are adjusted into 'develop'.
|
||||
|
||||
This task may be called with one positional argument that is interpreted
|
||||
as the branch to which all references are changed into.
|
||||
|
|
@ -362,6 +428,10 @@ def fix_branch_references(session): # noqa:WPS210
|
|||
# into 'main', we adjust all branch references to 'main' as well.
|
||||
if branch.startswith('release') or branch.startswith('research'):
|
||||
branch = 'main'
|
||||
# If the current branch appears to be a feature branch, we adjust
|
||||
# all branch references to 'develop'.
|
||||
elif branch != 'main':
|
||||
branch = 'develop'
|
||||
# If a "--branch=BRANCH_NAME" argument is passed in
|
||||
# as the only positional argument, we use BRANCH_NAME.
|
||||
# Note: The --branch is required as session.posargs contains
|
||||
|
|
@ -445,7 +515,7 @@ def init_project(session):
|
|||
|
||||
|
||||
@nox.session(name='clean-pwd', python=PYTHON, venv_backend='none')
|
||||
def clean_pwd(session): # noqa:WPS210,WPS231
|
||||
def clean_pwd(session): # noqa:WPS231
|
||||
"""Remove (almost) all glob patterns listed in .gitignore.
|
||||
|
||||
The difference compared to `git clean -X` is that this task
|
||||
|
|
@ -519,6 +589,7 @@ def _install_packages(session: Session, *packages_or_pip_args: str, **kwargs) ->
|
|||
'--dev',
|
||||
'--format=requirements.txt',
|
||||
f'--output={requirements_txt.name}',
|
||||
'--without-hashes',
|
||||
external=True,
|
||||
)
|
||||
session.install(
|
||||
|
|
|
|||
2633
poetry.lock
generated
2633
poetry.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -9,7 +9,7 @@ target-version = ["py38"]
|
|||
|
||||
[tool.poetry]
|
||||
name = "urban-meal-delivery"
|
||||
version = "0.3.0.dev0"
|
||||
version = "0.4.0.dev0"
|
||||
|
||||
authors = ["Alexander Hess <alexander@webartifex.biz>"]
|
||||
description = "Optimizing an urban meal delivery platform"
|
||||
|
|
@ -28,18 +28,23 @@ repository = "https://github.com/webartifex/urban-meal-delivery"
|
|||
python = "^3.8"
|
||||
|
||||
# Package => code developed in *.py files and packaged under src/urban_meal_delivery
|
||||
Shapely = "^1.7.1"
|
||||
alembic = "^1.4.2"
|
||||
click = "^7.1.2"
|
||||
folium = "^0.12.1"
|
||||
matplotlib = "^3.3.3"
|
||||
pandas = "^1.1.0"
|
||||
psycopg2 = "^2.8.5" # adapter for PostgreSQL
|
||||
python-dotenv = "^0.14.0"
|
||||
rpy2 = "^3.4.1"
|
||||
sqlalchemy = "^1.3.18"
|
||||
statsmodels = "^0.12.1"
|
||||
utm = "^0.7.0"
|
||||
|
||||
# Jupyter Lab => notebooks with analyses using the developed package
|
||||
# IMPORTANT: must be kept in sync with the "research" extra below
|
||||
jupyterlab = { version="^2.2.2", optional=true }
|
||||
nb_black = { version="^1.0.7", optional=true }
|
||||
numpy = { version="^1.19.1", optional=true }
|
||||
pandas = { version="^1.1.0", optional=true }
|
||||
pytz = { version="^2020.1", optional=true }
|
||||
|
||||
[tool.poetry.extras]
|
||||
|
|
@ -47,7 +52,6 @@ research = [
|
|||
"jupyterlab",
|
||||
"nb_black",
|
||||
"numpy",
|
||||
"pandas",
|
||||
"pytz",
|
||||
]
|
||||
|
||||
|
|
@ -68,14 +72,18 @@ flake8-black = "^0.2.1"
|
|||
flake8-expression-complexity = "^0.0.8"
|
||||
flake8-pytest-style = "^1.2.2"
|
||||
mypy = "^0.782"
|
||||
pylint = "^2.5.3"
|
||||
wemake-python-styleguide = "^0.14.1" # flake8 plug-in
|
||||
|
||||
# Test Suite
|
||||
Faker = "^5.0.1"
|
||||
factory-boy = "^3.1.0"
|
||||
geopy = "^2.1.0"
|
||||
packaging = "^20.4" # used to test the packaged version
|
||||
pytest = "^6.0.1"
|
||||
pytest-cov = "^2.10.0"
|
||||
pytest-env = "^0.6.2"
|
||||
pytest-mock = "^3.5.1"
|
||||
pytest-randomly = "^3.5.0"
|
||||
xdoctest = { version="^0.13.0", extras=["optional"] }
|
||||
|
||||
# Documentation
|
||||
|
|
@ -83,4 +91,4 @@ sphinx = "^3.1.2"
|
|||
sphinx-autodoc-typehints = "^1.11.0"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
umd = "urban_meal_delivery.console:main"
|
||||
umd = "urban_meal_delivery.console:cli"
|
||||
|
|
|
|||
|
|
@ -103,8 +103,7 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"_engine = db.make_engine()\n",
|
||||
"connection = _engine.connect()"
|
||||
"connection = db.connection"
|
||||
]
|
||||
},
|
||||
{
|
||||
1
research/papers/demand-forecasting
Submodule
1
research/papers/demand-forecasting
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 9ee3396a24ce20c9886b4cde5cfe2665fd5a8102
|
||||
1868
research/r_dependencies.ipynb
Normal file
1868
research/r_dependencies.ipynb
Normal file
File diff suppressed because it is too large
Load diff
141
setup.cfg
141
setup.cfg
|
|
@ -89,16 +89,33 @@ extend-ignore =
|
|||
# Comply with black's style.
|
||||
# Source: https://github.com/psf/black/blob/master/docs/compatible_configs.md#flake8
|
||||
E203, W503, WPS348,
|
||||
# Google's Python Style Guide is not reStructuredText
|
||||
# until after being processed by Sphinx Napoleon.
|
||||
# Source: https://github.com/peterjc/flake8-rst-docstrings/issues/17
|
||||
RST201,RST203,RST210,RST213,RST301,
|
||||
# String constant over-use is checked visually by the programmer.
|
||||
WPS226,
|
||||
# Allow underscores in numbers.
|
||||
WPS303,
|
||||
# f-strings are ok.
|
||||
WPS305,
|
||||
# Classes should not have to specify a base class.
|
||||
WPS306,
|
||||
# Let's be modern: The Walrus is ok.
|
||||
WPS332,
|
||||
# Let's not worry about the number of noqa's.
|
||||
WPS402,
|
||||
# Putting logic into __init__.py files may be justified.
|
||||
WPS412,
|
||||
# Allow multiple assignment, e.g., x = y = 123
|
||||
WPS429,
|
||||
# There are no magic numbers.
|
||||
WPS432,
|
||||
|
||||
per-file-ignores =
|
||||
# Top-levels of a sub-packages are intended to import a lot.
|
||||
**/__init__.py:
|
||||
F401,WPS201,
|
||||
docs/conf.py:
|
||||
# Allow shadowing built-ins and reading __*__ variables.
|
||||
WPS125,WPS609,
|
||||
|
|
@ -108,14 +125,12 @@ per-file-ignores =
|
|||
migrations/versions/*.py:
|
||||
# Type annotations are not strictly enforced.
|
||||
ANN0, ANN2,
|
||||
# Do not worry about SQL injection here.
|
||||
S608,
|
||||
# File names of revisions are ok.
|
||||
WPS114,WPS118,
|
||||
# Revisions may have too many expressions.
|
||||
WPS204,WPS213,
|
||||
# No overuse of string constants (e.g., 'RESTRICT').
|
||||
WPS226,
|
||||
# Too many noqa's are ok.
|
||||
WPS402,
|
||||
noxfile.py:
|
||||
# Type annotations are not strictly enforced.
|
||||
ANN0, ANN2,
|
||||
|
|
@ -123,38 +138,70 @@ per-file-ignores =
|
|||
WPS202,
|
||||
# TODO (isort): Remove after simplifying the nox session "lint".
|
||||
WPS213,
|
||||
# No overuse of string constants (e.g., '--version').
|
||||
WPS226,
|
||||
# The noxfile is rather long => allow many noqa's.
|
||||
WPS402,
|
||||
src/urban_meal_delivery/configuration.py:
|
||||
# Allow upper case class variables within classes.
|
||||
WPS115,
|
||||
# Numbers are normal in config files.
|
||||
WPS432,
|
||||
src/urban_meal_delivery/db/addresses.py:
|
||||
WPS226,
|
||||
src/urban_meal_delivery/db/orders.py:
|
||||
WPS226,
|
||||
src/urban_meal_delivery/console/forecasts.py:
|
||||
# The module is not too complex.
|
||||
WPS232,
|
||||
src/urban_meal_delivery/db/customers.py:
|
||||
# The module is not too complex.
|
||||
WPS232,
|
||||
src/urban_meal_delivery/db/restaurants.py:
|
||||
# The module is not too complex.
|
||||
WPS232,
|
||||
src/urban_meal_delivery/forecasts/methods/decomposition.py:
|
||||
# The module is not too complex.
|
||||
WPS232,
|
||||
src/urban_meal_delivery/forecasts/methods/extrapolate_season.py:
|
||||
# The module is not too complex.
|
||||
WPS232,
|
||||
src/urban_meal_delivery/forecasts/models/tactical/horizontal.py:
|
||||
# The many noqa's are ok.
|
||||
WPS403,
|
||||
src/urban_meal_delivery/forecasts/timify.py:
|
||||
# No SQL injection as the inputs come from a safe source.
|
||||
S608,
|
||||
# The many noqa's are ok.
|
||||
WPS403,
|
||||
tests/*.py:
|
||||
# Type annotations are not strictly enforced.
|
||||
ANN0, ANN2,
|
||||
# The `Meta` class inside the factory_boy models do not need a docstring.
|
||||
D106,
|
||||
# `assert` statements are ok in the test suite.
|
||||
S101,
|
||||
# The `random` module is not used for cryptography.
|
||||
S311,
|
||||
# Shadowing outer scopes occurs naturally with mocks.
|
||||
WPS442,
|
||||
# Test names may be longer than 40 characters.
|
||||
WPS118,
|
||||
# Modules may have many test cases.
|
||||
WPS202,WPS204,WPS214,
|
||||
# No overuse of string constants (e.g., '__version__').
|
||||
WPS226,
|
||||
# Numbers are normal in test cases as expected results.
|
||||
WPS432,
|
||||
# Do not check for Jones complexity in the test suite.
|
||||
WPS221,
|
||||
# "Private" methods are really just a convention for
|
||||
# fixtures without a return value.
|
||||
WPS338,
|
||||
# We do not care about the number of "# noqa"s in the test suite.
|
||||
WPS402,
|
||||
# Allow closures.
|
||||
WPS430,
|
||||
# When testing, it is normal to use implementation details.
|
||||
WPS437,
|
||||
|
||||
# Explicitly set mccabe's maximum complexity to 10 as recommended by
|
||||
# Thomas McCabe, the inventor of the McCabe complexity, and the NIST.
|
||||
# Source: https://en.wikipedia.org/wiki/Cyclomatic_complexity#Limiting_complexity_during_development
|
||||
max-complexity = 10
|
||||
|
||||
# Allow more than wemake-python-styleguide's 5 local variables per function.
|
||||
max-local-variables = 8
|
||||
|
||||
# Allow more than wemake-python-styleguide's 7 methods per class.
|
||||
max-methods = 12
|
||||
|
||||
# Comply with black's style.
|
||||
# Source: https://github.com/psf/black/blob/master/docs/the_black_code_style.md#line-length
|
||||
max-line-length = 88
|
||||
|
|
@ -166,6 +213,7 @@ show-source = true
|
|||
# wemake-python-styleguide's settings
|
||||
# ===================================
|
||||
allowed-domain-names =
|
||||
data,
|
||||
obj,
|
||||
param,
|
||||
result,
|
||||
|
|
@ -217,53 +265,28 @@ single_line_exclusions = typing
|
|||
[mypy]
|
||||
cache_dir = .cache/mypy
|
||||
|
||||
[mypy-dotenv]
|
||||
[mypy-folium.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-matplotlib.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-nox.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-numpy.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-packaging]
|
||||
ignore_missing_imports = true
|
||||
[mypy-pandas]
|
||||
ignore_missing_imports = true
|
||||
[mypy-pytest]
|
||||
ignore_missing_imports = true
|
||||
[mypy-rpy2.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-sqlalchemy.*]
|
||||
ignore_missing_imports = true
|
||||
|
||||
|
||||
[pylint.FORMAT]
|
||||
# Comply with black's style.
|
||||
max-line-length = 88
|
||||
|
||||
[pylint.MESSAGES CONTROL]
|
||||
disable =
|
||||
# We use TODO's to indicate locations in the source base
|
||||
# that must be worked on in the near future.
|
||||
fixme,
|
||||
# Too many false positives and cannot be disabled within a file.
|
||||
# Source: https://github.com/PyCQA/pylint/issues/214
|
||||
duplicate-code,
|
||||
# Comply with black's style.
|
||||
bad-continuation, bad-whitespace,
|
||||
# =====================
|
||||
# flake8 de-duplication
|
||||
# Source: https://pylint.pycqa.org/en/latest/faq.html#i-am-using-another-popular-linter-alongside-pylint-which-messages-should-i-disable-to-avoid-duplicates
|
||||
# =====================
|
||||
# mccabe
|
||||
too-many-branches,
|
||||
# pep8-naming
|
||||
bad-classmethod-argument, bad-mcs-classmethod-argument,
|
||||
invalid-name, no-self-argument,
|
||||
# pycodestyle
|
||||
bad-indentation, bare-except, line-too-long, missing-final-newline,
|
||||
multiple-statements, trailing-whitespace, unnecessary-semicolon, unneeded-not,
|
||||
# pydocstyle
|
||||
missing-class-docstring, missing-function-docstring, missing-module-docstring,
|
||||
# pyflakes
|
||||
undefined-variable, unused-import, unused-variable,
|
||||
# wemake-python-styleguide
|
||||
redefined-outer-name,
|
||||
|
||||
[pylint.REPORTS]
|
||||
score = no
|
||||
[mypy-statsmodels.*]
|
||||
ignore_missing_imports = true
|
||||
[mypy-utm.*]
|
||||
ignore_missing_imports = true
|
||||
|
||||
|
||||
[tool:pytest]
|
||||
|
|
@ -273,5 +296,9 @@ cache_dir = .cache/pytest
|
|||
console_output_style = count
|
||||
env =
|
||||
TESTING=true
|
||||
filterwarnings =
|
||||
ignore:::patsy.*
|
||||
markers =
|
||||
e2e: integration tests, inlc., for example, tests touching a database
|
||||
db: (integration) tests touching the database
|
||||
e2e: non-db and non-r integration tests
|
||||
r: (integration) tests using rpy2
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ Example:
|
|||
>>> umd.__version__ != '0.0.0'
|
||||
True
|
||||
"""
|
||||
# The config object must come before all other project-internal imports.
|
||||
from urban_meal_delivery.configuration import config # isort:skip
|
||||
|
||||
import os as _os
|
||||
from importlib import metadata as _metadata
|
||||
|
||||
from urban_meal_delivery import configuration as _configuration
|
||||
from urban_meal_delivery import db
|
||||
from urban_meal_delivery import forecasts
|
||||
|
||||
|
||||
try:
|
||||
|
|
@ -24,14 +26,3 @@ else:
|
|||
__author__ = _pkg_info['author']
|
||||
__pkg_name__ = _pkg_info['name']
|
||||
__version__ = _pkg_info['version']
|
||||
|
||||
|
||||
# Global `config` object to be used in the package.
|
||||
config: _configuration.Config = _configuration.make_config(
|
||||
'testing' if _os.getenv('TESTING') else 'production',
|
||||
)
|
||||
|
||||
|
||||
# Import `db` down here as it depends on `config`.
|
||||
# pylint:disable=wrong-import-position
|
||||
from urban_meal_delivery import db # noqa:E402,F401 isort:skip
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue