stock-bot/apps/stock/analytics/test/test_unavailable_endpoints.py

55 lines
2.5 KiB
Python

"""HTTP contract tests for the observed-data routes, without loading research/ML runtimes."""
import importlib.util
from pathlib import Path
import unittest
from fastapi import FastAPI
from fastapi.testclient import TestClient
class ObservedDataEndpoints(unittest.TestCase):
@classmethod
def setUpClass(cls):
source = Path(__file__).parents[1] / "src/api/endpoints/analytics.py"
spec = importlib.util.spec_from_file_location("observed_analytics_endpoints", source)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
app = FastAPI()
app.include_router(module.router, prefix="/analytics")
cls.client = TestClient(app)
@classmethod
def tearDownClass(cls):
cls.client.close()
def test_missing_observed_data_is_unavailable_not_generated_financial_results(self):
requests = [
("GET", "/performance/P?start_date=2024-01-01T00:00:00Z&end_date=2024-02-01T00:00:00Z", None),
("GET", "/risk/P", None),
("GET", "/regime", None),
("POST", "/correlation", ["A.US", "B.US"]),
("GET", "/backtest/run", None),
("POST", "/attribution?portfolio_id=P&benchmark=B&start_date=2024-01-01&end_date=2024-02-01", None),
]
for method, path, body in requests:
with self.subTest(path=path):
response = self.client.request(method, "/analytics" + path, json=body)
self.assertEqual(response.status_code, 503)
self.assertEqual(response.json()["detail"]["code"], "analytics_data_unavailable")
self.assertNotIn("metrics", response.json())
self.assertNotIn("matrix", response.json())
def test_invalid_ranges_and_duplicate_symbols_are_client_errors(self):
response = self.client.get("/analytics/performance/P?start_date=2024-02-01T00:00:00Z&end_date=2024-01-01T00:00:00Z")
self.assertEqual(response.status_code, 400)
response = self.client.post("/analytics/correlation", json=["A.US", "A.US"])
self.assertEqual(response.status_code, 400)
def test_framework_validates_dates_windows_and_request_shapes(self):
self.assertEqual(self.client.get("/analytics/risk/P?window=0").status_code, 422)
self.assertEqual(self.client.get("/analytics/performance/P").status_code, 422)
self.assertEqual(self.client.post("/analytics/correlation", json={"symbols": ["A.US", "B.US"]}).status_code, 422)
if __name__ == "__main__":
unittest.main()