AppContext extended with kis_client + kis_ws. lifespan: - If KIS_APP_KEY set: create KISClient + KISWebSocket, fetch portfolio, subscribe WebSocket H0STASP0 for holdings. - If unset: WARNING log, signal_v2 still serves /health (no KIS data). - Shutdown closes kis_ws → kis_client → stock client in order. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
"""Tests for FastAPI main app."""
|
|
import logging
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
def test_health_endpoint_returns_status_online(monkeypatch):
|
|
monkeypatch.setenv("STOCK_API_URL", "https://test.stock.local")
|
|
monkeypatch.setenv("WEBAI_API_KEY", "test-secret")
|
|
# Reload modules so they pick up the new env
|
|
import importlib
|
|
from signal_v2 import config as cfg
|
|
importlib.reload(cfg)
|
|
from signal_v2 import main as main_mod
|
|
importlib.reload(main_mod)
|
|
with TestClient(main_mod.app) as client:
|
|
r = client.get("/health")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["status"] == "online"
|
|
assert body["stock_api_url"] == "https://test.stock.local"
|
|
|
|
|
|
def test_startup_warns_if_webai_api_key_missing(monkeypatch, caplog):
|
|
monkeypatch.delenv("WEBAI_API_KEY", raising=False)
|
|
monkeypatch.setenv("STOCK_API_URL", "https://test.stock.local")
|
|
import importlib
|
|
from signal_v2 import config as cfg
|
|
importlib.reload(cfg)
|
|
from signal_v2 import main as main_mod
|
|
importlib.reload(main_mod)
|
|
with caplog.at_level(logging.WARNING, logger="signal_v2.main"):
|
|
with TestClient(main_mod.app) as client:
|
|
client.get("/health")
|
|
assert any("WEBAI_API_KEY" in rec.message for rec in caplog.records)
|
|
|
|
|
|
def test_startup_warns_if_kis_app_key_missing(monkeypatch, caplog):
|
|
"""KIS_APP_KEY 미설정 시 startup WARNING (KIS 호출 disabled)."""
|
|
monkeypatch.setenv("STOCK_API_URL", "https://test.stock.local")
|
|
monkeypatch.setenv("WEBAI_API_KEY", "test-secret")
|
|
monkeypatch.delenv("KIS_APP_KEY", raising=False)
|
|
|
|
import importlib
|
|
from signal_v2 import config as cfg
|
|
importlib.reload(cfg)
|
|
from signal_v2 import main as main_mod
|
|
importlib.reload(main_mod)
|
|
with caplog.at_level(logging.WARNING, logger="signal_v2.main"):
|
|
with TestClient(main_mod.app) as client:
|
|
client.get("/health")
|
|
assert any("KIS_APP_KEY" in rec.message for rec in caplog.records)
|