hugin/server/tests/conftest.py
2026-10-27 08:33:21 +01:00

49 lines
1.5 KiB
Python

import os
import uuid
from pathlib import Path
import pytest
from sqlalchemy import create_engine, text
from hugin_core.package import Package, load
CONTRACT = Path(__file__).resolve().parents[2] / "contract"
EXAMPLES = CONTRACT / "examples"
def pytest_addoption(parser):
parser.addoption("--update", action="store_true",
help="goldene Bundles unter contract/examples/bundles neu schreiben")
@pytest.fixture
def update(request) -> bool:
return request.config.getoption("--update")
def example(name: str) -> Package:
"""Beispielpaket als <id>/<version>, z. B. pub-munin-nutzen/1.4.0."""
return load(EXAMPLES / "packages" / name)
# HUGIN_TEST_POSTGRES=postgresql+psycopg://user:pw@host:port/postgres lässt die Tests gegen
# PostgreSQL laufen (scripts/postgres-test.sh); jede Test-Funktion bekommt eine eigene Datenbank.
POSTGRES = os.environ.get("HUGIN_TEST_POSTGRES", "")
@pytest.fixture
def database_url(tmp_path):
if not POSTGRES:
yield f"sqlite:///{tmp_path / 'hugin.db'}"
return
name = f"hugin_test_{uuid.uuid4().hex[:12]}"
management = create_engine(POSTGRES, isolation_level="AUTOCOMMIT")
with management.connect() as v:
v.execute(text(f'CREATE DATABASE "{name}"'))
base, _, _ = POSTGRES.rpartition("/")
try:
yield f"{base}/{name}"
finally:
with management.connect() as v:
v.execute(text(f'DROP DATABASE IF EXISTS "{name}" WITH (FORCE)'))
management.dispose()