Files
set50-system/backend/tests/test_themes.py
T
Kunthawat Greethong 33a4662cd4 [verified] Add per-symbol analysis breakdown endpoint + per-theme narrative
- themes.symbol_breakdown(): transparent scoring derivation (theme_score, siamchart_score components, combined = 0.6*theme + 0.4*siamchart npolut)
- GET /api/v1/symbols/<symbol>: themes + theme surprise contributions + fundamentals + price + weights (ข้อ 7)
- dashboard.py _theme_narrative(): long-form Thai explanation of each theme's analysis outcome + implication for its stocks (ข้อ 5)
- 2 tests; full suite OK; live verified (AOT: combined 0.107 = 0.6*0.571 + 0.4*(-0.588))
2026-08-25 21:18:20 +07:00

106 lines
4.5 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)
self.assertEqual(d["themes"], ["refining_energy"]) # BANPU in energy map
self.assertEqual(d["theme_contributions"], []) # but no surprise set -> 0