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:
@@ -5,13 +5,7 @@ Mocks async_session — no real DB, matching the other notes-service tests.
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _make_mock_session():
|
||||
s = AsyncMock()
|
||||
s.__aenter__ = AsyncMock(return_value=s)
|
||||
s.__aexit__ = AsyncMock(return_value=False)
|
||||
return s
|
||||
from tests.helpers import fake_note, make_mock_session
|
||||
|
||||
|
||||
def _result(first=None, all_=None):
|
||||
@@ -22,17 +16,10 @@ def _result(first=None, all_=None):
|
||||
return r
|
||||
|
||||
|
||||
def _note(id, title):
|
||||
n = MagicMock()
|
||||
n.id = id
|
||||
n.title = title
|
||||
return n
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_process_by_numeric_id():
|
||||
note = _note(5, "Drift Audit")
|
||||
session = _make_mock_session()
|
||||
note = fake_note(id=5, title="Drift Audit")
|
||||
session = make_mock_session()
|
||||
# numeric id → first execute (id lookup) hits
|
||||
session.execute = AsyncMock(side_effect=[_result(first=note)])
|
||||
with patch("scribe.services.notes.async_session") as cls:
|
||||
@@ -46,8 +33,8 @@ async def test_resolve_process_by_numeric_id():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_process_exact_title_beats_substring():
|
||||
note = _note(7, "Drift Audit")
|
||||
session = _make_mock_session()
|
||||
note = fake_note(id=7, title="Drift Audit")
|
||||
session = make_mock_session()
|
||||
# non-digit → exact-title query (first execute) hits; substring never runs
|
||||
session.execute = AsyncMock(side_effect=[_result(first=note)])
|
||||
with patch("scribe.services.notes.async_session") as cls:
|
||||
@@ -61,9 +48,9 @@ async def test_resolve_process_exact_title_beats_substring():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_process_substring_returns_candidates():
|
||||
n1 = _note(7, "Drift Audit Remediation")
|
||||
n2 = _note(9, "Drift Audit Notes")
|
||||
session = _make_mock_session()
|
||||
n1 = fake_note(id=7, title="Drift Audit Remediation")
|
||||
n2 = fake_note(id=9, title="Drift Audit Notes")
|
||||
session = make_mock_session()
|
||||
# exact miss, then substring returns two (most-recent first)
|
||||
session.execute = AsyncMock(side_effect=[_result(first=None), _result(all_=[n1, n2])])
|
||||
with patch("scribe.services.notes.async_session") as cls:
|
||||
@@ -77,7 +64,7 @@ async def test_resolve_process_substring_returns_candidates():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_process_no_match():
|
||||
session = _make_mock_session()
|
||||
session = make_mock_session()
|
||||
session.execute = AsyncMock(side_effect=[_result(first=None), _result(all_=[])])
|
||||
with patch("scribe.services.notes.async_session") as cls:
|
||||
cls.return_value = session
|
||||
|
||||
Reference in New Issue
Block a user