## 배경 "전략이 혼란스럽고 현재와 맞지 않는다"를 조사한 결과, 전략 설계가 아니라 전략을 검증했다는 도구와 파라미터 소스가 어긋나 있었다. ## 파라미터 소스 단일화 - 우선순위가 adaptive > strategy_cfg(DB) > config.py 인데 문서는 맨 마지막인 config.py 를 기술하고 있었다. 11곳이 불일치(BULL RSI 46 vs DB 30, SL -7% vs -6%). - DEFAULT_PARAMS 를 config.py 에 정합 + alembic 0012 로 기존 DB 행 갱신 (운영자가 UI 로 바꾼 값은 보존, 옛 기본값과 같은 행만 갱신, 테이블 가드·멱등) ## 백테스트를 실전 로직에 정합 - calculate_adx_simple 이 정확히 period 배(14.0×) 발산 → Aggressive 게이트 `ADX>25` 가 전 레짐 100% 통과. 전환 빈도 5.6%→2.0%(-65%), 성과 상방 편향 - 백테스트 RSI 는 SMA, 실전은 Wilder → 진입 판정 10~11% 불일치 - us_backtest_engine RSI 1봉 지연 제거 (룩어헤드 없음을 절단 테스트로 증명) - backend/backtest_results/ 25건은 폐기 대상 (§8.4) ## 리스크 가드 복원 - 4% 절대 상한이 try 블록 안에 있어 예외 시 적응형 6~7% 가 통과 → try 밖으로 - 청산을 매수보다 먼저 평가 (보유 종목이 추천에 들면 손절 대신 물타기가 나갔다) - 리스크 가드 4개를 매수 전용화 → MDD -10%·보유한도 도달 시에도 손절 작동 - MDD 갱신 5회 연속 실패 시 신규 진입 차단 fail-safe (청산은 계속 허용) - 스윙 청산이 DT 보유분까지 매도하던 문제, sell_pct 기준 혼재 정정 ## 죽은 코드 제거 (2,695줄) - short_term_strategy, kosdaq_scanner, app/strategy/ 패키지 - app/trading/ + korea_investment_api_adapter (real_market_api 정본은 korea_investment_api.py:1482, adapter 는 import 0건) ## 지표·사이징 단일화 (수치 무변경 증명) - indicators_core.py: 3,274건 비교 최대 오차 0.000e+00 - sizing_core.py: 42,105건 비교 불일치 0 - 미국 전략 상수 13개 config 편입 (hex 까지 동일, 3,168 케이스 전수 비교) ## 테스트 안전 (실 KIS 호출 차단) - 유닛 테스트가 실제 KIS 에 매도 주문을 2회 전송하고 있었다. 리팩터링이 close_stale_positions 의 대상을 _execute_exit → _execute_exit_stale 로 바꿨는데 목이 전자에 걸려 빗나갔다. test_api_endpoints 도 캡처된 싱글톤 참조 누수로 실제 잔고 조회를 타 141초를 소비했다. - backend/tests/conftest.py 신설: 주문·토큰·시세·잔고를 호출 즉시 예외로 차단. 예외는 BaseException 파생(프로덕션 except Exception 폴백에 삼켜지지 않게), 클래스 메서드에 적용(캡처된 참조까지 덮음), DATABASE_URL `_test` 가드 강제 - 전체 유닛 스위트: 365초/6 failed → 11.8초/0 failed (소켓 차단 하 559 passed) ## 신규 (둘 다 플래그 OFF, 백테스트 미검증) - range_trading_strategy.py — SIDEWAYS 담당 전략이 없던 빈 칸. 미배선 - check_bear_buy_signal_v2 — BEAR 4중 AND(발화율 2.3%) → 점수제(12.4%) ## 문서 - CLAUDE.md 를 실제 코드 기준으로 재작성. §0 파라미터 우선순위, §4.1 테스트 규약, §6.4 문서화되지 않았던 진입 차단 게이트 9종, §8.4 백테스트 폐기 근거, §8.5 미해결 19항목 - 루트 md 6개 → 2개(docs/ 이동), 중복 배치 문서 병합, 스테일 worktree 제거 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
795 lines
30 KiB
Python
795 lines
30 KiB
Python
"""
|
|
FastAPI API 엔드포인트 통합 테스트
|
|
|
|
- 모니터링 (헬스체크, API 통계)
|
|
- 포트폴리오 (보유 종목 조회, 요약)
|
|
- 거래 이력 (목록 조회, 개별 조회, 통계)
|
|
|
|
httpx.AsyncClient + ASGITransport 사용 (비동기 테스트)
|
|
DB 세션 및 외부 서비스(KIS API, Redis 등)는 Mock 처리
|
|
"""
|
|
import sys
|
|
import os
|
|
import tempfile
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
from datetime import datetime, timedelta
|
|
from decimal import Decimal
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
from contextlib import asynccontextmanager
|
|
|
|
import httpx
|
|
from httpx import ASGITransport
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 외부 서비스 Mock 패치 (app 임포트 전에 적용해야 함)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# KIS API mock
|
|
_mock_kis = MagicMock()
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 100,
|
|
"total_errors": 2,
|
|
"total_retries": 3,
|
|
"error_rate": 0.02,
|
|
"avg_duration": 0.15,
|
|
"recent_calls": 50,
|
|
}
|
|
_mock_kis.log_monitor_stats = MagicMock()
|
|
_mock_kis.get_balance = AsyncMock(return_value={})
|
|
_mock_kis.get_overseas_balance = AsyncMock(return_value={})
|
|
|
|
# 모듈 레벨에서 korea_investment_api 대체
|
|
sys.modules.setdefault("app.services.korea_investment_api", MagicMock())
|
|
patch("app.services.korea_investment_api.korea_investment_api", _mock_kis).start()
|
|
|
|
# Redis mock (선택적 — import 실패 방지)
|
|
_redis_mock_module = MagicMock()
|
|
_redis_mock_module.setex_json = AsyncMock(return_value=True)
|
|
_redis_mock_module.get_json = AsyncMock(return_value=None)
|
|
sys.modules.setdefault("app.core.redis", _redis_mock_module)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 테스트용 DB 세션 (파일 기반 SQLite — 연결 간 테이블 공유 보장)
|
|
# ---------------------------------------------------------------------------
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
|
|
from sqlalchemy import JSON, String, Text
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# SQLite 호환: PostgreSQL 전용 타입(JSONB, ARRAY)을 SQLite에서 사용 가능하게 매핑
|
|
# ---------------------------------------------------------------------------
|
|
from sqlalchemy.ext.compiler import compiles
|
|
from sqlalchemy.dialects.sqlite.base import SQLiteTypeCompiler
|
|
|
|
@compiles(JSONB, "sqlite")
|
|
def _compile_jsonb_sqlite(element, compiler, **kw):
|
|
"""SQLite에서 JSONB를 JSON으로 매핑"""
|
|
return "JSON"
|
|
|
|
|
|
# SQLite type compiler에 ARRAY 렌더링 메서드 직접 등록
|
|
def _visit_array_sqlite(self, type_, **kw):
|
|
"""SQLite에서 ARRAY를 TEXT로 매핑"""
|
|
return "TEXT"
|
|
|
|
SQLiteTypeCompiler.visit_ARRAY = _visit_array_sqlite
|
|
|
|
|
|
# 테스트 전용 임시 파일 DB (연결 간 테이블 공유 보장)
|
|
_tmp_db_file = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
|
|
_tmp_db_path = _tmp_db_file.name
|
|
_tmp_db_file.close()
|
|
|
|
TEST_DATABASE_URL = f"sqlite+aiosqlite:///{_tmp_db_path}"
|
|
|
|
test_engine = create_async_engine(TEST_DATABASE_URL, echo=False)
|
|
TestSessionLocal = sessionmaker(
|
|
test_engine, class_=AsyncSession, expire_on_commit=False
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# lifespan을 비활성화한 앱 생성 (스케줄러, DB 초기화 등 건너뛰기)
|
|
# ---------------------------------------------------------------------------
|
|
@asynccontextmanager
|
|
async def _noop_lifespan(app):
|
|
"""테스트용 lifespan — 스케줄러/외부 연결 없이 즉시 yield"""
|
|
yield
|
|
|
|
|
|
# app 임포트 전에 lifespan을 패치할 수 없으므로 임포트 후 교체
|
|
from app.core.database import Base, get_db
|
|
from app.main import app
|
|
|
|
# 원본 lifespan 대신 noop lifespan 사용
|
|
app.router.lifespan_context = _noop_lifespan
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# DB 의존성 오버라이드
|
|
# ---------------------------------------------------------------------------
|
|
async def override_get_db():
|
|
"""테스트용 DB 세션 생성 (SQLite 파일 기반)"""
|
|
async with TestSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 모델 임포트
|
|
# ---------------------------------------------------------------------------
|
|
from app.models.models import Portfolio, TradeHistory
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Fixtures
|
|
# ---------------------------------------------------------------------------
|
|
@pytest.fixture(autouse=True)
|
|
def bind_mock_kis_to_captured_refs():
|
|
"""`_mock_kis`를 이미 실제 싱글톤을 캡처한 모듈들에까지 강제 바인딩.
|
|
|
|
파일 상단의 `patch("app.services.korea_investment_api.korea_investment_api", _mock_kis)`는
|
|
**이 모듈이 가장 먼저 import될 때만** 통한다. 전체 스위트 실행에서는 알파벳 순으로 앞선
|
|
테스트 모듈(`test_adaptive_strategy` 등)이 이미 `app.main`을 import해
|
|
`app/api/endpoints/portfolio.py:20` 의 `from app.services.korea_investment_api import
|
|
korea_investment_api` 가 **실제 싱글톤을 자기 모듈 네임스페이스에 캡처**해 둔 상태다.
|
|
그래서 소스 모듈 속성만 갈아끼운 상단 patch가 새고, `/api/v1/portfolio/summary` 가
|
|
라이브 `get_balance()` → `_get_access_token()` → `_handle_rate_limit()` 의 70초
|
|
`asyncio.sleep` 까지 내려갔다.
|
|
(2026-09-27 측정: 단독 실행 2.9초 / 전체 스위트에서 이 파일 2건이 71초·142초 + 모니터링 5건 실패)
|
|
|
|
`tests/conftest.py` 의 autouse 차단 픽스처가 클래스 메서드를 "호출 시 실패"로 덮으므로,
|
|
복구도 **픽스처 단계**에서 해야 한다(부모 conftest autouse → 모듈 autouse 순서이므로
|
|
이쪽이 나중에 적용되어 이긴다).
|
|
"""
|
|
from app.services.korea_investment_api import KoreaInvestmentAPI
|
|
|
|
patches = [
|
|
patch.object(mod, "korea_investment_api", _mock_kis)
|
|
for name, mod in list(sys.modules.items())
|
|
if name.startswith("app.")
|
|
and isinstance(getattr(mod, "korea_investment_api", None), KoreaInvestmentAPI)
|
|
]
|
|
for p in patches:
|
|
p.start()
|
|
try:
|
|
yield
|
|
finally:
|
|
for p in reversed(patches):
|
|
p.stop()
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
async def setup_database():
|
|
"""각 테스트 전에 테이블 생성, 테스트 후 모든 데이터 정리"""
|
|
async with test_engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield
|
|
# 테이블 드롭 후 재생성 (테스트 간 데이터 격리)
|
|
async with test_engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def db_session():
|
|
"""테스트에서 직접 DB에 데이터를 삽입할 때 사용하는 세션"""
|
|
async with TestSessionLocal() as session:
|
|
yield session
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client():
|
|
"""httpx AsyncClient (ASGITransport 기반) — 실제 X-API-Key 헤더로 인증.
|
|
|
|
[2026-09-27 수정] 선재 결함: 이 픽스처는 `require_api_key` 의존성만
|
|
noop("test-key" 반환)으로 오버라이드했다. 그런데 portfolio/trades 라우터에는
|
|
멀티테넌트 레이어(`app/api/__init__.py` → `Depends(deny_non_owner_session_when_mt)`)가
|
|
추가돼 있고, 그 체인은 `get_current_user`(app/core/tenant_auth.py)에서
|
|
require_api_key 의 **반환값**을 API 키 또는 세션 토큰으로 다시 해석한다.
|
|
"test-key" 는 둘 다 아니므로 사용자 해석 실패 → 401 (20건 실패의 단일 원인).
|
|
|
|
→ 인증을 무력화하지 않고, 실제 운영과 동일하게 X-API-Key 헤더에 유효한 키를
|
|
실어 보낸다(`_get_api_key()` = API_KEY 우선, 없으면 SECRET_KEY 폴백).
|
|
이렇게 하면 require_api_key → get_current_user(API 키 호출자 = legacy owner)
|
|
→ deny_non_owner_session_when_mt(MULTI_TENANT_ENABLED=False 이므로 통과)
|
|
전체 체인이 실제로 실행된다. 의존성 오버라이드는 get_db 뿐.
|
|
"""
|
|
from app.core.security import _get_api_key
|
|
|
|
transport = ASGITransport(app=app)
|
|
async with httpx.AsyncClient(
|
|
transport=transport,
|
|
base_url="http://test",
|
|
headers={"X-API-Key": _get_api_key()},
|
|
) as c:
|
|
yield c
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def unauthenticated_client():
|
|
"""인증 헤더가 없는 클라이언트 — require_api_key 가 실제로 걸려 있는지 검증용."""
|
|
transport = ASGITransport(app=app)
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:
|
|
yield c
|
|
|
|
|
|
def _reset_portfolio_cache(**overrides):
|
|
"""포트폴리오 캐시를 테스트에 맞게 초기화하는 헬퍼"""
|
|
from app.api.endpoints.portfolio import portfolio_cache
|
|
defaults = {
|
|
"holdings": [],
|
|
"summary": None,
|
|
"last_synced": datetime.now(),
|
|
"change_flag": False,
|
|
"last_error": None,
|
|
"failure_count": 0,
|
|
"next_retry_at": None,
|
|
"last_sync_success": True,
|
|
"last_trade_id": None,
|
|
}
|
|
defaults.update(overrides)
|
|
for k, v in defaults.items():
|
|
setattr(portfolio_cache, k, v)
|
|
return portfolio_cache
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def sample_portfolio(db_session: AsyncSession):
|
|
"""테스트용 포트폴리오 샘플 데이터 (국내 2종목)"""
|
|
items = [
|
|
Portfolio(
|
|
id=1,
|
|
stock_code="005930",
|
|
stock_name="삼성전자",
|
|
quantity=10,
|
|
average_price=Decimal("70000.00"),
|
|
current_price=Decimal("72000.00"),
|
|
profit_loss=Decimal("20000.00"),
|
|
profit_loss_rate=Decimal("2.8571"),
|
|
market="KRX",
|
|
),
|
|
Portfolio(
|
|
id=2,
|
|
stock_code="000660",
|
|
stock_name="SK하이닉스",
|
|
quantity=5,
|
|
average_price=Decimal("120000.00"),
|
|
current_price=Decimal("115000.00"),
|
|
profit_loss=Decimal("-25000.00"),
|
|
profit_loss_rate=Decimal("-4.1667"),
|
|
market="KRX",
|
|
),
|
|
]
|
|
for item in items:
|
|
db_session.add(item)
|
|
await db_session.commit()
|
|
return items
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def sample_trades(db_session: AsyncSession):
|
|
"""테스트용 거래 이력 샘플 데이터"""
|
|
now = datetime.utcnow()
|
|
trades = [
|
|
TradeHistory(
|
|
id=1,
|
|
stock_code="005930",
|
|
stock_name="삼성전자",
|
|
order_type="BUY",
|
|
order_price=Decimal("70000.00"),
|
|
order_quantity=10,
|
|
executed_price=Decimal("70000.00"),
|
|
executed_quantity=10,
|
|
total_amount=Decimal("700000.00"),
|
|
status="EXECUTED",
|
|
order_id="ORD001",
|
|
message="매수 체결",
|
|
entry_reason="RSI 과매도",
|
|
created_at=now - timedelta(days=5),
|
|
executed_at=now - timedelta(days=5),
|
|
),
|
|
TradeHistory(
|
|
id=2,
|
|
stock_code="005930",
|
|
stock_name="삼성전자",
|
|
order_type="SELL",
|
|
order_price=Decimal("72000.00"),
|
|
order_quantity=5,
|
|
executed_price=Decimal("72000.00"),
|
|
executed_quantity=5,
|
|
total_amount=Decimal("360000.00"),
|
|
status="EXECUTED",
|
|
order_id="ORD002",
|
|
message="매도 체결, 손익률 2.86%",
|
|
exit_reason="RSI 과매수 도달, 손익: +10,000원",
|
|
net_profit_loss=Decimal("10000.00"),
|
|
profit_loss_rate=Decimal("2.8571"),
|
|
created_at=now - timedelta(days=2),
|
|
executed_at=now - timedelta(days=2),
|
|
),
|
|
TradeHistory(
|
|
id=3,
|
|
stock_code="000660",
|
|
stock_name="SK하이닉스",
|
|
order_type="BUY",
|
|
order_price=Decimal("120000.00"),
|
|
order_quantity=5,
|
|
executed_price=None,
|
|
executed_quantity=None,
|
|
total_amount=None,
|
|
status="FAILED",
|
|
order_id="ORD003",
|
|
message="주문 실패: 잔고 부족",
|
|
created_at=now - timedelta(days=1),
|
|
),
|
|
]
|
|
for t in trades:
|
|
db_session.add(t)
|
|
await db_session.commit()
|
|
return trades
|
|
|
|
|
|
# ===========================================================================
|
|
# 1. 모니터링 / 헬스체크 엔드포인트 테스트
|
|
# ===========================================================================
|
|
class TestMonitoringEndpoints:
|
|
"""모니터링 API 엔드포인트 테스트"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_health_check_healthy(self, client: httpx.AsyncClient):
|
|
"""헬스체크 정상 상태 확인"""
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 100,
|
|
"total_errors": 2,
|
|
"total_retries": 3,
|
|
"error_rate": 0.02,
|
|
"avg_duration": 0.15,
|
|
"recent_calls": 50,
|
|
}
|
|
response = await client.get("/api/v1/monitoring/health")
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
|
assert data["status"] == "healthy"
|
|
assert "error_rate" in data
|
|
assert "total_calls" in data
|
|
assert "timestamp" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_health_check_unhealthy(self, client: httpx.AsyncClient):
|
|
"""헬스체크 비정상 상태 확인 (오류율 50% 초과)"""
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 10,
|
|
"total_errors": 8,
|
|
"total_retries": 5,
|
|
"error_rate": 0.8,
|
|
"avg_duration": 2.5,
|
|
"recent_calls": 10,
|
|
}
|
|
response = await client.get("/api/v1/monitoring/health")
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
|
assert data["status"] == "unhealthy"
|
|
assert data["error_rate"] == 0.8
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_stats(self, client: httpx.AsyncClient):
|
|
"""API 호출 통계 조회"""
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 200,
|
|
"total_errors": 10,
|
|
"total_retries": 5,
|
|
"error_rate": 0.05,
|
|
"avg_duration": 0.2,
|
|
"recent_calls": 80,
|
|
}
|
|
response = await client.get("/api/v1/monitoring/api-stats")
|
|
assert response.status_code == 200
|
|
|
|
data = response.json()
|
|
assert data["status"] == "healthy"
|
|
assert "stats" in data
|
|
assert data["stats"]["total_calls"] == 200
|
|
assert "message" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_stats_warning_status(self, client: httpx.AsyncClient):
|
|
"""API 통계 — 오류율 30% 초과 시 warning 상태"""
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 100,
|
|
"total_errors": 35,
|
|
"total_retries": 10,
|
|
"error_rate": 0.35,
|
|
"avg_duration": 1.0,
|
|
"recent_calls": 100,
|
|
}
|
|
response = await client.get("/api/v1/monitoring/api-stats")
|
|
assert response.status_code == 200
|
|
assert response.json()["status"] == "warning"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_api_stats_critical_status(self, client: httpx.AsyncClient):
|
|
"""API 통계 — 오류율 50% 초과 시 critical 상태"""
|
|
_mock_kis.get_monitor_stats.return_value = {
|
|
"total_calls": 100,
|
|
"total_errors": 60,
|
|
"total_retries": 20,
|
|
"error_rate": 0.6,
|
|
"avg_duration": 3.0,
|
|
"recent_calls": 100,
|
|
}
|
|
response = await client.get("/api/v1/monitoring/api-stats")
|
|
assert response.status_code == 200
|
|
assert response.json()["status"] == "critical"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_log_stats(self, client: httpx.AsyncClient):
|
|
"""API 통계 로그 기록 요청"""
|
|
response = await client.post("/api/v1/monitoring/log-stats")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "message" in data
|
|
_mock_kis.log_monitor_stats.assert_called()
|
|
|
|
|
|
# ===========================================================================
|
|
# 2. 포트폴리오 엔드포인트 테스트
|
|
# ===========================================================================
|
|
class TestPortfolioEndpoints:
|
|
"""포트폴리오 API 엔드포인트 테스트"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_empty(self, client: httpx.AsyncClient):
|
|
"""포트폴리오 조회 — 보유 종목 없을 때 빈 목록 반환"""
|
|
_reset_portfolio_cache()
|
|
|
|
response = await client.get("/api/v1/portfolio")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_with_cache(self, client: httpx.AsyncClient, sample_portfolio):
|
|
"""포트폴리오 조회 — 캐시에 보유 종목이 있을 때"""
|
|
_reset_portfolio_cache(holdings=sample_portfolio)
|
|
|
|
response = await client.get("/api/v1/portfolio")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 2
|
|
codes = {item["stock_code"] for item in data}
|
|
assert "005930" in codes
|
|
assert "000660" in codes
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_filters_zero_quantity(self, client: httpx.AsyncClient, db_session):
|
|
"""포트폴리오 조회 — 수량 0인 종목은 필터링됨"""
|
|
zero_item = Portfolio(
|
|
id=10,
|
|
stock_code="035420",
|
|
stock_name="NAVER",
|
|
quantity=0,
|
|
average_price=Decimal("200000.00"),
|
|
current_price=Decimal("210000.00"),
|
|
profit_loss=Decimal("0.00"),
|
|
profit_loss_rate=Decimal("0.0000"),
|
|
market="KRX",
|
|
)
|
|
active_item = Portfolio(
|
|
id=11,
|
|
stock_code="035720",
|
|
stock_name="카카오",
|
|
quantity=3,
|
|
average_price=Decimal("50000.00"),
|
|
current_price=Decimal("52000.00"),
|
|
profit_loss=Decimal("6000.00"),
|
|
profit_loss_rate=Decimal("4.0000"),
|
|
market="KRX",
|
|
)
|
|
db_session.add(zero_item)
|
|
db_session.add(active_item)
|
|
await db_session.commit()
|
|
|
|
_reset_portfolio_cache(holdings=[zero_item, active_item])
|
|
|
|
response = await client.get("/api/v1/portfolio")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 1
|
|
assert data[0]["stock_code"] == "035720"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_summary_cached(self, client: httpx.AsyncClient):
|
|
"""포트폴리오 요약 조회 — 캐시된 summary 반환"""
|
|
_reset_portfolio_cache(
|
|
summary={
|
|
"total_stocks": 2,
|
|
"total_value": 1295000.0,
|
|
"total_investment": 1300000.0,
|
|
"total_profit_loss": -5000.0,
|
|
"total_profit_loss_rate": -0.38,
|
|
"available_cash": 500000.0,
|
|
"total_assets": 1795000.0,
|
|
}
|
|
)
|
|
|
|
response = await client.get("/api/v1/portfolio/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["total_stocks"] == 2
|
|
assert data["total_assets"] == 1795000.0
|
|
assert data["sync_status"] == "ok"
|
|
assert "cumulative_realized_pl" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_summary_empty(self, client: httpx.AsyncClient):
|
|
"""포트폴리오 요약 조회 — 캐시 없을 때 기본값 반환"""
|
|
_reset_portfolio_cache()
|
|
|
|
response = await client.get("/api/v1/portfolio/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["total_stocks"] == 0
|
|
assert data["total_value"] == 0
|
|
assert data["total_assets"] == 0
|
|
assert data["sync_status"] == "ok"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_summary_with_sync_error(self, client: httpx.AsyncClient):
|
|
"""포트폴리오 요약 조회 — 동기화 오류 시 에러 상태 포함"""
|
|
_reset_portfolio_cache(
|
|
summary={
|
|
"total_stocks": 1,
|
|
"total_value": 720000.0,
|
|
"total_investment": 700000.0,
|
|
"total_profit_loss": 20000.0,
|
|
"total_profit_loss_rate": 2.86,
|
|
"available_cash": 300000.0,
|
|
"total_assets": 1020000.0,
|
|
},
|
|
last_error="KIS API timeout",
|
|
failure_count=1,
|
|
)
|
|
|
|
response = await client.get("/api/v1/portfolio/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["sync_status"] == "error"
|
|
assert data["last_error"] == "KIS API timeout"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_portfolio_summary_cumulative_realized_pl(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""포트폴리오 요약 — 누적 실현 손익이 올바르게 계산되는지 확인"""
|
|
_reset_portfolio_cache(
|
|
summary={
|
|
"total_stocks": 1,
|
|
"total_value": 360000.0,
|
|
"total_investment": 350000.0,
|
|
"total_profit_loss": 10000.0,
|
|
"total_profit_loss_rate": 2.86,
|
|
"available_cash": 640000.0,
|
|
"total_assets": 1000000.0,
|
|
}
|
|
)
|
|
|
|
response = await client.get("/api/v1/portfolio/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
# sample_trades에서 SELL 체결의 net_profit_loss = 10000
|
|
assert data["cumulative_realized_pl"] == 10000.0
|
|
|
|
|
|
# ===========================================================================
|
|
# 3. 거래 이력 엔드포인트 테스트
|
|
# ===========================================================================
|
|
class TestTradeHistoryEndpoints:
|
|
"""거래 이력 API 엔드포인트 테스트"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_empty(self, client: httpx.AsyncClient):
|
|
"""거래 이력 조회 — 데이터 없을 때 빈 목록"""
|
|
response = await client.get("/api/v1/trades")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_with_data(self, client: httpx.AsyncClient, sample_trades):
|
|
"""거래 이력 조회 — 샘플 데이터 포함"""
|
|
response = await client.get("/api/v1/trades")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 3
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_filter_by_stock_code(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""거래 이력 조회 — 종목 코드 필터"""
|
|
response = await client.get("/api/v1/trades", params={"stock_code": "005930"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert all(t["stock_code"] == "005930" for t in data)
|
|
assert len(data) == 2 # BUY + SELL
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_filter_by_status(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""거래 이력 조회 — 상태 필터 (EXECUTED)"""
|
|
response = await client.get("/api/v1/trades", params={"status": "EXECUTED"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert all(t["status"] == "EXECUTED" for t in data)
|
|
assert len(data) == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_filter_by_order_type(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""거래 이력 조회 — 주문 유형 필터 (SELL)"""
|
|
response = await client.get("/api/v1/trades", params={"order_type": "SELL"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 1
|
|
assert data[0]["order_type"] == "SELL"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_pagination(self, client: httpx.AsyncClient, sample_trades):
|
|
"""거래 이력 조회 — 페이지네이션 (skip, limit)"""
|
|
response = await client.get("/api/v1/trades", params={"skip": 0, "limit": 2})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 2
|
|
|
|
response2 = await client.get("/api/v1/trades", params={"skip": 2, "limit": 2})
|
|
assert response2.status_code == 200
|
|
data2 = response2.json()
|
|
assert len(data2) == 1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_sell_profit_loss(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""거래 이력 조회 — 매도 체결 건에 손익 정보가 포함되는지 확인"""
|
|
response = await client.get(
|
|
"/api/v1/trades", params={"order_type": "SELL", "status": "EXECUTED"}
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 1
|
|
sell_trade = data[0]
|
|
assert sell_trade["profit_loss"] is not None
|
|
assert float(sell_trade["profit_loss"]) == 10000.0
|
|
assert sell_trade["profit_loss_rate"] is not None
|
|
assert abs(sell_trade["profit_loss_rate"] - 2.8571) < 0.01
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_trades_days_filter(
|
|
self, client: httpx.AsyncClient, sample_trades
|
|
):
|
|
"""거래 이력 조회 — days 파라미터로 기간 필터"""
|
|
# days=3 이면 최근 3일 내 거래 (SELL 2일 전 + FAILED 1일 전 = 2건)
|
|
response = await client.get("/api/v1/trades", params={"days": 3})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert len(data) == 2
|
|
|
|
# days=30 이면 모든 거래 포함 (5일, 2일, 1일 전 = 3건)
|
|
response_all = await client.get("/api/v1/trades", params={"days": 30})
|
|
assert response_all.status_code == 200
|
|
assert len(response_all.json()) == 3
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_single_trade(self, client: httpx.AsyncClient, sample_trades):
|
|
"""개별 거래 조회 — 존재하는 trade_id"""
|
|
response = await client.get("/api/v1/trades/1")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == 1
|
|
assert data["stock_code"] == "005930"
|
|
assert data["order_type"] == "BUY"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_single_trade_not_found(self, client: httpx.AsyncClient, sample_trades):
|
|
"""개별 거래 조회 — 존재하지 않는 trade_id는 404"""
|
|
response = await client.get("/api/v1/trades/9999")
|
|
assert response.status_code == 404
|
|
assert "not found" in response.json()["detail"].lower()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_trade_stats_summary(self, client: httpx.AsyncClient, sample_trades):
|
|
"""거래 통계 요약 조회"""
|
|
response = await client.get("/api/v1/trades/stats/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["total_trades"] == 3
|
|
assert data["buy_trades"] == 2
|
|
assert data["sell_trades"] == 1
|
|
assert data["executed_trades"] == 2
|
|
assert data["failed_trades"] == 1
|
|
assert 0 <= data["success_rate"] <= 1
|
|
assert data["total_amount"] > 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_trade_stats_summary_empty(self, client: httpx.AsyncClient):
|
|
"""거래 통계 요약 — 데이터 없을 때"""
|
|
response = await client.get("/api/v1/trades/stats/summary")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["total_trades"] == 0
|
|
assert data["success_rate"] == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_pending_trades(self, client: httpx.AsyncClient, sample_trades):
|
|
"""PENDING/FAILED 거래 정리"""
|
|
response = await client.delete("/api/v1/trades/pending")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["deleted_count"] == 1 # FAILED 1건
|
|
assert "message" in data
|
|
|
|
# 정리 후 FAILED 거래가 없어야 함
|
|
response2 = await client.get("/api/v1/trades", params={"status": "FAILED"})
|
|
assert response2.status_code == 200
|
|
assert len(response2.json()) == 0
|
|
|
|
|
|
# ===========================================================================
|
|
# 4. 인증 강제 테스트 (require_api_key 가 무력화되지 않았음을 보장)
|
|
# ===========================================================================
|
|
class TestApiKeyEnforcement:
|
|
"""[2026-09-27 추가] client 픽스처가 헤더 인증 방식으로 바뀌었으므로,
|
|
인증 자체가 살아 있는지를 별도로 고정한다. 과거처럼 require_api_key 를
|
|
dependency_overrides 로 우회하면 이 테스트가 먼저 깨진다.
|
|
"""
|
|
|
|
@pytest.mark.parametrize(
|
|
"method,path",
|
|
[
|
|
("get", "/api/v1/portfolio"),
|
|
("get", "/api/v1/portfolio/summary"),
|
|
("get", "/api/v1/trades"),
|
|
("get", "/api/v1/trades/stats/summary"),
|
|
("delete", "/api/v1/trades/pending"),
|
|
("get", "/api/v1/monitoring/health"),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_requires_api_key(
|
|
self, unauthenticated_client: httpx.AsyncClient, method: str, path: str
|
|
):
|
|
"""X-API-Key 헤더가 없으면 401"""
|
|
response = await getattr(unauthenticated_client, method)(path)
|
|
assert response.status_code == 401, f"{method.upper()} {path} 가 인증 없이 통과했다"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rejects_invalid_api_key(self, unauthenticated_client: httpx.AsyncClient):
|
|
"""잘못된 키도 401 (세션 토큰으로도 해석 불가)"""
|
|
response = await unauthenticated_client.get(
|
|
"/api/v1/trades", headers={"X-API-Key": "not-a-valid-key"}
|
|
)
|
|
assert response.status_code == 401
|