Skip to content

Commit

Permalink
Fix 'str' object has no attribute 'pop' error when parsing query (#6941)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcowley committed May 2, 2024
1 parent 897c683 commit b7f22b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions redash/query_runner/elasticsearch2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
from typing import Optional, Tuple

Expand Down Expand Up @@ -64,6 +65,7 @@ def run_query(self, query, user):
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)
Expand Down
13 changes: 12 additions & 1 deletion tests/query_runner/test_elasticsearch2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest import TestCase
from unittest import TestCase, mock

from redash.query_runner.elasticsearch2 import (
ElasticSearch2,
Expand Down Expand Up @@ -137,3 +137,14 @@ def test_parse_results(self):
],
}
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"])

0 comments on commit b7f22b1

Please sign in to comment.