poll_loop: asyncio.gather parallel fetch of 3 endpoints (portfolio, news_sentiment, screener_preview) + state update. main.py: FastAPI lifespan creates StockClient/SignalDedup/shutdown.Event then spawns poll_loop as background task. GET /health reports status, last poll times, cache size. Signal V2 test suite: 19/19 PASS. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
37 lines
1.3 KiB
Python
37 lines
1.3 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)
|