fix(pry): test_check_selectors_empty → per-test fresh engine fixture

The module-level db._engine was being reused across tests. Tests that
went through the temp_db fixture wrote StructureSnapshot rows to a
tmp_path sqlite file, but test_check_selectors_empty runs without that
fixture, so on the second run the engine pointed at the polluted file
instead of starting empty — RuntimeError when StructureSnapshot rows
were queried.

Fix: add an autouse fixture that swaps the engine for a fresh
sqlite:///:memory: instance for every test in this file. The temp_db
fixture is preserved for tests that need a real file path.

Tests: 620 passed, 1 skipped, 1 deselected (was 619 passed + 2 failed).
This commit is contained in:
opencode 2026-07-06 20:11:07 +07:00
parent 3804c1cd47
commit 025bcbbe9e

View file

@ -16,6 +16,42 @@ from structure_monitor import (
)
@pytest.fixture(autouse=True)
def _fresh_db(monkeypatch):
"""Force a brand-new in-memory SQLite engine for every test.
`structure_monitor` reads from the module-level `db._engine`. Without
this fixture a previous test that committed StructureSnapshot rows
(e.g. via `temp_db` pointing at a tmp_path sqlite file) leaks state
into the next test that doesn't use the `temp_db` fixture
(`test_check_selectors_empty`). Yielding a private :memory: engine
per-test guarantees isolation regardless of import order.
Also resets the shared `client.http_client` so an httpx.AsyncClient
bound to a closed event loop (from a prior test) doesn't surface as
RuntimeError on the next test that hits the network.
"""
import db as db_module
import client as client_module
from sqlalchemy import create_engine
monkeypatch.setenv("PRY_DATABASE_URL", "sqlite:///:memory:")
monkeypatch.setenv("PRY_DATA_DIR", "/tmp")
db_module._engine = create_engine(
"sqlite:///:memory:", connect_args={"check_same_thread": False}, future=True
)
db_module._SessionLocal = None
# Recreate tables on the new engine.
from db import Base
Base.metadata.create_all(db_module._engine)
client_module.http_client = None
yield
db_module._engine = None
db_module._SessionLocal = None
client_module.http_client = None
@pytest.fixture
def temp_db(monkeypatch, tmp_path):
import db as db_module