Files
dcalc/backend/tests/unit/test_gold_time.py
T
2026-11-04 15:35:15 +08:00

39 lines
1.6 KiB
Python

from datetime import datetime, timedelta, timezone
import pytest
from api.types.colg import OfficialGold, RealTimeGold
from api.utils.gold_time import half_hour_start, refresh_slot
CHINA = timezone(timedelta(hours=8))
def stamp(hour, minute, second=0):
return int(datetime(2026, 11, 4, hour, minute, second, tzinfo=CHINA).timestamp())
@pytest.mark.parametrize(('hour', 'minute', 'second', 'target_hour', 'target_minute'), [
(1, 24, 59, 1, 0), (1, 25, 0, 1, 30), (1, 29, 59, 1, 30),
(1, 30, 0, 1, 30), (1, 54, 59, 1, 30), (1, 55, 0, 2, 0), (2, 0, 0, 2, 0),
(23, 55, 0, 0, 0),
])
def test_refresh_slot_advances_five_minutes_before_boundary(hour, minute, second, target_hour, target_minute):
expected = stamp(target_hour, target_minute)
if hour == 23:
expected += 24 * 60 * 60
assert refresh_slot(stamp(hour, minute, second)) == expected
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
assert realtime.updataTime == stamp(1, 30)
assert isinstance(realtime.updataTime, int)
official = OfficialGold(updataTime=str(stamp(1, 30)), region='跨1', code='k1',
goldDealRank=[125.556], goldTopDealRank=[250.123], max=79.64444, min=39.9876)
assert official.goldDealRank == [125.56]
assert official.goldTopDealRank == [250.12]
assert official.max == 79.64 and official.min == 39.99
assert official.updataTime == str(stamp(1, 30))
assert half_hour_start(stamp(1, 29, 59)) == stamp(1, 0)