77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
|
|
"""本地:python3 -m unittest test_dividend_research_feed;HTTP全部替身。"""
|
|||
|
|
import copy
|
|||
|
|
import json
|
|||
|
|
import unittest
|
|||
|
|
from unittest.mock import patch
|
|||
|
|
|
|||
|
|
import dividend_research_feed as f
|
|||
|
|
import dividend_watch as w
|
|||
|
|
from test_dividend_watch import reviewed
|
|||
|
|
|
|||
|
|
|
|||
|
|
CUTOFF = '2026-10-21T08:00:00+08:00'
|
|||
|
|
|
|||
|
|
|
|||
|
|
def payload():
|
|||
|
|
return {'schema_version': 1, 'cutoff_at': CUTOFF, 'items': {'600001.SH': {
|
|||
|
|
'research': {'ts_code': '600001.SH', 'schema_version': 1, 'kind': 'dividend_sustainability',
|
|||
|
|
'ref': 'a'*64, 'conclusion': '有支持', 'cutoff_at': '2026-10-20T20:00:00+08:00',
|
|||
|
|
'generated_at': '2026-10-20T20:03:00+08:00', 'unknowns': []},
|
|||
|
|
'last_check': {'state': 'success'}}}}
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Response:
|
|||
|
|
def __init__(self, body): self.body = json.dumps(body).encode()
|
|||
|
|
def __enter__(self): return self
|
|||
|
|
def __exit__(self, *args): pass
|
|||
|
|
def read(self, n): return self.body[:n]
|
|||
|
|
|
|||
|
|
|
|||
|
|
class FeedTests(unittest.TestCase):
|
|||
|
|
def collect(self, p):
|
|||
|
|
with patch('urllib.request.urlopen', return_value=Response(p)) as get:
|
|||
|
|
result = f.collect(['600001.SH'], CUTOFF, 'http://base')
|
|||
|
|
self.assertEqual(get.call_count, 1)
|
|||
|
|
self.assertIn('/research/dividends?', get.call_args.args[0])
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
def test_read_only_fixed_research_reference(self):
|
|||
|
|
x = self.collect(payload())['600001.SH']
|
|||
|
|
self.assertEqual(x['status'], 'ok')
|
|||
|
|
self.assertIn('?ref='+'a'*64, x['url'])
|
|||
|
|
|
|||
|
|
def test_unavailable_never_becomes_approved(self):
|
|||
|
|
with patch('urllib.request.urlopen', side_effect=TimeoutError):
|
|||
|
|
x = f.collect(['600001.SH'], CUTOFF, 'http://base')
|
|||
|
|
self.assertEqual(x['600001.SH']['status'], 'unavailable')
|
|||
|
|
|
|||
|
|
def test_future_or_wrong_identity_rejected(self):
|
|||
|
|
for k, v in [('generated_at', '2026-10-22T08:00:00+08:00'), ('ts_code', '000001.SZ'), ('ref', '../file')]:
|
|||
|
|
p = payload(); p['items']['600001.SH']['research'][k] = v
|
|||
|
|
self.assertEqual(self.collect(p)['600001.SH']['status'], 'unavailable')
|
|||
|
|
|
|||
|
|
def test_failed_refresh_does_not_claim_fresh_success(self):
|
|||
|
|
p = payload(); p['items']['600001.SH']['last_check']['state'] = 'failed'
|
|||
|
|
self.assertEqual(self.collect(p)['600001.SH']['status'], 'unavailable')
|
|||
|
|
|
|||
|
|
def test_monthly_due_does_not_claim_current_support(self):
|
|||
|
|
p = payload(); p['items']['600001.SH']['review_due'] = True
|
|||
|
|
row = self.collect(p)['600001.SH']
|
|||
|
|
self.assertEqual(row['status'], 'due'); self.assertEqual(f.display(row), '待月度复核')
|
|||
|
|
|
|||
|
|
def test_missing_fields_old_archive_compatible(self):
|
|||
|
|
self.assertEqual(w.assemble(reviewed())['rows'][0]['sustainability'], '待核验')
|
|||
|
|
|
|||
|
|
def test_research_never_changes_eligibility_or_rank(self):
|
|||
|
|
raw = reviewed(); before = w.assemble(raw)
|
|||
|
|
raw['dividend_research'] = self.collect(payload())
|
|||
|
|
after = w.assemble(raw)
|
|||
|
|
self.assertEqual(before['counts'], after['counts'])
|
|||
|
|
self.assertEqual(before['rows'][0]['status'], after['rows'][0]['status'])
|
|||
|
|
self.assertEqual(after['rows'][0]['sustainability'], '有支持')
|
|||
|
|
self.assertFalse(after['rows'][0]['investment_approved'])
|
|||
|
|
self.assertIn('分红研究:有支持', w.report(after))
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == '__main__': unittest.main()
|