:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(eng-analytics): pin one seed clock per test so day gaps stay exact (#82581)
Thiago Salvatorec479c319351b
diff --git a/products/engineering_analytics/backend/tests/_logic_helpers.py b/products/engineering_analytics/backend/tests/_logic_helpers.pyindex 2a1e1ec1cb03..ed8546bb3abf 100644--- a/products/engineering_analytics/backend/tests/_logic_helpers.py+++ b/products/engineering_analytics/backend/tests/_logic_helpers.py@@ -55,6 +55,25 @@ def _dt(value: str) -> datetime:return datetime.fromisoformat(value).replace(tzinfo=UTC)+_seed_clock: datetime | None = None+++def anchor_seed_clock() -> None:+ """Pin the clock every seed in one test reads from. Called per test by the autouse fixture.++ Seeds format to whole seconds, so two calls that straddle a wall-clock second land a second+ apart even when the test asked for an exact number of days between them. Tests assert on the+ distance between seeded rows (`merged_at` minus a `ready_for_review` event, for one), so+ sampling the clock per call turned that distance into a coin flip on sub-second timing.+ """+ global _seed_clock+ _seed_clock = timezone.now()+++def _seed_now() -> datetime:+ return _seed_clock if _seed_clock is not None else timezone.now()++def _ago(days: int) -> str:return _ago_with_duration(days, 0)[0]@@ -62,7 +81,7 @@ def _ago(days: int) -> str:def _ago_with_duration(days: int, duration_seconds: int) -> tuple[str, str]:# Seed dates relative to real time: HogQL now() runs server-side and ignores# freezegun, so window/age assertions must share the clock the query uses.- started_at = timezone.now() - timedelta(days=days)+ started_at = _seed_now() - timedelta(days=days)updated_at = started_at + timedelta(seconds=duration_seconds)fmt = "%Y-%m-%d %H:%M:%S"return started_at.strftime(fmt), updated_at.strftime(fmt)@@ -70,7 +89,7 @@ def _ago_with_duration(days: int, duration_seconds: int) -> tuple[str, str]:def _ago_offset_with_duration(days: int, offset_seconds: int, duration_seconds: int) -> tuple[str, str]:"""Like ``_ago_with_duration`` but shifted forward, so several runs can share one round anchor."""- started_at = timezone.now() - timedelta(days=days) + timedelta(seconds=offset_seconds)+ started_at = _seed_now() - timedelta(days=days) + timedelta(seconds=offset_seconds)updated_at = started_at + timedelta(seconds=duration_seconds)fmt = "%Y-%m-%d %H:%M:%S"return started_at.strftime(fmt), updated_at.strftime(fmt)diff --git a/products/engineering_analytics/backend/tests/conftest.py b/products/engineering_analytics/backend/tests/conftest.pyindex bbd7fc327e44..ee6ef77012e4 100644--- a/products/engineering_analytics/backend/tests/conftest.py+++ b/products/engineering_analytics/backend/tests/conftest.py@@ -3,6 +3,8 @@ from unittest.mock import patchfrom posthog.models.scoping import team_scope+from products.engineering_analytics.backend.tests._logic_helpers import anchor_seed_clock+def _only_engineering_analytics_flag(flag_key: str, *args: object, **kwargs: object) -> bool:# Enable just the engineering-analytics rollout flag; leave every other flag at its@@ -17,6 +19,11 @@ def _enable_engineering_analytics_flag():yield[email protected](autouse=True)+def _anchor_seed_clock():+ anchor_seed_clock()++@pytest.fixture(autouse=True)def _set_team_scope(request):if request.node.get_closest_marker("django_db") is None: