Files
set50-system/backend/tests/test_dashboard.py
Kunthawat Greethong 9739849f68 [verified] Add real multi-theme dashboard (3 themes + macro + board + sources) — req #6/#8/#9/#10
- dashboard.py: RealDashboard assembles real Thai data (tourism + auto+NPL + energy TOP + macro BOT) with uniform z-score surprise per theme, per-theme thesis, sources provenance table, 49-symbol combined board
- macro_thai.py: BOT Thai Economy macro backdrop (consumption +4.9%, inflation 1.95%, unemployment 0.93%, tourists 16.2mn)
- GET /api/v1/dashboard endpoint (real data, no fixture fallback per user)
- 7 new tests; full suite 195 OK; live verified (3 theme surprise: 0.571/0.81/1.623)
2026-08-25 20:30:14 +07:00

68 lines
2.6 KiB
Python

"""Tests for the real multi-theme dashboard assembly."""
from __future__ import annotations
import unittest
from unittest.mock import patch
from app.dashboard import RealDashboard, _auto_read, _zscore
class _FakeCache:
"""Minimal cache that invokes the fetcher each call."""
def __init__(self, data: dict):
self.data = data
def fetch_or_stale(self, key, fetcher):
val = self.data.get(key)
if val is not None:
return val
return fetcher()
class DashboardTest(unittest.TestCase):
def test_zscore_centers(self):
self.assertAlmostEqual(_zscore(1.0, 1.0, 1.0), 0.0)
self.assertAlmostEqual(_zscore(2.0, 1.0, 1.0), 1.0)
def test_auto_read_multisource(self):
auto = {"new_car_sales_yoy": 20.07, "total_vehicle_sales": 59000,
"vehicle_production": 120000, "auto_exports": 80000}
npl = {"pct_of_npls": 3.95, "npl_amount": 20602}
read = _auto_read(auto, npl, _FakeCache({}))
self.assertEqual(read["new_car_sales_yoy"], 20.07)
self.assertEqual(read["auto_npl_pct"], 3.95)
self.assertIn("thesis", read)
@patch("app.auto_credit.fetch_auto_credit")
@patch("app.auto_npl.fetch_auto_npl")
@patch("app.energy_thai.fetch_energy_thai")
@patch("app.macro_thai.fetch_macro_thai")
def test_build_returns_structure(self, macro, energy, npl, auto):
class _Factory:
def __init__(self, data): self._data = data
def to_dict(self): return self._data
macro.return_value = _Factory({
"private_consumption_yoy": 4.9, "headline_inflation_yoy": 1.95, "periods": {}})
energy.return_value = _Factory({
"quarterly": {"Q2/2026": {"net_profit": 8000.0, "ebitda": 9000.0}}})
auto.return_value = _Factory({
"new_car_sales_yoy": 20.07, "total_vehicle_sales": 59000})
npl.return_value = _Factory({
"pct_of_npls": 3.95, "npl_amount": 20602, "period": "Q2/2568"})
cache = _FakeCache({})
dash = RealDashboard([], cache).build()
self.assertEqual(len(dash["themes"]), 3)
self.assertEqual(len(dash["sources"]), 5)
self.assertIn("macro", dash)
self.assertIn("board", dash)
def test_auto_read_npl_piece(self):
read = _auto_read({"new_car_sales_yoy": -3.0, "total_vehicle_sales": 20000},
{"pct_of_npls": 6.0, "npl_amount": 90000}, _FakeCache({}))
self.assertEqual(read["new_car_sales_yoy"], -3.0)
self.assertEqual(read["auto_npl_pct"], 6.0)
if __name__ == "__main__":
unittest.main()