Spaces:
Running
Running
| import unittest | |
| from unittest.mock import patch | |
| from fastapi.testclient import TestClient | |
| from app import app | |
| from bucklake_ai.errors import InvalidScalerArtifact, ScalerArtifactNotFound | |
| class AppErrorHandlerTests(unittest.TestCase): | |
| def _payload(self): | |
| return { | |
| "symbol": "AAPL", | |
| "published_at": "2026-04-15T12:30:00Z", | |
| "market_bars": { | |
| name: [ | |
| { | |
| "open": 100.0 + i, | |
| "close": 101.0 + i, | |
| "high": 102.0 + i, | |
| "low": 99.0 + i, | |
| "volume": 1000.0 + i, | |
| "amount": 0.0, | |
| } | |
| for i in range(30) | |
| ] | |
| for name in ("stock", "inx", "dj", "ixic", "ndx") | |
| }, | |
| } | |
| def test_scaler_artifact_not_found_returns_503(self): | |
| with patch( | |
| "app.service.predict", | |
| side_effect=ScalerArtifactNotFound("missing scaler"), | |
| ): | |
| response = TestClient(app).post("/api/predict/v2", json=self._payload()) | |
| self.assertEqual(response.status_code, 503) | |
| self.assertEqual(response.json()["error"], "scaler_artifact_not_found") | |
| def test_invalid_scaler_artifact_returns_503(self): | |
| with patch( | |
| "app.service.predict", | |
| side_effect=InvalidScalerArtifact("bad scaler"), | |
| ): | |
| response = TestClient(app).post("/api/predict/v2", json=self._payload()) | |
| self.assertEqual(response.status_code, 503) | |
| self.assertEqual(response.json()["error"], "invalid_scaler_artifact") | |
| if __name__ == "__main__": | |
| unittest.main() | |