o2o-negosium-original/schedules/anchoring/tests/test_batch.py
민헌 a132dac57a feat(anchoring): 조회용 뷰 2종 신설 — records 테이블은 검토 후 기각 (TODO 2)
요구(회사별 앵커링 값 업데이트 리스트업 + 이전 값 판별)는 rate_adjustments
한 행에 anchor_rate_before→after 가 박제되어 이미 충족 — 신규 테이블은 동일
정보의 사본만 만들므로 기각하고, 조회를 제품화하는 파생 뷰로 해결:

- anchoring.rate_history: 값 변경 이력 리스트업(이전→새 값, delta_permille,
  success_rate, created_at)
- anchoring.current_rates: 칸별 현재값(최신 조정 행 — 없는 칸 = 시작값 10‰)

뷰는 상태가 없어 오염·재구축 이슈 자체가 없고 append-only 보호 대상 아님.
통합 테스트에 뷰 검증 추가(이력 before/after·성공률, 현재값). 운영 문서 §8
쿼리를 뷰 기반으로 단순화, TODO 과제 2 종결(대시보드 페이징 요구 시 스냅샷
테이블 승격 재검토 명시).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 20:22:25 +09:00

175 lines
8.3 KiB
Python

"""배치 통합 테스트 (스펙 §11.5 — 실제 Postgres, Redis 없음(무Redis 폴백 경로)).
실행: cd schedules/anchoring && PYTHONPATH=src .venv/bin/python -m pytest tests/test_batch.py -q
"""
import logging
import uuid
from sqlalchemy import select, text
from anchoring import db as adb
from anchoring.batch import MarkingConflictError, _evaluate_cell, run_evaluation_batch
from anchoring.models import RateAdjustment, Session
from anchoring.reader import get_anchor_rate
from conftest import requires_db
pytestmark = requires_db
# 시드 기본값: target 30,000 / rate 10‰ / anchor 29,700 → bracket 12 ("3만 원대" 칸)
BRACKET = 12
SUCCESS_BID = 29_000 # ≤ anchor → BID_SUCCESS
FAIL_BID = 29_999 # > anchor → BID_FAIL
async def _adjustments(db, seeder):
stmt = (
select(RateAdjustment)
.where(RateAdjustment.company_id == seeder.company_id)
.order_by(RateAdjustment.id)
)
return (await db.execute(stmt)).scalars().all()
async def _marks(db, session_ids):
stmt = select(Session.session_id, Session.anchoring_adjustment_id).where(Session.session_id.in_(session_ids))
return dict((await db.execute(stmt)).all())
async def _seed_mixed(db, seeder, success: int, fail: int, **kw):
ids = []
for _ in range(success):
ids.append(await seeder.seed_session(db, bid_price=SUCCESS_BID, **kw))
for _ in range(fail):
ids.append(await seeder.seed_session(db, bid_price=FAIL_BID, **kw))
return ids
# ── §11.5: 13건 전량 평가 + 멱등 (실패 3종 혼합) + 로그 규약 ──
async def test_full_cycle_and_idempotency(seeder, caplog):
async with adb.session_scope() as db:
ids = await _seed_mixed(db, seeder, success=8, fail=2) # DONE 인데 앵커 초과(와일드카드 상단 등)
for _ in range(2): # 가격 쓰고 결렬(REJECTED) = 실패
ids.append(await seeder.seed_session(db, status=5, bid_price=None, last_offered_price=FAIL_BID))
# 가격 쓰고 이탈 → 견적 마감 시 일괄 NOT_PARTICIPATED = 실패 (중간 이탈 시나리오)
ids.append(await seeder.seed_session(db, status=4, bid_price=None, last_offered_price=FAIL_BID))
# 합계 13건, 성공 8 → r≈0.615 → +20
with caplog.at_level(logging.INFO, logger="anchoring"):
result = await run_evaluation_batch(force=True)
# 로그 규약: run_id 태그 + 회사별 grep 가능한 칸별 조정 라인 + 회사요약 라인
assert result["run_id"]
tagged = [m for m in caplog.messages if f"[batch {result['run_id']}]" in m]
assert any(f"조정 company={seeder.company_id}" in m and "10‰→30‰" in m for m in tagged)
assert any(f"회사요약 company={seeder.company_id}" in m and "평가=1" in m for m in tagged)
async with adb.session_scope() as db:
adjustments = await _adjustments(db, seeder)
assert len(adjustments) == 1
adj = adjustments[0]
assert (adj.nego_count, adj.success_count) == (13, 8)
assert (adj.anchor_rate_before, adj.anchor_rate_after) == (10, 30)
assert sorted(adj.consumed_session_ids) == sorted(str(i) for i in ids)
marks = await _marks(db, ids)
assert all(v == adj.id for v in marks.values()) # 13건 모두 소비 마킹
# 재실행 — 마킹 멱등: 우리 칸 조정은 그대로 1건
await run_evaluation_batch(force=True)
async with adb.session_scope() as db:
assert len(await _adjustments(db, seeder)) == 1
# 조회용 뷰 — rate_history(이전→새 값 리스트업) / current_rates(칸별 현재값)
hist = (await db.execute(text(
"SELECT anchor_rate_before, anchor_rate_after, delta_permille, success_rate "
"FROM anchoring.rate_history WHERE company_id = :c"), {"c": seeder.company_id})).one()
assert (hist.anchor_rate_before, hist.anchor_rate_after, hist.delta_permille) == (10, 30, 20)
assert float(hist.success_rate) == 0.615
cur = (await db.execute(text(
"SELECT anchor_rate_permille FROM anchoring.current_rates "
"WHERE company_id = :c AND supplier_type = 1 AND price_bracket_index = :b"),
{"c": seeder.company_id, "b": BRACKET})).scalar_one()
assert cur == 30
# ── §11.5: 이월(7건 스킵 → 누적 13건 단일 평가) ──────────
async def test_carryover(seeder):
async with adb.session_scope() as db:
first = await _seed_mixed(db, seeder, success=5, fail=2) # 7건 < 10
await run_evaluation_batch(force=True)
async with adb.session_scope() as db:
assert await _adjustments(db, seeder) == []
marks = await _marks(db, first)
assert all(v is None for v in marks.values()) # 마킹 없음 = 이월
second = await _seed_mixed(db, seeder, success=3, fail=3) # 누적 13건 (8S/5F)
await run_evaluation_batch(force=True)
async with adb.session_scope() as db:
adjustments = await _adjustments(db, seeder)
assert len(adjustments) == 1
assert adjustments[0].nego_count == 13 # 4주치 전량 1회 평가
assert adjustments[0].anchor_rate_after == 30
marks = await _marks(db, first + second)
assert all(v == adjustments[0].id for v in marks.values())
# ── §11.5: 회사 격리 + 현재값 조회(무Redis DB 폴백) + δ 유형 차원 ──
async def test_company_isolation_and_reader(seeder):
async with adb.session_scope() as db:
await _seed_mixed(db, seeder, success=10, fail=0, supplier_type=1) # 유통 → +20
await _seed_mixed(db, seeder, success=10, fail=0, supplier_type=2) # 제조 → +10 (δ 스왑 가드)
await run_evaluation_batch(force=True)
other_company = uuid.uuid4()
async with adb.session_scope() as db:
adjustments = await _adjustments(db, seeder)
by_type = {a.supplier_type: a.anchor_rate_after for a in adjustments}
assert by_type == {1: 30, 2: 20}
# 조정된 칸은 새 rate, 타사 같은 (유형,구간) 칸은 정적 테이블 시작값
assert await get_anchor_rate(db, seeder.company_id, 1, BRACKET) == 30
assert await get_anchor_rate(db, other_company, 1, BRACKET) == 10
# ── §11.5: EXCLUDED 마킹 0 + 유효 n<10 이월 + supplier_type 미지정 ──
async def test_excluded_and_unsampleable(seeder):
async with adb.session_scope() as db:
# 가격 흔적 없는 종료(무가격 결렬·미참여) → EXCLUDED
excluded = [await seeder.seed_session(db, status=5, last_offered_price=None) for _ in range(8)]
excluded += [await seeder.seed_session(db, status=4, last_offered_price=None) for _ in range(7)]
valid = await _seed_mixed(db, seeder, success=5, fail=0) # 유효 5 < 10
untyped = [await seeder.seed_session(db, supplier_type=0, bid_price=SUCCESS_BID)] # 칸 구성 불가
await run_evaluation_batch(force=True)
async with adb.session_scope() as db:
assert await _adjustments(db, seeder) == [] # 유효 5 < 10 → 평가 없음
marks = await _marks(db, excluded + untyped)
assert all(v == 0 for v in marks.values()) # 제외 확정 마킹(재스캔 방지)
marks = await _marks(db, valid)
assert all(v is None for v in marks.values()) # 유효 표본은 이월
# ── 개정 1: 마킹 rowcount ≠ n → 조정 INSERT 포함 전체 롤백 ──
async def test_marking_conflict_rolls_back(seeder):
async with adb.session_scope() as db:
ids = await _seed_mixed(db, seeder, success=10, fail=0)
# 경합 시뮬레이션: 1건을 다른 실행이 먼저 소비한 상태로 만든다
await db.execute(text(
"UPDATE negotiation.sessions SET anchoring_adjustment_id = 999999 WHERE session_id = :sid"
), {"sid": ids[0]})
samples = [(sid, 1) for sid in ids] # 10건 전부 BID_SUCCESS 로 평가 시도
try:
await _evaluate_cell(seeder.company_id, 1, BRACKET, samples)
raised = False
except MarkingConflictError:
raised = True
assert raised
async with adb.session_scope() as db:
assert await _adjustments(db, seeder) == [] # 롤백 — 이중 조정 없음
marks = await _marks(db, ids[1:])
assert all(v is None for v in marks.values()) # 나머지 9건 마킹도 롤백