- dashboard._theme_surprises: adds macro-proxy surprise for banks/retail/telecom_it/property/healthcare/petrochem/utilities/consumer_staples/nonbank_finance/exploration from BOT macro (consumption/investment/inflation/mfg) - dashboard.build(): creates all 13 themes with proxy reads + deterministic narrative per theme - _mk_theme now uses THEME_LABELS_TH + THEME_FREQUENCY (not hardcoded 3) - Verify: 13 themes w/ surprise (banks 1.0, retail 0.19, utilities -0.31, exploration 1.62); BBL modal (ธนาคาร 1.00σ, combined 0.467) - Full suite 199 OK; fixed test_build themes=13
68 lines
2.6 KiB
Python
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"]), 13) # all SET50 themes
|
|
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()
|