This commit is contained in:
2026-11-04 15:52:50 +08:00
parent 62291c57ec
commit cc66e98f61
6 changed files with 41 additions and 26 deletions
+2 -2
View File
@@ -25,8 +25,8 @@ def test_refresh_slot_advances_five_minutes_before_boundary(hour, minute, second
def test_response_types_round_price_values_and_keep_timestamp_types():
realtime = RealTimeGold(updataTime=stamp(1, 30), region='跨1', code='k1', goldPrice=.016789)
assert realtime.goldPrice == .02
realtime = RealTimeGold(updataTime=stamp(1, 30), region='跨1', code='k1', goldPrice=105.26315789)
assert realtime.goldPrice == 105.26
assert realtime.updataTime == stamp(1, 30)
assert isinstance(realtime.updataTime, int)
official = OfficialGold(updataTime=str(stamp(1, 30)), region='跨1', code='k1',
+21 -9
View File
@@ -18,7 +18,7 @@ def offer(price, url='listing', platform=17, money=10):
def cached_item(region='跨1', age=0):
fetched_at = time.time() - age
return {'region': region, 'goldPrice': .01, 'sampleCount': 7,
return {'region': region, 'goldPrice': 100.0, 'sampleCount': 7,
'code': GOLD_REGION_CODES[region], 'updataTime': refresh_slot(fetched_at),
'fetchedAt': fetched_at}
@@ -54,16 +54,16 @@ class FakeRedis:
return 0
def test_average_uses_displayed_unit_prices_at_indices_three_through_nine():
def test_average_uses_gold_per_yuan_ratios_at_indices_three_through_nine():
rows = [offer(i / 100) for i in range(1, 12)]
assert gold.first_page_average(rows) == (.07, 7)
assert gold.first_page_average(rows) == (15.65, 7)
invalid = [offer(0), offer(-1), offer(float('nan')), offer(float('inf')),
offer(.01, money=-1), offer(None), offer(.01, money=float('nan'))]
# 页面允许Money=-1的库存占位报价;另行排除不显示的低额订单。
invalid.pop(4)
assert gold.first_page_average(invalid + [offer(.01, money=.5)] + rows) == (.07, 7)
assert gold.first_page_average([offer(.016789)] * 10) == (.02, 7)
assert gold.first_page_average(rows[:4]) == (.04, 1)
assert gold.first_page_average(invalid + [offer(.01, money=.5)] + rows) == (15.65, 7)
assert gold.first_page_average([offer(.016789)] * 10) == (round(1 / .016789, 2), 7)
assert gold.first_page_average(rows[:4]) == (25.0, 1)
with pytest.raises(ValueError, match='有效报价'):
gold.first_page_average(invalid)
with pytest.raises(ValueError, match='有效报价'):
@@ -73,9 +73,15 @@ def test_average_uses_displayed_unit_prices_at_indices_three_through_nine():
def test_page_deduplicates_before_cutoff_and_preserves_7881_variants():
rows = [offer(.01, 'duplicate', 40), offer(.01, 'duplicate', 40)]
rows += [offer(i / 100, str(i), 17) for i in range(2, 12)]
assert gold.first_page_average(rows) == (.07, 7)
assert gold.first_page_average(rows) == (15.65, 7)
rows = [offer(.01, platform=44)] * 3 + [offer(.04, platform=44)] * 7
assert gold.first_page_average(rows) == (.04, 7)
assert gold.first_page_average(rows) == (25.0, 7)
def test_each_quote_is_inverted_before_averaging_without_early_rounding():
rows = [offer(.008)] * 3 + [offer(.0095)] * 4 + [offer(.01)] * 3
assert gold.first_page_average(rows) == (103.01, 7)
assert round(1 / ((.0095 * 4 + .01 * 3) / 7), 2) == 102.94
def test_request_signature_matches_observed_browser_request():
@@ -135,7 +141,7 @@ def test_region_fetches_its_own_seed_config_and_all_initial_channels():
assert result['region'] == '跨8'
assert result['code'] == 'k8'
assert isinstance(result['updataTime'], int)
assert result['goldPrice'] == .01
assert result['goldPrice'] == 100.0
assert result['sampleCount'] == 7
assert result['updataTime'] == refresh_slot(started_at)
assert result['fetchedAt'] == started_at
@@ -184,6 +190,12 @@ def test_invalid_cached_average_is_not_served():
assert asyncio.run(gold.read_cache(redis)) == []
def test_old_yuan_per_gold_cache_cannot_be_returned_as_new_ratio():
redis = FakeRedis()
redis.values['openapi:colg:thirdpart_gold:v2:kua1'] = json.dumps({**cached_item(), 'goldPrice': .01})
assert asyncio.run(gold.read_cache(redis)) == []
def test_legacy_cache_gets_epoch_and_region_code_without_refetching():
redis = FakeRedis()
legacy = cached_item('跨3A')