fix: fall back to latest SGE trading price
ci / gitleaks (push) Successful in 8s
ci / docker (push) Successful in 3m14s

This commit is contained in:
2026-09-20 08:33:55 +08:00
parent 8ecc3e265f
commit ed123b710f
2 changed files with 110 additions and 3 deletions
+55
View File
@@ -56,6 +56,61 @@ def test_parse_sge_rejects_changed_structure_or_invalid_quote(html):
environment.parse_sge_au9999(html)
@pytest.mark.asyncio
async def test_fetch_gold_falls_back_to_latest_trading_day_when_delayed_quote_is_zero():
delayed_html = SGE_HTML.replace("2026年09月16日", "2026年09月20日").replace(
"935.99", "0.0"
)
history_html = """
<table>
<tr><th>序号</th><th>日期</th><th>合约</th><th>开盘价</th><th>收盘价</th></tr>
<tr><td>1</td><td>2026-09-17</td><td>Au99.99</td><td>937.50</td><td>934.81</td></tr>
<tr><td>2</td><td>2026-09-18</td><td>Au99.99</td><td>937.00</td><td>947.09</td></tr>
<tr><td>3</td><td>2026-09-16</td><td>Au99.99</td><td>-</td><td>-</td></tr>
</table>
"""
seen = []
async def request(url, **_kwargs):
seen.append(url)
return delayed_html if url == environment.GOLD_URL else history_html
result = await environment.fetch_gold(request)
assert seen == [
environment.GOLD_URL,
environment.GOLD_HISTORY_URL.format(start_date="2026-08-20", end_date="2026-09-20"),
]
assert result["latest_price"] == Decimal("947.09")
assert result["market_date"] == "2026-09-18"
@pytest.mark.asyncio
async def test_gold_source_has_time_for_two_sequential_upstream_requests(monkeypatch):
environment.clear_cache()
monkeypatch.setattr(environment, "SOURCE_TIMEOUT_SECONDS", 0.01)
monkeypatch.setattr(environment, "GOLD_SOURCE_TIMEOUT_SECONDS", 0.05, raising=False)
monkeypatch.setattr(environment, "TOTAL_TIMEOUT_SECONDS", 0.06)
async def weather():
return {"temperature_c": 25, "source": "Open-Meteo"}
async def gold():
await asyncio.sleep(0.03)
return {
"contract": "Au99.99",
"latest_price": Decimal("947.09"),
"market_date": "2026-09-18",
"delayed": True,
"source": "上海黄金交易所",
}
result = await environment.get_environment(weather_fetcher=weather, gold_fetcher=gold)
assert result["gold"]["latest_price"] == "947.09"
assert result["errors"] == {}
@pytest.mark.asyncio
async def test_fetch_weather_uses_fixed_haishu_location_and_current_conditions():
seen = {}