- factors.py: FACTORS registry (10 declarative entries: source/fetch/frequency/sign/weight) + normalize/z-score helpers. Add a source = one dict entry, no scoring-function edit. - themes.THEMES: 13 themes reference FACTORS with per-theme weights (flexible), replacing hardcoded _theme_surprises/_theme_narrative. - themes.quality_within_theme(): per-symbol quality vs theme cohort (ROE/EPS) -> real stock picking. dashboard board now surprise×quality (BBL 0.5 vs KTB 1.5 in banks). - board rows carry per-symbol themes[]; /api/v1/themes delegates to RealDashboard.build() -> 13-theme consistency with /api/v1/dashboard (removed 115 lines dead dup logic). - frontend: deleted THEME_BY_SYMBOL/themeLabelById hardcode; theme column + modal labels+quality all from API. Modal shows surprise×quality=theme_score. - Tests: 202 OK (quality selection, breakdown quality, themes/dashboard consistency). - Verified: BBL modal 1.00σ×0.5=0.50σ; KTB 1.5 vs BBL 0.5, PTT 2 themes; 49/49 rows theme from API.
135 lines
6.0 KiB
Python
135 lines
6.0 KiB
Python
"""Tests for the multi-theme registry + scoring."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from app import themes
|
|
|
|
|
|
class ThemesTest(unittest.TestCase):
|
|
def test_list_themes_has_three(self) -> None:
|
|
t = themes.list_themes()
|
|
self.assertEqual(len(t), 3)
|
|
ids = {x.id for x in t}
|
|
self.assertEqual(ids, {"tourism", "auto_credit", "refining_energy"})
|
|
for x in t:
|
|
self.assertTrue(x.label_th) # Thai label present
|
|
|
|
def test_exposure_mapping_contains_expected(self) -> None:
|
|
self.assertIn("AOT", themes.THEME_SYMBOLS["tourism"])
|
|
self.assertIn("PTT", themes.THEME_SYMBOLS["refining_energy"])
|
|
self.assertIn("TISCO", themes.THEME_SYMBOLS["auto_credit"])
|
|
|
|
def test_frequency_recorded_per_theme(self) -> None:
|
|
self.assertEqual(themes.THEME_FREQUENCY["refining_energy"], "quarterly")
|
|
self.assertEqual(themes.THEME_FREQUENCY["tourism"], "monthly")
|
|
|
|
def test_build_theme_scores_z_normalizes(self) -> None:
|
|
signals = [
|
|
{"symbol": "A", "score": 10.0},
|
|
{"symbol": "B", "score": 5.0},
|
|
{"symbol": "C", "score": 0.0},
|
|
]
|
|
scores = themes.build_theme_scores("tourism", signals)
|
|
self.assertAlmostEqual(scores["A"], 1.224, places=2)
|
|
self.assertAlmostEqual(scores["B"], 0.0, places=2)
|
|
self.assertAlmostEqual(scores["C"], -1.224, places=2)
|
|
|
|
def test_siamchart_score_uses_growth_and_yield(self) -> None:
|
|
factors = {
|
|
"factors": [
|
|
{"symbol": "X", "eps_growth_yoy": 10.0, "dividend_yield": 2.0},
|
|
{"symbol": "Y", "eps_growth_yoy": -5.0, "dividend_yield": 0.0},
|
|
]
|
|
}
|
|
sc = themes.build_siamchart_score(factors)
|
|
# X = 10 + 2*2 = 14 ; Y = -5 -> X higher
|
|
self.assertGreater(sc["X"], sc["Y"])
|
|
|
|
def test_combine_60_40(self) -> None:
|
|
theme_scores = [{"A": 1.0, "B": -1.0}]
|
|
siamchart = {"A": 2.0, "B": 0.0}
|
|
merged = themes.combine_score(theme_scores, siamchart)
|
|
# A: 0.6*1.0 + 0.4*2.0 = 1.4 ; B: 0.6*(-1) + 0.4*0 = -0.6
|
|
self.assertAlmostEqual(merged["A"]["combined"], 1.4, places=5)
|
|
self.assertAlmostEqual(merged["B"]["combined"], -0.6, places=5)
|
|
self.assertAlmostEqual(merged["A"]["theme_score"], 1.0, places=5)
|
|
|
|
def test_multiple_themes_average(self) -> None:
|
|
# Symbol in two themes -> theme_score is the mean.
|
|
t1 = {"A": 1.0}
|
|
t2 = {"A": 3.0}
|
|
merged = themes.combine_score([t1, t2], {})
|
|
self.assertAlmostEqual(merged["A"]["theme_score"], 2.0, places=5)
|
|
|
|
def test_missing_sym_siamchart_gets_theme_only(self) -> None:
|
|
merged = themes.combine_score([{"A": 2.0}], {})
|
|
self.assertAlmostEqual(merged["A"]["combined"], 0.6 * 2.0, places=5)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|
|
|
|
class SymbolBreakdownTest(unittest.TestCase):
|
|
def test_breakdown_shows_components(self):
|
|
factor_view = {
|
|
"factors": [
|
|
{"symbol": "AOT", "eps_growth_yoy": 10.0, "dividend_yield": 2.0,
|
|
"pe": 20.0, "eps": 5.0, "pbv": 2.0, "roe": 15.0, "is_dividend": True,
|
|
"company_name": "Airports"},
|
|
]
|
|
}
|
|
theme_surprises = {"tourism": 0.57, "auto_credit": 0.81, "refining_energy": 1.62}
|
|
from app import themes
|
|
d = themes.symbol_breakdown("AOT", factor_view=factor_view, theme_surprises=theme_surprises,
|
|
latest_price=67.0, price_date="2026-08-21")
|
|
self.assertEqual(d["symbol"], "AOT")
|
|
self.assertEqual(d["themes"], ["tourism"]) # AOT in tourism map
|
|
self.assertEqual(d["theme_contributions"][0]["surprise"], 0.57)
|
|
self.assertIn("siamchart_components", d)
|
|
self.assertEqual(d["weights"], {"theme": 0.6, "siamchart": 0.4})
|
|
self.assertIsInstance(d["combined_score"], float)
|
|
self.assertEqual(d["price"]["latest"], 67.0)
|
|
self.assertIn("fundamentals", d)
|
|
|
|
def test_breakdown_symbol_without_theme(self):
|
|
factor_view = {"factors": [{"symbol": "BANPU", "eps_growth_yoy": -2.0,
|
|
"dividend_yield": 0.0, "is_dividend": False,
|
|
"company_name": "BANPU"}]}
|
|
from app import themes
|
|
d = themes.symbol_breakdown("BANPU", factor_view=factor_view, theme_surprises={})
|
|
self.assertEqual(d["theme_score"], 0.0)
|
|
# BANPU is in energy/petrochem/utilities maps (full SET50 coverage)
|
|
self.assertTrue(set(d["themes"]) >= {"refining_energy", "petrochem_materials", "utilities"})
|
|
# no surprises set -> every contribution has surprise=None and theme_score 0
|
|
self.assertTrue(all(c["surprise"] is None for c in d["theme_contributions"]))
|
|
|
|
|
|
class QualitySelectionTest(unittest.TestCase):
|
|
def test_quality_differentiates_strong_vs_weak_in_theme(self):
|
|
from app import themes
|
|
fv = {"factors": [
|
|
{"symbol": "BBL", "roe": 12.0, "eps_growth_yoy": 8.0},
|
|
{"symbol": "KBANK", "roe": 10.0, "eps_growth_yoy": 5.0},
|
|
{"symbol": "KTB", "roe": 9.0, "eps_growth_yoy": 3.0},
|
|
{"symbol": "SCB", "roe": 8.0, "eps_growth_yoy": 2.0},
|
|
{"symbol": "TTB", "roe": 5.0, "eps_growth_yoy": -2.0},
|
|
]}
|
|
q_bbl = themes.quality_within_theme("BBL", "banks", fv)
|
|
q_ttb = themes.quality_within_theme("TTB", "banks", fv)
|
|
self.assertGreater(q_bbl, q_ttb) # strong bank outscores weak one
|
|
|
|
def test_breakdown_has_quality_and_theme_score_per_theme(self):
|
|
from app import themes
|
|
fv = {"factors": [
|
|
{"symbol": "BBL", "roe": 12.0, "eps_growth_yoy": 8.0},
|
|
{"symbol": "KTB", "roe": 9.0, "eps_growth_yoy": 3.0},
|
|
]}
|
|
d = themes.symbol_breakdown("BBL", factor_view=fv, theme_surprises={"banks": 1.0})
|
|
contrib = next(c for c in d["theme_contributions"] if c["theme"] == "banks")
|
|
self.assertIn("quality", contrib)
|
|
self.assertIn("theme_score", contrib)
|
|
self.assertAlmostEqual(contrib["theme_score"], contrib["surprise"] * contrib["quality"], places=3)
|