From 702af3e9420cef7214b8c10a97df1b9fef3ffcc5 Mon Sep 17 00:00:00 2001 From: Yeger Date: Sun, 27 Oct 2024 14:38:16 +0200 Subject: [PATCH 1/2] don't crash when there is no data --- redash/models/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redash/models/__init__.py b/redash/models/__init__.py index ce3446c916..bde67722ad 100644 --- a/redash/models/__init__.py +++ b/redash/models/__init__.py @@ -959,9 +959,9 @@ def get_by_id_and_org(cls, object_id, org): return super(Alert, cls).get_by_id_and_org(object_id, org, Query) def evaluate(self): - data = self.query_rel.latest_query_data.data + data = self.query_rel.latest_query_data.data if self.query_rel.latest_query_data else None - if data["rows"] and self.options["column"] in data["rows"][0]: + if data and data["rows"] and self.options["column"] in data["rows"][0]: op = OPERATORS.get(self.options["op"], lambda v, t: False) if "selector" not in self.options: From 2d6e20bb7c670b780d3ce149321aa1cfd7ff80c5 Mon Sep 17 00:00:00 2001 From: Yeger Date: Tue, 29 Oct 2024 19:31:12 +0200 Subject: [PATCH 2/2] Add test --- tests/models/test_alerts.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/models/test_alerts.py b/tests/models/test_alerts.py index 3bcfff0108..6c614db096 100644 --- a/tests/models/test_alerts.py +++ b/tests/models/test_alerts.py @@ -111,6 +111,13 @@ def test_evaluates_correctly_with_max_selector(self): alert.options["selector"] = "max" self.assertEqual(alert.evaluate(), Alert.UNKNOWN_STATE) + def test_evaluate_alerts_without_query_rel(self): + query = self.factory.create_query(latest_query_data_id=None) + alert = self.factory.create_alert( + query_rel=query, options={"selector": "first", "op": "equals", "column": "foo", "value": "1"} + ) + self.assertEqual(alert.evaluate(), Alert.UNKNOWN_STATE) + class TestNextState(TestCase): def test_numeric_value(self):