Skip to content

Commit 21c9d4e

Browse files
test(admin): make analytics-overview signup assertion relative to now
routes/admin.py builds signups_by_day over a rolling 30-day window, but the test hardcoded 2026-05-01 signup dates, so they fell out of the window as time passed and the test started failing. Seed the signups relative to now (3/2 days back, with margin from the window edges and the UTC midnight boundary) and assert against the computed date so the test no longer rots.
1 parent 2342127 commit 21c9d4e

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

backend/tests/test_admin_routes.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
1414
All DB access and admin auth are mocked.
1515
"""
16+
from datetime import datetime, timedelta, timezone
17+
1618
import pytest
1719
from unittest.mock import MagicMock, patch
1820
from fastapi.testclient import TestClient
@@ -611,10 +613,19 @@ def test_caps_page_size(self):
611613

612614
class TestAnalyticsOverview:
613615
def test_returns_totals_and_series(self):
616+
# Seed signup dates relative to "now" so they always land inside the
617+
# route's rolling 30-day window (admin.py builds signups_by_day for the
618+
# last 30 days). A previously hardcoded 2026-05-01 fell out of that
619+
# window as time passed, rotting this test. The 3-/2-day offsets keep
620+
# margin from both window edges and the UTC midnight boundary.
621+
signup_day = (datetime.now(timezone.utc) - timedelta(days=3)).date()
622+
pending_day = (datetime.now(timezone.utc) - timedelta(days=2)).date()
623+
signup_iso = f"{signup_day.isoformat()}T00:00:00Z"
624+
pending_iso = f"{pending_day.isoformat()}T00:00:00Z"
614625
users = [
615-
{"id": "u1", "is_approved": True, "created_at": "2026-05-01T00:00:00Z"},
616-
{"id": "u2", "is_approved": True, "created_at": "2026-05-01T00:00:00Z"},
617-
{"id": "u3", "is_approved": False, "created_at": "2026-05-02T00:00:00Z"},
626+
{"id": "u1", "is_approved": True, "created_at": signup_iso},
627+
{"id": "u2", "is_approved": True, "created_at": signup_iso},
628+
{"id": "u3", "is_approved": False, "created_at": pending_iso},
618629
]
619630
roles = [{"id": "rA", "slug": "admin", "name": "Admin", "color": "#dc2626"}]
620631
user_roles = [{"role_id": "rA"}, {"role_id": "rA"}, {"role_id": "rA"}]
@@ -640,6 +651,9 @@ def by_name(name):
640651
assert body["totals"]["approved"] == 2
641652
assert body["totals"]["pending"] == 1
642653
assert body["totals"]["admins"] == 3
643-
assert any(d["date"] == "2026-05-01" and d["count"] == 2 for d in body["signups_by_day"])
654+
assert any(
655+
d["date"] == signup_day.isoformat() and d["count"] == 2
656+
for d in body["signups_by_day"]
657+
)
644658
assert body["role_counts"][0]["slug"] == "admin"
645659
assert body["role_counts"][0]["count"] == 3

0 commit comments

Comments
 (0)