Fix 'str' object has no attribute 'pop' error when parsing query (#6941)

This commit is contained in:
Jason Cowley
2024-05-02 04:31:23 -07:00
committed by GitHub
parent 897c683980
commit b7f22b1896
2 changed files with 14 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
import json
import logging
from typing import Optional, Tuple
@@ -64,6 +65,7 @@ class ElasticSearch2(BaseHTTPQueryRunner):
return data, error
def _build_query(self, query: str) -> Tuple[dict, str, Optional[list]]:
query = json.loads(query)
index_name = query.pop("index", "")
result_fields = query.pop("result_fields", None)
url = "/{}/_search".format(index_name)

View File

@@ -1,4 +1,4 @@
from unittest import TestCase
from unittest import TestCase, mock
from redash.query_runner.elasticsearch2 import (
ElasticSearch2,
@@ -137,3 +137,14 @@ class TestXPackSQL(TestCase):
],
}
self.assertDictEqual(XPackSQLElasticSearch._parse_results(None, response), expected)
class TestElasticSearch2(TestCase):
@mock.patch("redash.query_runner.elasticsearch2.ElasticSearch2.__init__", return_value=None)
def test_build_query(self, mock_init):
query_runner = ElasticSearch2()
query_str = '{"index": "test_index", "result_fields": ["field1", "field2"]}'
query_dict, url, result_fields = query_runner._build_query(query_str)
self.assertEqual(query_dict, {})
self.assertEqual(url, "/test_index/_search")
self.assertEqual(result_fields, ["field1", "field2"])