Run the backend suite on Dev Postgres instead of isolated SQLite.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
709370da93
commit
36b9a2e424
34 files changed
+234
-79
No files matched your search
@@ -0,0 +1,83 @@
|
||||
"""Postgres test engine on the existing Dev instance.
|
||||
|
||||
Two Compose Postgres instances exist: Dev and Prod. Tests use the Dev
|
||||
instance, database `kansho_test` beside `kansho_dev`. That is not a third
|
||||
server. The suite drops and recreates `public` on the test database only,
|
||||
so personal Dev/Prod journals are not wiped.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
LIVE_DB_NAMES = frozenset(
|
||||
{
|
||||
"kansho",
|
||||
"kansho_dev",
|
||||
"postgres",
|
||||
"template0",
|
||||
"template1",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _backend() -> str:
|
||||
return (os.environ.get("KANSHO_DB_BACKEND") or "").strip().lower()
|
||||
|
||||
|
||||
def _db_name() -> str:
|
||||
return (os.environ.get("DB_NAME") or "").strip()
|
||||
|
||||
|
||||
def _require_test_database() -> None:
|
||||
backend = _backend()
|
||||
if backend not in {"postgres", "postgresql", "pg"}:
|
||||
raise SystemExit(
|
||||
"Backend-Tests laufen gegen PostgreSQL (KANSHO_DB_BACKEND=postgres), "
|
||||
"dieselbe Engine wie Dev und Prod. SQLite-Testdateien sind abgelöst. "
|
||||
"Gitea nutzt auf der Dev-Postgres die Datenbank kansho_test "
|
||||
"(neben kansho_dev, keine dritte Instanz)."
|
||||
)
|
||||
name = _db_name().lower()
|
||||
if not name:
|
||||
raise SystemExit(
|
||||
"DB_NAME fehlt. Tests nutzen auf der Dev-Postgres die Datenbank kansho_test, "
|
||||
"nicht kansho_dev."
|
||||
)
|
||||
if name in LIVE_DB_NAMES or not name.endswith("_test"):
|
||||
raise SystemExit(
|
||||
f"DB_NAME={_db_name()!r} ist für Tests gesperrt. "
|
||||
"Die Suite setzt das Schema zurück und darf daher nicht kansho oder kansho_dev treffen."
|
||||
)
|
||||
|
||||
|
||||
def reset_postgres_schema() -> None:
|
||||
import psycopg
|
||||
|
||||
from sql_compat import postgres_connect_kwargs
|
||||
|
||||
kwargs = postgres_connect_kwargs()
|
||||
if "conninfo" in kwargs:
|
||||
conn = psycopg.connect(kwargs["conninfo"], autocommit=True)
|
||||
else:
|
||||
conn = psycopg.connect(autocommit=True, **kwargs)
|
||||
try:
|
||||
conn.execute("DROP SCHEMA IF EXISTS public CASCADE")
|
||||
conn.execute("CREATE SCHEMA public")
|
||||
conn.execute("GRANT ALL ON SCHEMA public TO PUBLIC")
|
||||
conn.execute("GRANT ALL ON SCHEMA public TO CURRENT_USER")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def configure_test_engine() -> None:
|
||||
"""Call after sys.path includes backend/, before importing main/db."""
|
||||
os.environ.setdefault("KANSHO_PROVIDER_KEY", "")
|
||||
os.environ.setdefault("KANSHO_DETECT_PROVIDER_KEY", "")
|
||||
os.environ.pop("KANSHO_DB_PATH", None)
|
||||
_require_test_database()
|
||||
try:
|
||||
reset_postgres_schema()
|
||||
except Exception as exc: # noqa: BLE001 — fail before the suite touches a live DB
|
||||
print(f"Postgres-Testschema konnte nicht zurückgesetzt werden: {exc}", file=sys.stderr)
|
||||
raise SystemExit(1) from exc
|
||||
Reference in new issue
Block a user