Load the test schema once and truncate data between files.
Deploy Development / deploy (push) Successful in 56s
Test Suite / pytest-backend (push) Successful in 2m43s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 15s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-09-07 15:07:33 +02:00
1 parent 3c0213ae74
commit 79e4fd0d4b
8 files changed
+78 -25

No files matched your search

+20 -12
View File
@@ -1,9 +1,9 @@
"""pytest runner for the existing test_*.py scripts.
Each file stays one case (`main()`) until it is split into native pytest
functions. Integration files reset `kansho_test` only; `kansho_dev` is never
the test target. Provider keys and fake flags are restored per file so the
shared pytest process matches the old one-script-per-process isolation.
functions. Schema is loaded once per session; each integration file only
truncates data so kansho_dev is never the target and CI does not reload
schema.sql twenty times.
"""
from __future__ import annotations
@@ -21,6 +21,7 @@ from tests.harness import ( # noqa: E402
apply_module_env,
is_postgres_test_database,
prepare_test_env,
reset_postgres_data,
reset_postgres_schema,
)
@@ -64,6 +65,20 @@ class KanshoScriptModule(pytest.Module):
yield pytest.Function.from_parent(self, name="test_main", callobj=main)
@pytest.fixture(scope="session")
def _session_postgres():
if not is_postgres_test_database():
pytest.skip(
"integration requires KANSHO_DB_BACKEND=postgres and DB_NAME ending _test "
"(Gitea: kansho_test on the Dev Postgres, never kansho_dev)"
)
reset_postgres_schema()
from db import init_db
init_db()
yield
@pytest.fixture(scope="module", autouse=True)
def _module_backend(request: pytest.FixtureRequest):
source = Path(str(request.path))
@@ -74,15 +89,8 @@ def _module_backend(request: pytest.FixtureRequest):
if stem in UNIT_MODULES:
yield
return
if not is_postgres_test_database():
pytest.skip(
"integration requires KANSHO_DB_BACKEND=postgres and DB_NAME ending _test "
"(Gitea: kansho_test on the Dev Postgres, never kansho_dev)"
)
reset_postgres_schema()
from db import init_db
init_db()
request.getfixturevalue("_session_postgres")
reset_postgres_data()
yield
finally:
os.environ.clear()
+31
View File
@@ -89,6 +89,7 @@ def _require_test_database() -> None:
def reset_postgres_schema() -> None:
import psycopg
from db_init import mark_schema_dirty
from sql_compat import postgres_connect_kwargs
kwargs = postgres_connect_kwargs()
@@ -103,6 +104,36 @@ def reset_postgres_schema() -> None:
conn.execute("GRANT ALL ON SCHEMA public TO CURRENT_USER")
finally:
conn.close()
mark_schema_dirty()
def reset_postgres_data() -> None:
"""Clear rows on kansho_test without reloading schema.sql."""
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:
rows = conn.execute(
"""
SELECT tablename FROM pg_tables
WHERE schemaname = 'public' AND tablename <> 'schema_migrations'
"""
).fetchall()
names = [row[0] for row in rows]
if names:
joined = ", ".join('"' + name.replace('"', '""') + '"' for name in names)
conn.execute(f"TRUNCATE TABLE {joined} RESTART IDENTITY CASCADE")
finally:
conn.close()
from db import refresh_runtime_seed
refresh_runtime_seed()
def configure_test_engine() -> None:
+2 -3
View File
@@ -209,10 +209,9 @@ def main() -> None:
test_compile_flags()
test_budget_omission_in_effective_trace()
from tests.harness import reset_postgres_schema
from tests.harness import reset_postgres_data
reset_postgres_schema()
init_db()
reset_postgres_data()
reset_debug()
with TestClient(app) as client:
setup = client.post(
@@ -268,10 +268,9 @@ def test_later_semantic_seed_creates_successor() -> None:
def test_legacy_generate_trace_and_prompt() -> None:
from tests.harness import reset_postgres_schema
from tests.harness import reset_postgres_data
reset_postgres_schema()
init_db()
reset_postgres_data()
client = TestClient(app)
setup = client.post(
"/api/auth/setup",