diff --git a/src/sentry/discover/compare_tables.py b/src/sentry/discover/compare_tables.py index 3511b95b54563d..4081fd422077a4 100644 --- a/src/sentry/discover/compare_tables.py +++ b/src/sentry/discover/compare_tables.py @@ -4,6 +4,7 @@ import sentry_sdk +from sentry.constants import ObjectStatus from sentry.discover.arithmetic import is_equation from sentry.discover.dataset_split import _get_equation_list, _get_field_list from sentry.discover.translation.mep_to_eap import ( @@ -97,7 +98,11 @@ def compare_tables_for_dashboard_widget_queries( widget: DashboardWidget = widget_query.widget dashboard: Dashboard = widget.dashboard organization: Organization = dashboard.organization - projects: list[Project] = list(dashboard.projects.all()) + # if the dashboard has no projects, we will use all projects in the organization + projects = dashboard.projects.all() or Project.objects.filter( + organization_id=dashboard.organization.id, status=ObjectStatus.ACTIVE + ) + if len(list(projects)) == 0: return { "passed": False, diff --git a/tests/sentry/discover/test_compare_tables.py b/tests/sentry/discover/test_compare_tables.py index 12f20b54b7197c..9a42888e7c1425 100644 --- a/tests/sentry/discover/test_compare_tables.py +++ b/tests/sentry/discover/test_compare_tables.py @@ -32,7 +32,29 @@ def setUp(self): self.dashboard = self.create_dashboard( organization=self.organization, filters={"environment": []} ) + self.dashboard_2 = self.create_dashboard( + organization=self.organization, filters={"environment": []} + ) self.dashboard.projects.set([self.project]) + self.dashboard_2.projects.set([]) + + self.successful_widget_2 = DashboardWidget.objects.create( + dashboard=self.dashboard_2, + title="Test Successful Widget 2", + order=0, + display_type=DashboardWidgetDisplayTypes.TABLE, + widget_type=DashboardWidgetTypes.TRANSACTION_LIKE, + ) + + self.successful_widget_query_2 = DashboardWidgetQuery.objects.create( + widget=self.successful_widget_2, + name="Test Successful Widget Query 2", + order=0, + conditions="", + aggregates=["count()"], + columns=["count()", "transaction"], + fields=["count()", "transaction"], + ) self.successful_widget = DashboardWidget.objects.create( dashboard=self.dashboard, @@ -299,3 +321,10 @@ def test_compare_non_existent_eap_widget_query(self): assert comparison_result["passed"] is False assert comparison_result["reason"] == CompareTableResult.QUERY_FAILED assert comparison_result["mismatches"] is not None and [] == comparison_result["mismatches"] + + def test_compare_widget_query_with_no_project(self): + comparison_result = compare_tables_for_dashboard_widget_queries( + self.successful_widget_query_2 + ) + assert comparison_result["passed"] is True + assert comparison_result["mismatches"] == []