: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
chore(dashboards): make dashboard query snapshots independent of test order (#118739)
Phil Haack8a8076f9c483
diff --git a/posthog/api/test/dashboards/test_dashboard.py b/posthog/api/test/dashboards/test_dashboard.pyindex ae1a2ac50751..eb65191d9eb9 100644--- a/posthog/api/test/dashboards/test_dashboard.py+++ b/posthog/api/test/dashboards/test_dashboard.py@@ -3,7 +3,13 @@ import datetimefrom collections.abc import Callableimport time_machine-from posthog.test.base import APIBaseTest, FuzzyInt, QueryMatchingTest, snapshot_postgres_queries+from posthog.test.base import (+ APIBaseTest,+ FuzzyInt,+ QueryMatchingTest,+ snapshot_postgres_queries,+ snapshot_postgres_queries_context,+)from unittest import mockfrom unittest.mock import ANY, MagicMock, patch@@ -39,6 +45,7 @@ from posthog.models.project import Projectfrom posthog.models.quick_filter import QuickFilterfrom posthog.models.sharing_configuration import SharingConfigurationfrom posthog.models.signals import mute_selected_signals+from posthog.models.tagged_item_registry import content_type_forfrom posthog.test.db_context_capturing import capture_db_queriesfrom posthog.test.insight_queries import browser_filtered_pageview_query, default_pageview_query, insight_queryfrom posthog.test.test_utils import create_group_type_mapping_without_created_at@@ -102,18 +109,32 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):self.organization.save()self.dashboard_api = DashboardAPI(self.client, self.team, self.assertEqual)- @snapshot_postgres_queries- def test_retrieve_dashboard_list(self):- dashboard_names = ["a dashboard", "b dashboard"]- for dashboard_name in dashboard_names:- self.dashboard_api.create_dashboard({"name": dashboard_name})+ def _fill_content_type_cache(self) -> None:+ # Django caches ContentType lookups for the whole process.+ content_type_for(Dashboard)+ content_type_for(Insight)- with self.assertNumQueries(13):- response_data = self.dashboard_api.list_dashboards()- self.assertEqual(- [dashboard["name"] for dashboard in response_data["results"]],- dashboard_names,- )+ def _fill_team_config_cache(self) -> None:+ # The query runner reads these accessors. Each one caches its result in an lru_cache that outlives the test.+ # The cache key is the Team. Team instances with the same pk compare equal.+ # A read through self.team therefore also fills the cache for the request's Team.+ _ = self.team.revenue_analytics_config+ _ = self.team.marketing_analytics_config+ _ = self.team.customer_analytics_config++ def test_retrieve_dashboard_list(self):+ self._fill_content_type_cache()+ with snapshot_postgres_queries_context(self):+ dashboard_names = ["a dashboard", "b dashboard"]+ for dashboard_name in dashboard_names:+ self.dashboard_api.create_dashboard({"name": dashboard_name})++ with self.assertNumQueries(13):+ response_data = self.dashboard_api.list_dashboards()+ self.assertEqual(+ [dashboard["name"] for dashboard in response_data["results"]],+ dashboard_names,+ )@patch("products.dashboards.backend.api.dashboard.report_user_action")def test_non_web_retrieve_fires_dashboard_read_event(self, mock_report_user_action: mock.Mock) -> None:@@ -657,28 +678,29 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):# Set equality asserts the match is exact — every other dashboard (nested, other folder, root/named) is excludedassert result_ids == {ids[expected_key]}- @snapshot_postgres_queriesdef test_retrieve_dashboard(self):- dashboard = Dashboard.objects.create(team=self.team, name="private dashboard", created_by=self.user)+ self._fill_content_type_cache()+ with snapshot_postgres_queries_context(self):+ dashboard = Dashboard.objects.create(team=self.team, name="private dashboard", created_by=self.user)- with patch("products.dashboards.backend.access.record_dashboard_access") as mock_record_access:- response_data = self.dashboard_api.get_dashboard(dashboard.pk)+ with patch("products.dashboards.backend.access.record_dashboard_access") as mock_record_access:+ response_data = self.dashboard_api.get_dashboard(dashboard.pk)- mock_record_access.assert_called_once_with(DashboardAccessMethod.HUMAN)+ mock_record_access.assert_called_once_with(DashboardAccessMethod.HUMAN)- self.assertEqual(response_data["name"], "private dashboard")- self.assertEqual(response_data["description"], "")- self.assertEqual(response_data["created_by"]["distinct_id"], self.user.distinct_id)- self.assertEqual(response_data["created_by"]["first_name"], self.user.first_name)- self.assertEqual(response_data["creation_mode"], "default")- self.assertEqual(- response_data["restriction_level"],- Dashboard.RestrictionLevel.EVERYONE_IN_PROJECT_CAN_EDIT,- )- self.assertEqual(- response_data["effective_privilege_level"],- Dashboard.RestrictionLevel.ONLY_COLLABORATORS_CAN_EDIT,- )+ self.assertEqual(response_data["name"], "private dashboard")+ self.assertEqual(response_data["description"], "")+ self.assertEqual(response_data["created_by"]["distinct_id"], self.user.distinct_id)+ self.assertEqual(response_data["created_by"]["first_name"], self.user.first_name)+ self.assertEqual(response_data["creation_mode"], "default")+ self.assertEqual(+ response_data["restriction_level"],+ Dashboard.RestrictionLevel.EVERYONE_IN_PROJECT_CAN_EDIT,+ )+ self.assertEqual(+ response_data["effective_privilege_level"],+ Dashboard.RestrictionLevel.ONLY_COLLABORATORS_CAN_EDIT,+ )def test_create_basic_dashboard(self):# the front end sends an empty description even if not allowed to add one@@ -1196,7 +1218,6 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):assert [r.get("items", None) for r in response["results"]] == [None, None]assert [r.get("tiles", None) for r in response["results"]] == [None, None]- @snapshot_postgres_queriesdef test_loading_individual_dashboard_does_not_prefetch_all_possible_tiles(self) -> None:"""this test only exists for the query snapshot@@ -1207,15 +1228,18 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):AND "posthog_dashboardtile"."dashboard_id" IN (1,```"""- dashboard_one_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard-1"})- dashboard_two_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard-2"})- self.dashboard_api.create_insight({"dashboards": [dashboard_two_id, dashboard_one_id], "name": f"insight"})- self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})- self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})- self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})-- # so DB has 5 tiles, but we only load need to 1- self.dashboard_api.get_dashboard(dashboard_one_id)+ self._fill_content_type_cache()+ self._fill_team_config_cache()+ with snapshot_postgres_queries_context(self):+ dashboard_one_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard-1"})+ dashboard_two_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard-2"})+ self.dashboard_api.create_insight({"dashboards": [dashboard_two_id, dashboard_one_id], "name": f"insight"})+ self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})+ self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})+ self.dashboard_api.create_insight({"dashboards": [dashboard_one_id], "name": f"insight"})++ # so DB has 5 tiles, but we only load need to 1+ self.dashboard_api.get_dashboard(dashboard_one_id)def test_no_cache_available(self):dashboard = Dashboard.objects.create(team=self.team, name="dashboard")