fix: 让依赖当前日期的测试使用固定时间
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
@@ -577,7 +578,20 @@ def test_user_can_set_monthly_study_start() -> None:
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_user_can_start_and_stop_study_immediately() -> None:
|
||||
def _freeze_beijing(monkeypatch, year: int, month: int, day: int) -> None:
|
||||
from euniv.study_window import CN_TZ, as_cn
|
||||
|
||||
moment = datetime(year, month, day, 12, 0, tzinfo=CN_TZ)
|
||||
|
||||
def frozen(now=None):
|
||||
if now is not None:
|
||||
return as_cn(now)
|
||||
return moment
|
||||
|
||||
monkeypatch.setattr("euniv.study_window.as_cn", frozen)
|
||||
|
||||
|
||||
def test_user_can_start_and_stop_study_immediately(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
|
||||
@@ -585,6 +599,7 @@ def test_user_can_start_and_stop_study_immediately() -> None:
|
||||
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
|
||||
)
|
||||
client = TestClient(api_module.app)
|
||||
_freeze_beijing(monkeypatch, 2026, 11, 15)
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
client.put("/api/settings/study-start", json={"day": 31, "hour": 23, "minute": 59, "second": 59})
|
||||
idle = client.get("/api/settings/study-control")
|
||||
@@ -629,7 +644,7 @@ def test_study_control_stays_stopped_when_month_hours_met() -> None:
|
||||
assert started.json()["paused"] is False
|
||||
|
||||
|
||||
def test_study_control_websocket_sends_snapshot() -> None:
|
||||
def test_study_control_websocket_sends_snapshot(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
from starlette.websockets import WebSocketDisconnect
|
||||
@@ -638,6 +653,7 @@ def test_study_control_websocket_sends_snapshot() -> None:
|
||||
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
|
||||
)
|
||||
client = TestClient(api_module.app)
|
||||
_freeze_beijing(monkeypatch, 2026, 11, 15)
|
||||
with pytest.raises(WebSocketDisconnect) as closed:
|
||||
with client.websocket_connect("/api/settings/study-control/ws") as ws:
|
||||
ws.receive_json()
|
||||
@@ -691,7 +707,7 @@ def _set_hours_source(value: str) -> None:
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_home_and_history_hours_follow_source() -> None:
|
||||
def test_home_and_history_hours_follow_source(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
import euniv.service as service_module
|
||||
|
||||
@@ -704,6 +720,14 @@ def test_home_and_history_hours_follow_source() -> None:
|
||||
api_module.fetch_study_history = lambda db, employee_id: service_module.fetch_study_history(
|
||||
db, employee_id, FakeClient
|
||||
)
|
||||
from euniv.client import history_hour_totals
|
||||
from euniv.study_window import CN_TZ
|
||||
|
||||
frozen = datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ)
|
||||
monkeypatch.setattr(
|
||||
"euniv.service.history_hour_totals",
|
||||
lambda items, now=None: history_hour_totals(items, frozen),
|
||||
)
|
||||
client = TestClient(api_module.app)
|
||||
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user