"""Tests for the IRPC refining-margin collector + theme wiring."""
from __future__ import annotations
import unittest
from app import energy_irpc, themes
from app.energy_irpc import EnergyIrpcSnapshot, parse_energy_irpc_html
_PERF_HTML = """
| Financial Highlights | 2024 | 2025 | 3M26 |
| Current Assets | 56,999 | 67,086 | 101,978 |
| Total Assets | 184,555 | 187,383 | 217,356 |
| EBITDA Margin | 1.42% | 2.22% | 19.19% |
| Net Profit Margin | (1.65%) | (1.28%) | 10.27% |
| Return on Equity | (7.12%) | (5.26%) | 7.75% |
"""
class EnergyIrpcParseTest(unittest.TestCase):
def test_parses_margins(self):
snap = parse_energy_irpc_html(_PERF_HTML)
self.assertIsInstance(snap, EnergyIrpcSnapshot)
self.assertEqual(snap.columns, ["2024", "2025", "3M26"])
self.assertEqual(snap.net_margin_pct, 10.27)
self.assertEqual(snap.ebitda_margin_pct, 19.19)
self.assertEqual(snap.roe_pct, 7.75)
# latest period is the last column
self.assertEqual(snap.period, "3M26")
def test_parses_negative_parens(self):
# "(1.65%)" -> -1.65
snap = parse_energy_irpc_html(_PERF_HTML)
# net_margin picks the LAST column (3M26, +10.27), not the negative one;
# verify the paren parser separately on a table where the last col is negative
html_neg = _PERF_HTML.replace("10.27% | ", "(3.20%) | ")
snap2 = parse_energy_irpc_html(html_neg)
self.assertEqual(snap2.net_margin_pct, -3.20)
def test_to_dict_full(self):
d = parse_energy_irpc_html(_PERF_HTML).to_dict()
self.assertIn("net_margin_pct", d)
self.assertIn("period", d)
self.assertIn("source", d)
def test_missing_values_raise(self):
html_no = ""
with self.assertRaises(energy_irpc.EnergyIrpcError):
parse_energy_irpc_html(html_no)
def test_every_factor_value_key_resolves(self):
from app import factors
keys = set(EnergyIrpcSnapshot().to_dict().keys())
for fkey, fact in factors.FACTORS.items():
if fact.get("fetch") != "energy_irpc":
continue
self.assertIn(
fact.get("value_key"), keys,
f"factor {fkey!r} value_key not emitted by energy_irpc",
)
class EnergyIrpcThemeDirectionTest(unittest.TestCase):
"""IRPC margin must move energy themes the intended (bullish) direction."""
@staticmethod
def _fetched(**irpc):
base = {
"macro_thai": {
"private_consumption_yoy": 4.9, "private_investment_yoy": 18.1,
"headline_inflation_yoy": 1.95, "core_inflation_yoy": 1.0,
"unemployment_pct": 1.0, "manufacturing_yoy": -3.1,
"tourists_ytd_mn": 16.2,
},
"auto_credit": {"new_car_sales_yoy": 20.07, "vehicle_production": 117383.0,
"auto_exports": 81526.0},
"auto_npl": {"pct_of_npls": 3.0},
"bank_npl": {"pct_of_npls": 1.0},
"energy_thai": {"quarterly": {"Q1/2026": {"net_profit": 19481.0, "sales": 114809.0}}},
"energy_irpc": {"net_margin_pct": irpc.get("net", 0.0)},
"thai_trade": {"current_account_usdm": 500.0, "exports_usdm": 34000.0,
"imports_usdm": 38000.0},
"te_thailand": {
"interest_rate_pct": 1.5, "loans_to_fin_corp": 10000000.0,
"consumer_credit_thbmn": 5000000.0, "household_debt_gdp_pct": 85.0,
"retail_sales_yoy": 0.0, "consumer_confidence": 50.0,
"property_prices_yoy": 0.0, "business_confidence": 50.0,
},
}
# refresh energy_irpc from kwargs (neutral default 0.0 used above)
base["energy_irpc"] = {"net_margin_pct": irpc.get("net", 0.0)}
return base
def test_higher_irpc_margin_raises_energy_themes(self):
from app import themes
low = themes.compute_theme_surprises(self._fetched(net=-3.0))
high = themes.compute_theme_surprises(self._fetched(net=10.0))
self.assertGreater(high["refining_energy"], low["refining_energy"])
self.assertGreater(high["exploration"], low["exploration"])
self.assertGreater(high["utilities"], low["utilities"])
if __name__ == "__main__":
unittest.main()