Use globals for the database connection

- remove the factory functions for creating engines and sessions
- define global engine, connection, and session objects to be used
  everywhere in the urban_meal_delivery package
This commit is contained in:
Alexander Hess 2021-01-04 18:50:26 +01:00
commit 2e3ccd14d5
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
8 changed files with 74 additions and 24 deletions

View file

@ -1,6 +1,7 @@
"""Utils for testing the ORM layer."""
import pytest
import sqlalchemy as sa
from alembic import command as migrations_cmd
from alembic import config as migrations_config
from sqlalchemy import orm
@ -26,11 +27,18 @@ def db_connection(request):
This ensures that Alembic's migration files are consistent.
"""
engine = db.make_engine()
# We need a fresh database connection for each of the two `params`.
# Otherwise, the first test of the parameter run second will fail.
engine = sa.create_engine(config.DATABASE_URI)
connection = engine.connect()
# Monkey patch the package's global `engine` and `connection` objects,
# just in case if it is used somewhere in the code base.
db.engine = engine
db.connection = connection
if request.param == 'all_at_once':
engine.execute(f'CREATE SCHEMA {config.CLEAN_SCHEMA};')
connection.execute(f'CREATE SCHEMA {config.CLEAN_SCHEMA};')
db.Base.metadata.create_all(connection)
else:
cfg = migrations_config.Config('alembic.ini')
@ -54,13 +62,17 @@ def db_connection(request):
@pytest.fixture
def db_session(db_connection):
"""A SQLAlchemy session that rolls back everything after a test case."""
# Begin the outer most transaction
# that is rolled back at the end of the test.
# Begin the outermost transaction
# that is rolled back at the end of each test case.
transaction = db_connection.begin()
# Create a session bound on the same connection as the transaction.
# Using any other session would not work.
session_factory = orm.sessionmaker()
session = session_factory(bind=db_connection)
# Create a session bound to the same connection as the `transaction`.
# Using any other session would not result in the roll back.
session = orm.sessionmaker()(bind=db_connection)
# Monkey patch the package's global `session` object,
# which is used heavily in the code base.
db.session = session
try:
yield session

View file

@ -36,11 +36,27 @@ def test_database_uri_set(env, monkeypatch):
@pytest.mark.parametrize('env', envs)
def test_no_database_uri_set(env, monkeypatch):
def test_no_database_uri_set_with_testing_env_var(env, monkeypatch):
"""Package does not work without DATABASE_URI set in the environment."""
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', None)
monkeypatch.setattr(configuration.TestingConfig, 'DATABASE_URI', None)
monkeypatch.setenv('TESTING', 'true')
with pytest.warns(None) as record:
configuration.make_config(env)
assert len(record) == 0 # noqa:WPS441,WPS507
@pytest.mark.parametrize('env', envs)
def test_no_database_uri_set_without_testing_env_var(env, monkeypatch):
"""Package does not work without DATABASE_URI set in the environment."""
monkeypatch.setattr(configuration.ProductionConfig, 'DATABASE_URI', None)
monkeypatch.setattr(configuration.TestingConfig, 'DATABASE_URI', None)
monkeypatch.delenv('TESTING', raising=False)
with pytest.warns(UserWarning, match='no DATABASE_URI'):
configuration.make_config(env)