"""Shared pytest fixtures — Integrationstests benötigen PostgreSQL. Lokal typischerweise nicht verfügbar: Tests laufen nach Push auf develop/main im Backend-Container auf dem Pi (Gitea workflow test.yml). """ from __future__ import annotations import os import pytest os.environ.setdefault("SKIP_DB_MIGRATE", "1") os.environ.setdefault("SKIP_SEEDS", "1") os.environ.setdefault("SKIP_BOOTSTRAP", "1") os.environ.setdefault("SKIP_RIGHTS_SYNC", "1") os.environ.setdefault("SKIP_REGISTRY_SYNC", "1") def _db_available() -> bool: try: import psycopg2 conn = psycopg2.connect( host=os.getenv("DB_HOST", "localhost"), port=os.getenv("DB_PORT", "5432"), dbname=os.getenv("DB_NAME", "kairo_dev"), user=os.getenv("DB_USER", "kairo_dev"), password=os.getenv("DB_PASSWORD", "dev_password"), connect_timeout=3, ) conn.close() return True except Exception: return False _REMOTE_HINT = ( "PostgreSQL lokal nicht erreichbar. " "Backend-Integrationstests laufen nach Push auf develop/main " "im deployten Backend-Container (Gitea: test.yml auf dem Pi)." ) @pytest.fixture(scope="session", autouse=True) def _require_postgresql(): """Session-weiter Guard — verhindert Migrations-Setup ohne DB (keine ERROR-Spirale).""" if not _db_available(): pytest.skip(_REMOTE_HINT) @pytest.fixture(scope="session", autouse=True) def _run_migrations(): import run_migrations assert run_migrations.main() == 0 @pytest.fixture(scope="session", autouse=True) def _seed_hygiene(): """Vor/nach Tests pytest-Artefakte aus der geteilten Dev-DB entfernen.""" import run_seeds run_seeds.main(["--only", "seed_001_cleanup_pytest_artifacts"]) yield run_seeds.main(["--only", "seed_001_cleanup_pytest_artifacts"]) @pytest.fixture(scope="session", autouse=True) def _sync_rights_registry(): import rights_registrations # noqa: F401 from rights_registry import sync_rights_registry_to_db assert sync_rights_registry_to_db() == 0 @pytest.fixture(scope="session", autouse=True) def _sync_prompt_feature_config(): import feature_registrations # noqa: F401 import placeholder_registrations # noqa: F401 import prompt_registrations # noqa: F401 from sync_prompt_feature_config import main as sync_registries assert sync_registries() == 0 @pytest.fixture() def client(): import importlib import main as main_module importlib.reload(main_module) from fastapi.testclient import TestClient return TestClient(main_module.app)