diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 1977101f..40ab9d53 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -380,6 +380,13 @@ async def integration_session( yield session +@pytest_asyncio.fixture +async def patched_db_engine(integration_engine: Any) -> AsyncGenerator[None, None]: + """Patch the global db engine so create_session() uses the test engine.""" + with patch("routstr.core.db.engine", integration_engine): + yield + + class DatabaseSnapshot: """Utility to capture and compare database states""" diff --git a/tests/integration/test_balance_negative_on_cost_overrun.py b/tests/integration/test_balance_negative_on_cost_overrun.py index bfaad274..a304d91d 100644 --- a/tests/integration/test_balance_negative_on_cost_overrun.py +++ b/tests/integration/test_balance_negative_on_cost_overrun.py @@ -179,6 +179,7 @@ async def test_full_cost_charged_when_balance_sufficient_for_overrun( @pytest.mark.asyncio async def test_concurrent_cost_overruns_never_negative( integration_session: AsyncSession, + patched_db_engine: None, ) -> None: """Concurrent finalization with cost overruns must never produce negative balance.""" import asyncio @@ -302,6 +303,7 @@ async def test_zero_free_balance_overrun_is_safe( @pytest.mark.asyncio async def test_parallel_requests_no_free_inference( integration_session: AsyncSession, + patched_db_engine: None, ) -> None: """Second parallel finalization must be charged even when first depleted free balance.""" import asyncio diff --git a/tests/integration/test_reservation_lifecycle.py b/tests/integration/test_reservation_lifecycle.py index d1416bbb..ee3fc875 100644 --- a/tests/integration/test_reservation_lifecycle.py +++ b/tests/integration/test_reservation_lifecycle.py @@ -134,7 +134,9 @@ async def test_finalise_releases_reservation_and_charges_balance( # --------------------------------------------------------------------------- @pytest.mark.asyncio -async def test_concurrent_second_reserve_blocked_when_balance_exhausted() -> None: +async def test_concurrent_second_reserve_blocked_when_balance_exhausted( + patched_db_engine: None, +) -> None: """When two requests race for the same balance, only one succeeds; the other gets 402.""" cost = 300 key_hash = f"test_concurrent_{uuid.uuid4().hex}" @@ -185,7 +187,9 @@ async def test_concurrent_second_reserve_blocked_when_balance_exhausted() -> Non # --------------------------------------------------------------------------- @pytest.mark.asyncio -async def test_three_parallel_reserves_third_blocked() -> None: +async def test_three_parallel_reserves_third_blocked( + patched_db_engine: None, +) -> None: """Balance covers two reservations exactly; the third concurrent request must be blocked.""" cost = 100 key_hash = f"test_three_parallel_{uuid.uuid4().hex}"