70 lines
3.5 KiB
Python
70 lines
3.5 KiB
Python
"""取数和样本入口的沪深范围回归,仅内存假数据,不访问服务。"""
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import pandas as pd
|
|
import sources
|
|
|
|
DAY = '2026-11-04'
|
|
|
|
|
|
class SourcesScopeTest(unittest.TestCase):
|
|
def test_moved_members_filters_sql_and_rechecks_returned_codes(self):
|
|
rows = [{'ts_code': code, 'target': '测试环节', 'n_sources': 2, 'chain_fit': 1,
|
|
'members_total': 3, 'moved': 1, 'mkt_trade_date': DAY}
|
|
for code in ('600000.SH', 'sz000001', '920001.BJ', '600000.SZ')]
|
|
with patch.object(sources.db, 'read_pg', return_value=pd.DataFrame(rows)) as reader:
|
|
result = sources.moved_members(DAY)
|
|
self.assertEqual(set(result), {'SH600000', 'SZ000001'})
|
|
self.assertIn("UPPER(TRIM(CAST(ts_code AS TEXT))) ~", reader.call_args.args[0])
|
|
self.assertEqual(reader.call_args.args[1], (DAY,))
|
|
|
|
def test_daily_stock_filters_before_exposing_evidence(self):
|
|
rows = [{'ts_code': code, 'pct_change': 3, 'net_z': 1, 'heat_chg': 0.2}
|
|
for code in ('688001.SH', '300001', 'BJ920001', '000001.SH')]
|
|
with patch.object(sources.db, 'read_pg', return_value=pd.DataFrame(rows)) as reader:
|
|
result = sources.stock_daily(DAY)
|
|
self.assertEqual(set(result), {'SH688001', 'SZ300001'})
|
|
self.assertIn("UPPER(TRIM(CAST(ts_code AS TEXT))) ~", reader.call_args.args[0])
|
|
|
|
def test_prices_filter_at_query_and_normalize_without_guessing_wrong_exchange(self):
|
|
rows = [{'ts_code': code, 'close': 10} for code in
|
|
('SH600000', '000001.SZ', '300001', '920001', '920001.BJ', '600000.SZ')]
|
|
with patch.object(sources.db, 'read_mysql', return_value=rows) as reader:
|
|
result = sources.close_prices(DAY, code_col='symbol')
|
|
self.assertEqual(set(result), {'SH600000', 'SZ000001', 'SZ300001'})
|
|
self.assertIn('CAST(`symbol` AS CHAR)', reader.call_args.args[1])
|
|
self.assertEqual(reader.call_args.args[2], (DAY, '2026-11-05'))
|
|
|
|
def test_price_column_must_be_a_known_identifier(self):
|
|
with patch.object(sources.db, 'read_mysql') as reader:
|
|
self.assertEqual(sources.close_prices(DAY, code_col='symbol` OR 1=1'), {})
|
|
reader.assert_not_called()
|
|
|
|
def test_breadth_excludes_other_markets_without_filtering_market_indices(self):
|
|
seen = []
|
|
def pg(sql, params):
|
|
self.assertIn('SELECT ts_code, pct_change', sql)
|
|
self.assertIn('CAST(ts_code AS TEXT)', sql)
|
|
return [{'ts_code': '600000.SH', 'pct_change': 3},
|
|
{'ts_code': '000001.SZ', 'pct_change': -1},
|
|
{'ts_code': '920001.BJ', 'pct_change': 20},
|
|
{'ts_code': '000001.SH', 'pct_change': 10}, {'pct_change': 10}]
|
|
def mysql(source, sql, params=None):
|
|
seen.append((sql, params))
|
|
if 'zs_day_data' in sql:
|
|
return [{'symbol': '000001.SH', 'd': DAY, 'amount': 100},
|
|
{'symbol': '399001.SZ', 'd': DAY, 'amount': 200}]
|
|
return []
|
|
result = sources.market_context(DAY, read_pg=pg, read_mysql=mysql)
|
|
self.assertEqual(result['breadth']['n'], 2)
|
|
self.assertEqual(result['breadth']['up'], 1)
|
|
self.assertEqual(result['breadth']['down'], 1)
|
|
self.assertEqual(result['turnover']['amount'], 300)
|
|
self.assertEqual(seen[0][1], ('000001.SH', '399001.SZ'))
|
|
self.assertNotIn('REGEXP', seen[0][0])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|