From 2fa3e25841826aa4e8f95d988024f16149793368 Mon Sep 17 00:00:00 2001
From: log
Date: Sat, 11 Jul 2026 08:44:15 +0800
Subject: [PATCH] fix: make daily factor calculation repeatable

---
 tests/test_daily_master.py |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/tests/test_daily_master.py b/tests/test_daily_master.py
index 058b375..3dca1bf 100644
--- a/tests/test_daily_master.py
+++ b/tests/test_daily_master.py
@@ -2,6 +2,7 @@
 import sys
 import unittest
 from pathlib import Path
+from unittest.mock import Mock
 
 import pandas as pd
 
@@ -10,6 +11,7 @@
 sys.path.insert(0, str(PROJECT_ROOT))
 
 daily_master = importlib.import_module("scripts.daily_master")
+enhanced_factors = importlib.import_module("scripts.enhanced_factor_system")
 
 
 class LatestCompleteCrossSectionTests(unittest.TestCase):
@@ -94,5 +96,48 @@
         self.assertEqual(master.ml_optimization_status, "未应用(模型尚未训练)")
 
 
+class EnhancedFactorPipelineTests(unittest.TestCase):
+    def test_cross_sectional_factors_can_be_recalculated(self):
+        dates = pd.bdate_range(end="2026-07-10", periods=25)
+        rows = []
+        for code, price_step in (("600001", 0.1), ("600002", 0.2)):
+            rows.extend(
+                {
+                    "date": date,
+                    "stock_code": code,
+                    "close": 10 + index * price_step,
+                    "amount": 1_000_000 + index,
+                    "change_pct": price_step,
+                    "ind_mom_val": -999.0,
+                }
+                for index, date in enumerate(dates)
+            )
+
+        system = enhanced_factors.EnhancedFactorSystem()
+        calculated = system.calculate_cross_sectional_factors(pd.DataFrame(rows))
+        recalculated = system.calculate_cross_sectional_factors(calculated)
+
+        self.assertIn("ind_mom_val", recalculated.columns)
+        self.assertNotIn("ind_mom_val_x", recalculated.columns)
+        self.assertNotIn("ind_mom_val_y", recalculated.columns)
+        expected = recalculated.groupby(["date", "industry"])["momentum_20"].transform("mean")
+        pd.testing.assert_series_equal(
+            recalculated["ind_mom_val"],
+            expected,
+            check_names=False,
+        )
+
+    def test_daily_run_stops_when_enhanced_factors_fail(self):
+        master = daily_master.DailyMaster.__new__(daily_master.DailyMaster)
+        master.load_data = Mock(return_value=pd.DataFrame([{"stock_code": "600001"}]))
+        master._run_advanced_factor_system = Mock(return_value=None)
+        master.evaluate_factors = Mock()
+
+        result = master.run()
+
+        self.assertFalse(result)
+        master.evaluate_factors.assert_not_called()
+
+
 if __name__ == "__main__":
     unittest.main()

--
Gitblit v1.8.0