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
|
2026-12-01 15:51:24 +01:00
|
|
|
import random
|
2026-12-02 00:19:19 +01:00
|
|
|
import typing as t
|
2020-08-09 17:14:23 +02:00
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
import pytest
|
|
|
|
|
import sqlalchemy as sa
|
|
|
|
|
from alembic import command as alembic_cmd
|
|
|
|
|
from alembic import config as alembic_config
|
2026-12-01 15:51:24 +01:00
|
|
|
from factory import random as factory_random
|
2021-01-06 16:17:05 +01:00
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
2026-11-23 18:06:53 +01:00
|
|
|
from velofy import config as pkg_config
|
|
|
|
|
from velofy import db
|
2020-08-09 17:14:23 +02:00
|
|
|
|
|
|
|
|
|
2026-11-30 14:56:11 +01:00
|
|
|
# Register the shared fixtures in `tests.db.fake_data` here,
|
|
|
|
|
# as tests/console/, tests/db/, and tests/forecasts/ use them
|
|
|
|
|
pytest_plugins = [
|
|
|
|
|
"tests.db.fake_data.fixture_makers",
|
|
|
|
|
"tests.db.fake_data.static_fixtures",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2020-12-14 15:15:08 +01:00
|
|
|
# The TESTING environment variable is set
|
2026-12-03 10:03:26 +01:00
|
|
|
# in pyproject.toml in pytest's config section
|
2020-08-09 17:14:23 +02:00
|
|
|
if not os.getenv("TESTING"):
|
2026-11-30 22:59:41 +01:00
|
|
|
msg = "Tests must be executed with TESTING set in the environment"
|
|
|
|
|
raise RuntimeError(msg)
|
2020-08-09 17:14:23 +02:00
|
|
|
|
|
|
|
|
if not pkg_config.TESTING:
|
2026-11-30 22:59:41 +01:00
|
|
|
msg = "The testing configuration was not loaded"
|
|
|
|
|
raise RuntimeError(msg)
|
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.
|
|
|
|
|
"""
|
2026-12-03 10:52:09 +01:00
|
|
|
# Each of the two `params` needs a fresh database connection
|
|
|
|
|
# => Otherwise, the first test of the second parameter run fails
|
2021-01-06 16:17:05 +01:00
|
|
|
engine = sa.create_engine(pkg_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 {pkg_config.CLEAN_SCHEMA};"))
|
|
|
|
|
db.Base.metadata.create_all(connection)
|
2021-01-06 16:17:05 +01:00
|
|
|
else:
|
|
|
|
|
cfg = alembic_config.Config("alembic.ini")
|
|
|
|
|
alembic_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
|
2026-12-03 10:03:26 +01:00
|
|
|
# may have left open, which would otherwise swallow the `DROP`s below
|
2026-11-23 16:45:38 +01:00
|
|
|
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 {pkg_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"{pkg_config.ALEMBIC_TABLE}_{pkg_config.CLEAN_SCHEMA}"
|
2026-11-30 20:19:35 +01:00
|
|
|
)
|
2026-11-23 16:45:38 +01:00
|
|
|
cleanup.execute(
|
2026-11-24 16:05:54 +01:00
|
|
|
sa.text(
|
|
|
|
|
f"DROP TABLE {pkg_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
|
2026-12-02 00:19:19 +01:00
|
|
|
def db_session(db_connection, monkeypatch):
|
2021-01-06 16:17:05 +01:00
|
|
|
"""A SQLAlchemy session that rolls back everything after a test case."""
|
|
|
|
|
# Begin the outermost transaction
|
2026-12-03 10:03:26 +01:00
|
|
|
# that is rolled back at the end of each test case
|
2021-01-06 16:17:05 +01:00
|
|
|
transaction = db_connection.begin()
|
|
|
|
|
|
2026-12-03 10:52:09 +01:00
|
|
|
# The session joins the `transaction`'s connection to be rolled back with it
|
|
|
|
|
# => Commits inside a test case only release a savepoint
|
2026-11-30 10:23:39 +01:00
|
|
|
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,
|
2026-12-03 10:03:26 +01:00
|
|
|
# which is used heavily in the code base
|
2026-12-02 00:19:19 +01:00
|
|
|
monkeypatch.setattr(db, "session", session)
|
2021-01-06 16:17:05 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
session.close()
|
2021-09-12 12:13:26 +02:00
|
|
|
|
2026-11-30 10:23:39 +01:00
|
|
|
transaction.rollback()
|
2026-12-01 15:51:24 +01:00
|
|
|
|
|
|
|
|
|
2026-12-02 00:19:19 +01:00
|
|
|
class _NoSession:
|
|
|
|
|
"""Placeholder for `db.session` in tests without the `db_session` fixture."""
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, _name: str) -> t.NoReturn:
|
|
|
|
|
msg = "Use the `db_session` fixture to access the database"
|
|
|
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def _no_global_session(monkeypatch):
|
|
|
|
|
"""Prevent tests from opening real sessions via `db.session`.
|
|
|
|
|
|
|
|
|
|
Such a session would hold locks in the test schema
|
|
|
|
|
and block the `DROP SCHEMA` at the end of the test run.
|
|
|
|
|
"""
|
|
|
|
|
monkeypatch.setattr(db, "session", _NoSession())
|
|
|
|
|
|
|
|
|
|
|
2026-12-01 15:51:24 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def _seed_random() -> None:
|
|
|
|
|
"""Make the fake data reproducible across test runs."""
|
|
|
|
|
random.seed(42)
|
|
|
|
|
factory_random.reseed_random(42)
|