"""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 실접속 방지)