Fix python query runner crashing the GUI due to missing 'rows' and/or… (#5749)

* Fix python query runner crashing the GUI due to missing 'rows' and/or 'columns' in the JSON returned data.

* Fix typo of previous commit.

* Throw exception when python query runner has invalid result.

* Update test_python.py
---------

Co-authored-by: YuhengChen <yuheng.chen@imaygou.com>
This commit is contained in:
Harry C
2023-08-08 00:03:12 +08:00
committed by GitHub
parent f4a930ddeb
commit d333660473
2 changed files with 50 additions and 5 deletions

View File

@@ -20,19 +20,44 @@ class TestPythonQueryRunner(TestCase):
def test_empty_result(self):
query_string = "result={}"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], '{"log": []}')
self.assertEqual(result[0], None)
def test_invalidate_result_type_string(self):
def test_none_result(self):
query_string = "result=None"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_invalid_result_type_string(self):
query_string = "result='string'"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_invalidate_result_type_int(self):
def test_invalid_result_type_int(self):
query_string = "result=100"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_validate_result_type(self):
def test_invalid_result_missing_rows(self):
query_string = "result={'columns': []}"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_invalid_result_not_list_rows(self):
query_string = "result={'rows': {}, 'columns': []}"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_invalid_result_missing_columns(self):
query_string = "result={'rows': []}"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_invalid_result_not_list_columns(self):
query_string = "result={'rows': [], 'columns': {}}"
result = self.python.run_query(query_string, "user")
self.assertEqual(result[0], None)
def test_valid_result_type(self):
query_string = (
"result="
'{"columns": [{"name": "col1", "type": TYPE_STRING},'
@@ -51,7 +76,7 @@ class TestPythonQueryRunner(TestCase):
)
@mock.patch("datetime.datetime")
def test_validate_result_type_with_print(self, mock_dt):
def test_valid_result_type_with_print(self, mock_dt):
mock_dt.utcnow = mock.Mock(return_value=datetime(1901, 12, 21))
query_string = (
'print("test")\n'