Deploy Development / deploy (push) Failing after 40s
Testdaten entstehen nur dort und werden danach entfernt, damit Dev und Produktion sie nicht behalten. Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
3.1 KiB
Python
100 lines
3.1 KiB
Python
"""Ephemeral database for the mandatory suite. Dev and prod databases are refused."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
import psycopg
|
|
import pytest
|
|
|
|
TEST_DB = "tsumugu_pytest"
|
|
FORBIDDEN = {"tsumugu", "tsumugu_dev"}
|
|
|
|
if os.environ.get("TSUMUGU_TEST_DB") in FORBIDDEN or os.environ.get("DB_NAME") in FORBIDDEN:
|
|
raise RuntimeError("Die Test-Suite darf nicht gegen Dev- oder Prod-Datenbanken laufen.")
|
|
|
|
os.environ["TSUMUGU_ENV"] = "development"
|
|
os.environ["DB_HOST"] = os.environ.get("TSUMUGU_TEST_HOST", "127.0.0.1")
|
|
os.environ["DB_PORT"] = os.environ.get("TSUMUGU_TEST_PORT", "5432")
|
|
os.environ["DB_NAME"] = TEST_DB
|
|
os.environ["DB_USER"] = os.environ.get("TSUMUGU_TEST_USER", "tsumugu")
|
|
os.environ["DB_PASSWORD"] = os.environ.get("TSUMUGU_TEST_PASSWORD", "pytest")
|
|
os.environ["ALLOWED_ORIGINS"] = "http://testserver"
|
|
os.environ["SEED_DEMO"] = "false"
|
|
os.environ["MEDIA_ROOT"] = tempfile.mkdtemp(prefix="tsumugu-pytest-")
|
|
|
|
|
|
def _admin_dsn() -> str:
|
|
admin_db = os.environ.get("TSUMUGU_TEST_ADMIN_DB", "postgres")
|
|
if admin_db in FORBIDDEN or admin_db == TEST_DB:
|
|
raise RuntimeError("Die Verwaltungsverbindung der Test-Suite darf nicht die Fachdatenbank sein.")
|
|
return (
|
|
f"host={os.environ['DB_HOST']} port={os.environ['DB_PORT']} dbname={admin_db} "
|
|
f"user={os.environ['DB_USER']} password={os.environ['DB_PASSWORD']} connect_timeout=5"
|
|
)
|
|
|
|
|
|
def _reset(settings) -> None:
|
|
with psycopg.connect(settings.dsn(), autocommit=True) as conn:
|
|
conn.execute(
|
|
"""
|
|
TRUNCATE TABLE
|
|
media_operations,
|
|
domain_events,
|
|
auth_accounts,
|
|
data_rooms
|
|
RESTART IDENTITY CASCADE
|
|
"""
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def database():
|
|
if os.environ["DB_NAME"] in FORBIDDEN:
|
|
raise RuntimeError("Die Test-Suite darf nicht gegen Dev- oder Prod-Datenbanken laufen.")
|
|
try:
|
|
admin = psycopg.connect(_admin_dsn(), autocommit=True)
|
|
except psycopg.OperationalError as exc:
|
|
pytest.fail(f"Die Test-Suite braucht ein kurzlebiges PostgreSQL. {exc}")
|
|
admin.execute(f"DROP DATABASE IF EXISTS {TEST_DB} WITH (FORCE)")
|
|
admin.execute(f"CREATE DATABASE {TEST_DB}")
|
|
from app.core.settings import get_settings
|
|
from app.db.migrate import migrate
|
|
|
|
get_settings.cache_clear()
|
|
settings = get_settings()
|
|
assert settings.db_name == TEST_DB
|
|
migrate(settings)
|
|
yield settings
|
|
admin.execute(f"DROP DATABASE IF EXISTS {TEST_DB} WITH (FORCE)")
|
|
admin.close()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def client(database):
|
|
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clean(database, client):
|
|
from app.api import routes
|
|
|
|
routes._login_attempts.clear()
|
|
client.cookies.clear()
|
|
_reset(database)
|
|
yield
|
|
routes._login_attempts.clear()
|
|
client.cookies.clear()
|
|
_reset(database)
|
|
|
|
|
|
@pytest.fixture
|
|
def db(database):
|
|
with psycopg.connect(database.dsn(), row_factory=psycopg.rows.dict_row) as conn:
|
|
yield conn
|