From 0c57b54d77683bddd387f9f90e802b8b97051336 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 20 Jun 2026 06:14:55 +0000 Subject: [PATCH] fix(e2e): guard spend-log truncate on skip and stop returning unrelated priced rows --- tests/e2e/conftest.py | 22 +++++++++++++++---- .../test_custom_pricing_e2e.py | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.py index 953a11f9ca2..f792244fd57 100644 --- a/tests/e2e/conftest.py +++ b/tests/e2e/conftest.py @@ -22,6 +22,9 @@ from e2e_config import PROXY_BASE_URL from lifecycle import GatewayProvider, ResourceManager +_E2E_TEST_RAN = pytest.StashKey[bool]() + + def pytest_configure(config: pytest.Config) -> None: config.addinivalue_line( "markers", @@ -29,12 +32,23 @@ def pytest_configure(config: pytest.Config) -> None: ) +def pytest_runtest_call(item: pytest.Item) -> None: + """Mark that an e2e test body actually ran (not skipped at setup). Skipped + sessions never reach this hook, so the session-finish cleanup can use it as a + guard before truncating the spend-log DB.""" + item.session.stash[_E2E_TEST_RAN] = True + + def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None: """Once the whole e2e session is done (all suites), truncate the spend logs so - the DB doesn't accumulate test rows. Best-effort: a cleanup failure (no DB - reachable) must not fail the run. The spend_tracking dir goes on sys.path only - for this import and is removed after, so a broader `pytest tests/` run is not - left with a mutated path.""" + the DB doesn't accumulate test rows. Skipped sessions (no live proxy, no test + actually executed) leave the DB alone so a `DATABASE_URL` pointing at a shared + instance is never wiped without an e2e run. Best-effort: a cleanup failure (no + DB reachable) must not fail the run. The spend_tracking dir goes on sys.path + only for this import and is removed after, so a broader `pytest tests/` run is + not left with a mutated path.""" + if not session.stash.get(_E2E_TEST_RAN, False): + return spend_dir = str(Path(__file__).parent / "spend_tracking") sys.path.insert(0, spend_dir) try: diff --git a/tests/e2e/llm_translation/test_custom_pricing_e2e.py b/tests/e2e/llm_translation/test_custom_pricing_e2e.py index c4c5b72ce62..6c8619a075e 100644 --- a/tests/e2e/llm_translation/test_custom_pricing_e2e.py +++ b/tests/e2e/llm_translation/test_custom_pricing_e2e.py @@ -126,7 +126,7 @@ def _poll_breakdown_row( for row in priced: if response_id and row.request_id == response_id: return row - if priced: + if priced and response_id is None: return priced[0] time.sleep(client.gateway.poll_interval) pytest.fail("no spend row with a cost breakdown landed before the deadline")