Handle error when data_source is None (#4936)

* add try except for data_source

* add try except for data_source

* fix errir handle

* add test for no data source

* changed error handle

* fix format

* fix test

---------

Co-authored-by: Konstantin Smirnov <46676677+konnectr@users.noreply.github.com>
Co-authored-by: konnectr <1konnectrl@gmail.com>
This commit is contained in:
Fernando Salcido
2023-07-22 07:17:49 -07:00
committed by GitHub
parent b30622e531
commit 79ef3e4eb0
2 changed files with 12 additions and 1 deletions

View File

@@ -51,10 +51,14 @@ error_messages = {
),
"no_permission": error_response("You do not have permission to run queries with this data source.", 403),
"select_data_source": error_response("Please select data source to run this query.", 401),
"no_data_source": error_response("Target data source not available.", 401),
}
def run_query(query, parameters, data_source, query_id, should_apply_auto_limit, max_age=0):
if not data_source:
return error_messages["no_data_source"]
if data_source.paused:
if data_source.pause_reason:
message = "{} is paused ({}). Please try later.".format(data_source.name, data_source.pause_reason)

View File

@@ -1,9 +1,16 @@
from redash.handlers.query_results import error_messages
from redash.handlers.query_results import error_messages, run_query
from redash.models import db
from redash.utils import json_dumps
from tests import BaseTestCase
class TestRunQuery(BaseTestCase):
def test_run_query_with_no_data_source(self):
response, status = run_query(None, None, None, None, None)
self.assertDictEqual(response, error_messages["no_data_source"][0])
self.assertEqual(status, error_messages["no_data_source"][1])
class TestQueryResultsCacheHeaders(BaseTestCase):
def test_uses_cache_headers_for_specific_result(self):
query_result = self.factory.create_query_result()