refactor(tests): one definition each for the copied fixtures and fakes (#2825, milestone 296 area 1)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 18s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 18s
The shape ledger showed the same test scaffolding defined over and over: _bind_user x12 (byte-identical), _dispose_engine x10 in three wordings, _no_supersession x3, _make_mock_session x7 in three subsets, a get-or-create User helper x2 (+3 inlined), and fifteen hand-rolled MagicMock note factories each re-explaining the same "an auto-MagicMock attribute is truthy" hazard (note 2109). Now: conftest.py carries _bind_user / _dispose_engine / _no_supersession as opt-in fixtures (pytestmark = usefixtures(...) per module, so unit tests pay nothing), and tests/helpers.py carries make_mock_session(), ensure_user() and fake_note(**attrs) — the hazard documented once, real values on every attribute the product reads. Call sites were rewritten by AST so titles with dashes and commas survived; the three SimpleNamespace _note stand-ins that only feed a single function stay local. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,7 @@ import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import select
|
||||
|
||||
from scribe.models import async_session, engine
|
||||
from scribe.models import async_session
|
||||
from scribe.models.code_shape import CodeShape
|
||||
from scribe.models.project import Project
|
||||
from scribe.models.user import User
|
||||
@@ -19,8 +19,9 @@ from scribe.services.shape_ledger import (
|
||||
snippet_consumers,
|
||||
sync_repo_shapes,
|
||||
)
|
||||
from tests.helpers import ensure_user
|
||||
|
||||
pytestmark = pytest.mark.integration
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
|
||||
|
||||
REPO = "git.example.com/alice/widget"
|
||||
SHAPES = [
|
||||
@@ -31,39 +32,14 @@ SHAPES = [
|
||||
]
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(autouse=True)
|
||||
async def _dispose_engine():
|
||||
"""Dispose the app's module-level engine after each test.
|
||||
|
||||
The engine pools asyncpg connections per event loop, but pytest-asyncio runs
|
||||
each test on a fresh loop — so without this, test 2 gets handed test 1's
|
||||
connection bound to a now-dead loop ("Future attached to a different loop").
|
||||
Disposing in the test's own loop teardown clears the pool cleanly.
|
||||
"""
|
||||
yield
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
async def _user(session, username: str) -> User:
|
||||
existing = (
|
||||
await session.execute(select(User).where(User.username == username))
|
||||
).scalar_one_or_none()
|
||||
if existing is not None:
|
||||
return existing
|
||||
user = User(username=username)
|
||||
session.add(user)
|
||||
await session.flush()
|
||||
return user
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def seeded():
|
||||
"""Owner + outsider, a project with a synced 4-shape ledger, one snippet."""
|
||||
from scribe.services import snippets as snippets_svc
|
||||
|
||||
async with async_session() as s:
|
||||
owner = await _user(s, "classify_owner")
|
||||
other = await _user(s, "classify_other")
|
||||
owner = await ensure_user(s, "classify_owner")
|
||||
other = await ensure_user(s, "classify_other")
|
||||
project = Project(user_id=owner.id, title="Classify target")
|
||||
s.add(project)
|
||||
await s.flush()
|
||||
|
||||
Reference in New Issue
Block a user