2021-01-06 16:17:05 +01:00
|
|
|
"""Fixtures for testing the entire package.
|
|
|
|
|
|
|
|
|
|
The ORM related fixtures are placed here too as some integration tests
|
|
|
|
|
in the CLI layer need access to the database.
|
|
|
|
|
"""
|
2020-08-09 17:14:23 +02:00
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
import pytest
|
|
|
|
|
import sqlalchemy as sa
|
|
|
|
|
from alembic import command as migrations_cmd
|
|
|
|
|
from alembic import config as migrations_config
|
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
|
|
|
|
from tests.db import fake_data
|
2026-11-23 18:06:53 +01:00
|
|
|
from velofy import config
|
|
|
|
|
from velofy import db
|
2020-08-09 17:14:23 +02:00
|
|
|
|
|
|
|
|
|
2020-12-14 15:15:08 +01:00
|
|
|
# The TESTING environment variable is set
|
2026-11-25 14:43:31 +01:00
|
|
|
# in pyproject.toml in pytest's config section.
|
2020-08-09 17:14:23 +02:00
|
|
|
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")
|
2021-01-06 16:17:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", params=["all_at_once", "sequentially"])
|
|
|
|
|
def db_connection(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 database connection 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.
|
|
|
|
|
"""
|
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
|
|
if request.param == "all_at_once":
|
2026-11-24 16:05:27 +01:00
|
|
|
with connection.begin():
|
|
|
|
|
connection.execute(sa.text(f"CREATE SCHEMA {config.CLEAN_SCHEMA};"))
|
|
|
|
|
db.Base.metadata.create_all(connection)
|
2021-01-06 16:17:05 +01:00
|
|
|
else:
|
|
|
|
|
cfg = migrations_config.Config("alembic.ini")
|
|
|
|
|
migrations_cmd.upgrade(cfg, "head")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
yield connection
|
|
|
|
|
|
|
|
|
|
finally:
|
2026-11-23 16:45:38 +01:00
|
|
|
# Close the connection first: this rolls back any transaction a test
|
|
|
|
|
# may have left open, which would otherwise swallow the `DROP`s below.
|
|
|
|
|
connection.close()
|
2021-01-06 16:17:05 +01:00
|
|
|
|
2026-11-23 16:45:38 +01:00
|
|
|
with engine.begin() as cleanup:
|
2026-11-24 16:05:54 +01:00
|
|
|
cleanup.execute(sa.text(f"DROP SCHEMA {config.CLEAN_SCHEMA} CASCADE;"))
|
2021-01-06 16:17:05 +01:00
|
|
|
|
2026-11-23 16:45:38 +01:00
|
|
|
if request.param == "sequentially":
|
|
|
|
|
tmp_alembic_version = f"{config.ALEMBIC_TABLE}_{config.CLEAN_SCHEMA}"
|
|
|
|
|
cleanup.execute(
|
2026-11-24 16:05:54 +01:00
|
|
|
sa.text(
|
|
|
|
|
f"DROP TABLE {config.ALEMBIC_TABLE_SCHEMA}.{tmp_alembic_version};", # noqa:E501
|
|
|
|
|
),
|
2026-11-23 16:45:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
engine.dispose()
|
2021-01-06 16:17:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def db_session(db_connection):
|
|
|
|
|
"""A SQLAlchemy session that rolls back everything after a test case."""
|
|
|
|
|
# Begin the outermost transaction
|
|
|
|
|
# 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.
|
2026-11-30 10:23:39 +01:00
|
|
|
# Commits inside a test case then only release a savepoint.
|
|
|
|
|
session = orm.Session(bind=db_connection, join_transaction_mode="create_savepoint")
|
2021-01-06 16:17:05 +01:00
|
|
|
|
|
|
|
|
# Monkey patch the package's global `session` object,
|
|
|
|
|
# which is used heavily in the code base.
|
|
|
|
|
db.session = session
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
session.close()
|
2021-09-12 12:13:26 +02:00
|
|
|
|
2026-11-30 10:23:39 +01:00
|
|
|
transaction.rollback()
|
2021-01-06 16:17:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Import the fixtures from the `fake_data` sub-package.
|
|
|
|
|
|
|
|
|
|
make_address = fake_data.make_address
|
|
|
|
|
make_courier = fake_data.make_courier
|
|
|
|
|
make_customer = fake_data.make_customer
|
|
|
|
|
make_order = fake_data.make_order
|
2021-09-16 11:58:55 +02:00
|
|
|
make_replay_order = fake_data.make_replay_order
|
2021-01-06 16:17:05 +01:00
|
|
|
make_restaurant = fake_data.make_restaurant
|
|
|
|
|
|
|
|
|
|
address = fake_data.address
|
|
|
|
|
city = fake_data.city
|
|
|
|
|
city_data = fake_data.city_data
|
|
|
|
|
courier = fake_data.courier
|
|
|
|
|
customer = fake_data.customer
|
|
|
|
|
order = fake_data.order
|
2021-09-16 11:58:55 +02:00
|
|
|
pre_order = fake_data.pre_order
|
|
|
|
|
replayed_order = fake_data.replayed_order
|
2021-01-06 16:17:05 +01:00
|
|
|
restaurant = fake_data.restaurant
|
|
|
|
|
grid = fake_data.grid
|
|
|
|
|
pixel = fake_data.pixel
|
2021-09-16 11:58:55 +02:00
|
|
|
simulation = fake_data.simulation
|
|
|
|
|
simulation_data = fake_data.simulation_data
|