Files
auto-stock/backend/tests/test_swing_r7_balance_page_20261002.py
shuim d140055e64 fix: 하드닝 루프 9회차 — 매수 원복·잔고 연속조회·결과 불명 매도·브레이커·경보 보강
- 8회차 HIGH 수정 16건 레드팀 재검증: 12 확인·3 부분·1 미수정 → 전부 보강
- trade_executor/fill_sweep: 늦게 확정된 0체결 BUY → CANCELLED + ActiveTrade close, 부분체결 → 수량 축소(멱등, 재기동 후 TH 재구성)
- korea_investment_api: get_balance_all_v2(tr_cont 연속조회, _partial), get_todays_orders_v2(실패 None), 브레이커 청산용 조회 면제(OPEN 중 미집계)
- swing/exit_service·mdd_manager: 매도 직전 상한·MDD 축소가 전체 잔고 페이지 사용(§8.5-37), 불명 대기 중 부분익절 skip·쿨다운·pre_hldg 차감, 대기 TTL 15:30+재매수 폐기(대조 1회 선행)(§8.5-38 해소)
- swing/scheduler: 청산 TIMED_OUT/SKIPPED/in-flight 시 MDD 축소·진입 보류(예외는 진입만), 3사이클 연속 시 error+텔레그램
- daytrading: 결과 불명 매도 재판정 v3(주문조회 v2 None→보류, 등록 이후 일치 행→accepted, 원장≤0→보류), 재기동 복원 min(주문가능,보유), 잔고 조회 후 일일손실한도 재확인, 단독 실행 Redis 접속 제거
- main: 스트림 무수신 경보 v4(희망 구독 기준), 첫 하트비트 없음 stale(HB_FIRST_BEAT_GRACE_SEC)
- 운영: Jenkinsfile health_watch.py 디렉터리 잔존 복구, prod_smoke 사이드카 확인, config 키 12개 선언(동작 무변경)
- 진입가 v3(max(AT,PA))는 KIS 이동평균 평단 전제 오류로 레드팀 반증 → 폐기
- CLAUDE.md §3.2 ③-d, §8.5-37/38 갱신, §8.5-39~48 신규(Crisis 손절 소급 §8.5-39 는 사용자 결정 대기)
- 유닛 스위트 2781 passed / 0 failed (소켓 차단), 네트워크 시도 0건

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-12 15:37:16 +09:00

51 lines
2.0 KiB
Python

"""7회차 H-A: get_balance 첫 페이지만 조회 — 페이지가 가득 찼는데 종목이 없으면 '보유 0'이 아니라 조회 실패(None)."""
import pytest
from app.core.config import settings
from app.services.swing import exit_service as es
def _rows(n, code_prefix="9"):
return [{"pdno": f"{code_prefix}{i:05d}", "hldg_qty": "10", "ord_psbl_qty": "10"} for i in range(n)]
@pytest.mark.asyncio
async def test_full_page_without_stock_is_unknown(monkeypatch):
rows = _rows(int(settings.SWING_BALANCE_FULL_PAGE_ROWS))
async def _bal():
return {"rt_cd": "0", "output1": rows}
monkeypatch.setattr(es.korea_investment_api, "get_balance", _bal)
monkeypatch.setattr(es.korea_investment_api, "get_balance_all_v2", _bal) # [R9] 연속조회 목 보정 — 같은 목을 공유
assert await es._fetch_kis_holding_v2("005930") is None
@pytest.mark.asyncio
async def test_short_page_without_stock_is_zero(monkeypatch):
rows = _rows(int(settings.SWING_BALANCE_FULL_PAGE_ROWS) - 1)
async def _bal():
return {"rt_cd": "0", "output1": rows}
monkeypatch.setattr(es.korea_investment_api, "get_balance", _bal)
monkeypatch.setattr(es.korea_investment_api, "get_balance_all_v2", _bal) # [R9] 연속조회 목 보정 — 같은 목을 공유
assert await es._fetch_kis_holding_v2("005930") == (0, 0)
@pytest.mark.asyncio
async def test_full_page_with_stock_is_found(monkeypatch):
rows = _rows(int(settings.SWING_BALANCE_FULL_PAGE_ROWS) - 1) + [
{"pdno": "005930", "hldg_qty": "7", "ord_psbl_qty": "5"}
]
async def _bal():
return {"rt_cd": "0", "output1": rows}
monkeypatch.setattr(es.korea_investment_api, "get_balance", _bal)
monkeypatch.setattr(es.korea_investment_api, "get_balance_all_v2", _bal) # [R9] 연속조회 목 보정 — 같은 목을 공유
assert await es._fetch_kis_holding_v2("005930") == (7, 5)
from _swing_redis_guard import _swing_redis_guard # noqa: E402,F401 (R9: 단독 실행 시 Redis 실접속 방지)