test(server): v1/v2混合停留聚合回归+聚合点核查适配

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-18 12:12:03 +08:00
parent 5e024b814b
commit 0cb8c6fb0c
3 changed files with 70 additions and 1 deletions
+27
View File
@@ -4,6 +4,8 @@ from django.contrib.auth import get_user_model
from django.utils import timezone
from rest_framework.test import APIClient
from tracker.models import DailyStat, Event, Site, SourceClassification, VisitSession, Visitor
from tracker.services.ingest import ingest_batch
from tracker.tests.test_ingest import SITE_ID, _batch, _ev, _v2_batch, _v2_event
pytestmark = pytest.mark.django_db
User = get_user_model()
@@ -263,3 +265,28 @@ def test_overview_site_filter_and_404(client, member_user):
assert r.status_code == 404
body = r.json()
assert body["code"] == 404 and body["data"] is None and body["message"] == "site_not_found"
def test_overview_avg_stay_mixed_v1_v2_session(client, member_user):
"""v1+v2 混合会话回归(阶段A T4):avg_stay_ms 读 VisitSession.stay_ms 重算值。
经真实 ingest 落库——v1 page_stay active_ms=8000(加法)+ v2 同 page_id 相邻区间
[0,15000][15000,30000](并集=30000,非加法 60000)→ 会话 stay_ms=38000;
单会话 avg=38000/1。聚合点不直接 SUM(Event.properties.active_ms),
T3 重算式回写后读数侧自动正确——本用例钉死该语义不被回归。"""
_auth(client, member_user)
site = Site.objects.create(name="s", domains=["a.com"], site_id=SITE_ID)
ingest_batch(site, _batch( # v1 批:建会话 sm + v1 停留增量
[_ev(1), _ev(2, "page_stay", properties={"active_ms": 8000, "path": "/p"})],
sid="sm"))
r = ingest_batch(site, _v2_batch([ # v2 批:同会话同 page_id 两段相邻区间
_v2_event("page_stay", "p1", "sm", seq=1,
active_from=0, active_to=15000, flush_reason="periodic"),
_v2_event("page_stay", "p1", "sm", seq=2,
active_from=15000, active_to=30000, flush_reason="periodic")],
sid="sm"))
assert (r["accepted"], r["rejected"]) == (2, [])
assert VisitSession.objects.get(session_id="sm").stay_ms == 38000 # 写侧:v1 SUM + v2 并集
d = client.get("/api/admin/dashboard/overview", {"range": "today"}).json()["data"]
assert d["cards"]["visits"]["value"] == 1
assert d["cards"]["avg_stay_ms"] == {"value": 38000, "delta_pct": None} # 38000/1;上期无会话 → None