63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Gemeinsame Test-Hilfen.
|
|
|
|
Tests mit dem Marker `db` laufen nur, wenn SURVEYBOX_TEST_DB=1 gesetzt ist
|
|
und die SURVEYBOX_DB_*-Variablen auf eine Wegwerf-Datenbank zeigen (CI).
|
|
Die Datenbank wird dabei geleert!
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
|
|
if os.environ.get("SURVEYBOX_TEST_DB") == "1":
|
|
return
|
|
skip = pytest.mark.skip(reason="SURVEYBOX_TEST_DB=1 nicht gesetzt")
|
|
for item in items:
|
|
if "db" in item.keywords:
|
|
item.add_marker(skip)
|
|
|
|
|
|
@pytest.fixture
|
|
def db_settings(tmp_path, monkeypatch: pytest.MonkeyPatch): # type: ignore[no-untyped-def]
|
|
"""Einstellungen mit leerem Datenverzeichnis und frischem Schema."""
|
|
pytest.importorskip("sqlalchemy")
|
|
from sqlalchemy import text
|
|
|
|
from surveybox import db
|
|
from surveybox import settings as settings_mod
|
|
|
|
monkeypatch.setenv("SURVEYBOX_DATA_DIR", str(tmp_path / "data"))
|
|
settings_mod.get_settings.cache_clear()
|
|
db.get_engine.cache_clear()
|
|
engine = db.get_engine()
|
|
with engine.begin() as conn:
|
|
conn.execute(text(f"DROP SCHEMA IF EXISTS {db.DB_SCHEMA} CASCADE"))
|
|
yield settings_mod.get_settings()
|
|
engine.dispose()
|
|
settings_mod.get_settings.cache_clear()
|
|
db.get_engine.cache_clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def app(db_settings): # type: ignore[no-untyped-def]
|
|
"""Web-Anwendung auf frisch migrierter Datenbank."""
|
|
from surveybox import db
|
|
from surveybox.upgrade import run_upgrade
|
|
from surveybox.web.app import create_app
|
|
|
|
run_upgrade(db_settings, db.get_engine())
|
|
return create_app()
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app): # type: ignore[no-untyped-def]
|
|
from helpers import new_client
|
|
|
|
with new_client(app) as c:
|
|
yield c
|