This commit is contained in:
2026-11-04 16:21:18 +08:00
parent cc66e98f61
commit c2775b732e
3 changed files with 28 additions and 9 deletions
+21 -5
View File
@@ -318,7 +318,7 @@ def test_realtime_uses_thirdpart_cache_and_returns_only_defined_fields(monkeypat
assert isinstance(row['goldPrice'], (int, float))
def test_realtime_initialization_still_returns_contract_fields(monkeypatch, client):
def test_realtime_initialization_returns_empty_array_instead_of_partial_regions(monkeypatch, client):
async def read(redis):
return [{**cached_item('跨2'), 'stale': False}]
@@ -329,9 +329,8 @@ def test_realtime_initialization_still_returns_contract_fields(monkeypatch, clie
monkeypatch.setattr(gold, 'schedule_refresh', schedule)
body = client.get('/api/open/gold/realtime/').json()
assert body['code'] == 202
assert len(body['data']) == 1
assert set(body['data'][0]) == {'updataTime', 'region', 'code', 'goldPrice'}
assert body['data'][0]['code'] == 'k2'
assert body['data'] == []
assert '1/9' in body['message']
def test_route_cold_start_and_redis_failure_do_not_block_or_scrape(monkeypatch):
@@ -363,7 +362,24 @@ def test_route_partial_cache_reports_initialization_progress(monkeypatch):
result = asyncio.run(colg.get_thirdpart_gold(None, BackgroundTasks()))
assert result.code == 202
assert '1/9' in result.message
assert len(result.data) == 1
assert result.data == []
@pytest.mark.parametrize(('count', 'expected_code'), [(1, 503), (9, 200)])
def test_refresh_schedule_failure_serves_only_complete_cache(monkeypatch, count, expected_code):
data = [{**cached_item(name), 'stale': False} for name in list(gold.REGIONS.values())[:count]]
async def read(redis):
return data
async def schedule(*args):
raise ConnectionError('Redis unavailable')
monkeypatch.setattr(gold, 'read_cache', read)
monkeypatch.setattr(gold, 'schedule_refresh', schedule)
result = asyncio.run(colg.get_thirdpart_gold(None, BackgroundTasks()))
assert result.code == expected_code
assert result.data == (data if count == 9 else [])
def test_background_launcher_finishes_before_refresh_completes(monkeypatch):