Files
syops/backend/tests/test_schedule_timezone.py
T

34 lines
1.4 KiB
Python

"""排程 Cron 业务时区计算测试。"""
from datetime import datetime
from unittest.mock import patch
import pytest
from app.services.schedule.timezone_util import compute_next_run_at
def test_compute_next_run_at_uses_shanghai_timezone(app):
"""0 12,17 * * * 在 01:00 北京时间后,下次应为当日 12:00(非 UTC 12:00)。"""
with app.app_context():
# 2026-06-12 01:00:15 CST = 2026-06-11 17:00:15 UTC
base_utc = datetime(2026, 6, 11, 17, 0, 15)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 4, 0, 0)
def test_compute_next_run_at_after_noon(app):
with app.app_context():
# 2026-06-12 12:30 CST = 2026-06-12 04:30 UTC
base_utc = datetime(2026, 6, 12, 4, 30, 0)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 9, 0, 0)
def test_compute_next_run_at_without_tzdata(app):
"""缺少 tzdata 时仍应通过固定 UTC+8 回退计算下次执行。"""
with app.app_context():
with patch('app.services.schedule.timezone_util.ZoneInfo', side_effect=Exception('no tzdata')):
base_utc = datetime(2026, 6, 11, 17, 0, 15)
nxt = compute_next_run_at('0 12,17 * * *', base_utc_naive=base_utc)
assert nxt == datetime(2026, 6, 12, 4, 0, 0)