forked from ZhuLinsen/daily_stock_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pipeline_realtime_indicators.py
More file actions
229 lines (197 loc) · 8.82 KB
/
test_pipeline_realtime_indicators.py
File metadata and controls
229 lines (197 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
Unit tests for Issue #234: intraday realtime technical indicators.
Covers:
- _augment_historical_with_realtime: append/update logic, guards
- _compute_ma_status: MA alignment string
- _enhance_context: today override with realtime + trend_result
"""
import os
import sys
import unittest
from datetime import date, timedelta
from unittest.mock import MagicMock, patch
import pandas as pd
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from data_provider.realtime_types import UnifiedRealtimeQuote, RealtimeSource
from src.stock_analyzer import StockTrendAnalyzer, TrendAnalysisResult, TrendStatus
from src.core.pipeline import StockAnalysisPipeline
def _make_realtime_quote(
price: float = 15.72,
open_price: float = 15.62,
high: float = 16.29,
low: float = 15.55,
volume: int = 13995600,
change_pct: float = 0.96,
) -> UnifiedRealtimeQuote:
return UnifiedRealtimeQuote(
code="600519",
name="贵州茅台",
source=RealtimeSource.TENCENT,
price=price,
open_price=open_price,
high=high,
low=low,
volume=volume,
change_pct=change_pct,
)
def _make_historical_df(days: int = 25, last_date: date = None) -> pd.DataFrame:
"""Build historical OHLCV DataFrame."""
if last_date is None:
last_date = date.today() - timedelta(days=1)
dates = [last_date - timedelta(days=i) for i in range(days - 1, -1, -1)]
base = 100.0
data = []
for i, d in enumerate(dates):
close = base + i * 0.5
data.append({
"code": "600519",
"date": d,
"open": close - 0.2,
"high": close + 0.3,
"low": close - 0.3,
"close": close,
"volume": 1000000 + i * 10000,
"amount": close * (1000000 + i * 10000),
"pct_chg": 0.5,
"ma5": close,
"ma10": close - 0.1,
"ma20": close - 0.2,
"volume_ratio": 1.0,
})
return pd.DataFrame(data)
class TestAugmentHistoricalWithRealtime(unittest.TestCase):
"""Tests for _augment_historical_with_realtime."""
def setUp(self) -> None:
self._db_path = os.path.join(
os.path.dirname(__file__), "..", "data", "test_issue234.db"
)
os.makedirs(os.path.dirname(self._db_path), exist_ok=True)
with patch.dict(os.environ, {"DATABASE_PATH": self._db_path}):
from src.config import Config
Config._instance = None
self.config = Config._load_from_env()
self.pipeline = StockAnalysisPipeline(config=self.config)
def test_returns_unchanged_when_realtime_none(self) -> None:
df = _make_historical_df()
result = self.pipeline._augment_historical_with_realtime(df, None, "600519")
self.assertIs(result, df)
self.assertEqual(len(result), len(df))
def test_returns_unchanged_when_price_invalid(self) -> None:
df = _make_historical_df()
quote = _make_realtime_quote(price=0)
result = self.pipeline._augment_historical_with_realtime(df, quote, "600519")
self.assertEqual(len(result), len(df))
quote2 = MagicMock()
quote2.price = None
result2 = self.pipeline._augment_historical_with_realtime(df, quote2, "600519")
self.assertEqual(len(result2), len(df))
def test_returns_unchanged_when_df_empty(self) -> None:
df = pd.DataFrame()
quote = _make_realtime_quote()
result = self.pipeline._augment_historical_with_realtime(df, quote, "600519")
self.assertTrue(result.empty)
def test_returns_unchanged_when_df_missing_close(self) -> None:
df = pd.DataFrame({"date": [date.today()], "open": [100]})
quote = _make_realtime_quote()
result = self.pipeline._augment_historical_with_realtime(df, quote, "600519")
self.assertEqual(len(result), 1)
self.assertNotIn("close", result.columns)
@patch("src.core.pipeline.is_market_open", return_value=True)
@patch("src.core.pipeline.get_market_for_stock", return_value="cn")
def test_appends_row_when_last_date_before_today(
self, _mock_market, _mock_open
) -> None:
df = _make_historical_df(last_date=date.today() - timedelta(days=1))
quote = _make_realtime_quote(price=15.72)
result = self.pipeline._augment_historical_with_realtime(df, quote, "600519")
self.assertEqual(len(result), len(df) + 1)
last = result.iloc[-1]
self.assertEqual(last["close"], 15.72)
self.assertEqual(last["date"], date.today())
@patch("src.core.pipeline.is_market_open", return_value=True)
@patch("src.core.pipeline.get_market_for_stock", return_value="cn")
def test_updates_last_row_when_last_date_is_today(
self, _mock_market, _mock_open
) -> None:
df = _make_historical_df(last_date=date.today(), days=25)
df.loc[df.index[-1], "date"] = date.today()
quote = _make_realtime_quote(price=16.0)
result = self.pipeline._augment_historical_with_realtime(df, quote, "600519")
self.assertEqual(len(result), len(df))
self.assertEqual(result.iloc[-1]["close"], 16.0)
class TestComputeMaStatus(unittest.TestCase):
"""Tests for _compute_ma_status."""
def test_bullish_alignment(self) -> None:
status = StockAnalysisPipeline._compute_ma_status(11, 10, 9.5, 9)
self.assertIn("多头", status)
def test_bearish_alignment(self) -> None:
status = StockAnalysisPipeline._compute_ma_status(8, 9, 9.5, 10)
self.assertIn("空头", status)
def test_consolidation(self) -> None:
status = StockAnalysisPipeline._compute_ma_status(10, 10, 10, 10)
self.assertIn("震荡", status)
class TestEnhanceContextRealtimeOverride(unittest.TestCase):
"""Tests for _enhance_context today override with realtime + trend."""
def setUp(self) -> None:
self._db_path = os.path.join(
os.path.dirname(__file__), "..", "data", "test_issue234.db"
)
os.makedirs(os.path.dirname(self._db_path), exist_ok=True)
with patch.dict(os.environ, {"DATABASE_PATH": self._db_path}):
from src.config import Config
Config._instance = None
self.config = Config._load_from_env()
self.pipeline = StockAnalysisPipeline(config=self.config)
def test_today_overridden_when_realtime_and_trend_exist(self) -> None:
context = {
"code": "600519",
"date": (date.today() - timedelta(days=1)).isoformat(),
"today": {"close": 15.0, "ma5": 14.8, "ma10": 14.5},
"yesterday": {"close": 14.5, "volume": 1000000},
}
quote = _make_realtime_quote(price=15.72, volume=2000000)
trend = TrendAnalysisResult(
code="600519",
trend_status=TrendStatus.BULL,
ma5=15.5,
ma10=15.2,
ma20=14.9,
)
enhanced = self.pipeline._enhance_context(
context, quote, None, trend, "贵州茅台"
)
self.assertEqual(enhanced["today"]["close"], 15.72)
self.assertEqual(enhanced["today"]["ma5"], 15.5)
self.assertEqual(enhanced["today"]["ma10"], 15.2)
self.assertEqual(enhanced["today"]["ma20"], 14.9)
self.assertIn("多头", enhanced["ma_status"])
self.assertEqual(enhanced["date"], date.today().isoformat())
self.assertIn("price_change_ratio", enhanced)
self.assertIn("volume_change_ratio", enhanced)
def test_today_not_overridden_when_trend_missing(self) -> None:
context = {"code": "600519", "today": {"close": 15.0}}
quote = _make_realtime_quote(price=15.72)
enhanced = self.pipeline._enhance_context(
context, quote, None, None, "贵州茅台"
)
self.assertEqual(enhanced["today"]["close"], 15.0)
def test_today_not_overridden_when_realtime_missing(self) -> None:
context = {"code": "600519", "today": {"close": 15.0}}
trend = TrendAnalysisResult(code="600519", ma5=15.0, ma10=14.8, ma20=14.5)
enhanced = self.pipeline._enhance_context(
context, None, None, trend, "贵州茅台"
)
self.assertEqual(enhanced["today"]["close"], 15.0)
def test_today_not_overridden_when_trend_ma_zero(self) -> None:
"""When StockTrendAnalyzer returns early (data insufficient), ma5=0.0. Must not override."""
context = {"code": "600519", "today": {"close": 15.0, "ma5": 14.8}}
quote = _make_realtime_quote(price=15.72)
trend = TrendAnalysisResult(code="600519") # defaults: ma5=ma10=ma20=0.0
enhanced = self.pipeline._enhance_context(
context, quote, None, trend, "贵州茅台"
)
self.assertEqual(enhanced["today"]["close"], 15.0)
self.assertEqual(enhanced["today"]["ma5"], 14.8)
if __name__ == "__main__":
unittest.main()