Add database migrations

- use Alembic to migrate the PostgreSQL database
  + create initial migration script to set up the database,
    as an alternative to db.Base.metadata.create_all()
  + integrate Alembic into the test suite; the db_engine fixture
    now has two modes:
    * create the latest version of tables all at once
    * invoke `alembic upgrade head`
    => the "e2e" tests are all run twice, once in each mode; this
       ensures that the migration scripts re-create the same database
       schema as db.Base.metadata.create_all() would
    * in both modes, a temporary PostgreSQL schema is used to create the
      tables in
    => could now run "e2e" tests against production database and still
       have isolation
- make the configuration module public (to be used by Alembic)
- adjust linting rules for Alembic
This commit is contained in:
Alexander Hess 2020-08-09 17:14:23 +02:00
commit a16c260543
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
14 changed files with 1104 additions and 35 deletions

12
tests/conftest.py Normal file
View file

@ -0,0 +1,12 @@
"""Utils for testing the entire package."""
import os
from urban_meal_delivery import config
if not os.getenv('TESTING'):
raise RuntimeError('Tests must be executed with TESTING set in the environment')
if not config.TESTING:
raise RuntimeError('The testing configuration was not loaded')

View file

@ -3,30 +3,49 @@
import datetime
import pytest
from sqlalchemy import schema
from alembic import command as migrations_cmd
from alembic import config as migrations_config
from urban_meal_delivery import config
from urban_meal_delivery import db
@pytest.fixture(scope='session')
def db_engine():
@pytest.fixture(scope='session', params=['all_at_once', 'sequentially'])
def db_engine(request):
"""Create all tables given the ORM models.
The tables are put into a distinct PostgreSQL schema
that is removed after all tests are over.
The engine used to do that is yielded.
There are two modes for this fixture:
- "all_at_once": build up the tables all at once with MetaData.create_all()
- "sequentially": build up the tables sequentially with `alembic upgrade head`
This ensures that Alembic's migration files are consistent.
"""
engine = db.make_engine()
engine.execute(schema.CreateSchema(config.CLEAN_SCHEMA))
db.Base.metadata.create_all(engine)
if request.param == 'all_at_once':
engine.execute(f'CREATE SCHEMA {config.CLEAN_SCHEMA};')
db.Base.metadata.create_all(engine)
else:
cfg = migrations_config.Config('alembic.ini')
migrations_cmd.upgrade(cfg, 'head')
try:
yield engine
finally:
engine.execute(schema.DropSchema(config.CLEAN_SCHEMA, cascade=True))
engine.execute(f'DROP SCHEMA {config.CLEAN_SCHEMA} CASCADE;')
if request.param == 'sequentially':
tmp_alembic_version = f'{config.ALEMBIC_TABLE}_{config.CLEAN_SCHEMA}'
engine.execute(
f'DROP TABLE {config.ALEMBIC_TABLE_SCHEMA}.{tmp_alembic_version};',
)
@pytest.fixture

View file

@ -2,7 +2,7 @@
import pytest
from urban_meal_delivery import _config as config_mod # noqa:WPS450
from urban_meal_delivery import configuration
envs = ['production', 'testing']
@ -11,7 +11,7 @@ envs = ['production', 'testing']
@pytest.mark.parametrize('env', envs)
def test_config_repr(env):
"""Config objects have the text representation '<configuration>'."""
config = config_mod.get_config(env)
config = configuration.make_config(env)
assert str(config) == '<configuration>'
@ -19,18 +19,18 @@ def test_config_repr(env):
def test_invalid_config():
"""There are only 'production' and 'testing' configurations."""
with pytest.raises(ValueError, match="'production' or 'testing'"):
config_mod.get_config('invalid')
configuration.make_config('invalid')
@pytest.mark.parametrize('env', envs)
def test_database_uri_set(env, monkeypatch):
"""Package does NOT emit warning if DATABASE_URI is set."""
uri = 'postgresql://user:password@localhost/db'
monkeypatch.setattr(config_mod.ProductionConfig, 'DATABASE_URI', uri)
monkeypatch.setattr(config_mod.TestingConfig, 'DATABASE_URI', uri)
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', uri)
monkeypatch.setattr(configuration.TestingConfig, 'DATABASE_URI', uri)
with pytest.warns(None) as record:
config_mod.get_config(env)
configuration.make_config(env)
assert len(record) == 0 # noqa:WPS441,WPS507
@ -38,16 +38,17 @@ def test_database_uri_set(env, monkeypatch):
@pytest.mark.parametrize('env', envs)
def test_no_database_uri_set(env, monkeypatch):
"""Package does not work without DATABASE_URI set in the environment."""
monkeypatch.setattr(config_mod.ProductionConfig, 'DATABASE_URI', None)
monkeypatch.setattr(config_mod.TestingConfig, 'DATABASE_URI', None)
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', None)
monkeypatch.setattr(configuration.TestingConfig, 'DATABASE_URI', None)
with pytest.warns(UserWarning, match='no DATABASE_URI'):
config_mod.get_config(env)
configuration.make_config(env)
def test_random_testing_schema():
"""CLEAN_SCHEMA is randomized if not seti explicitly."""
result = config_mod.random_schema_name()
"""CLEAN_SCHEMA is randomized if not set explicitly."""
result = configuration.random_schema_name()
assert isinstance(result, str)
assert len(result) <= 10
assert result.startswith('temp_')
assert len(result) == 15