Skip to content

Commit

Permalink
Move is_url_key method to function for testability. (getredash#3750)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariarijp authored and harveyrendell committed Nov 14, 2019
1 parent 3a743f9 commit 55417a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
11 changes: 5 additions & 6 deletions redash/query_runner/google_spreadsheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def parse_spreadsheet(spreadsheet, worksheet_num):
return parse_worksheet(worksheet)


def is_url_key(key):
return key.startswith('https://')


class TimeoutSession(Session):
def request(self, *args, **kwargs):
kwargs.setdefault('timeout', 300)
Expand Down Expand Up @@ -171,19 +175,14 @@ def _get_spreadsheet_service(self):
def test_connection(self):
self._get_spreadsheet_service()

def is_url_key(self, key):
if key.startswith('https://'):
return True
return False

def run_query(self, query, user):
logger.debug("Spreadsheet is about to execute query: %s", query)
key, worksheet_num = parse_query(query)

try:
spreadsheet_service = self._get_spreadsheet_service()

if self.is_url_key(key):
if is_url_key(key):
spreadsheet = spreadsheet_service.open_by_url(key)
else:
spreadsheet = spreadsheet_service.open_by_key(key)
Expand Down
11 changes: 10 additions & 1 deletion tests/query_runner/test_google_spreadsheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mock import MagicMock

from redash.query_runner import TYPE_DATETIME, TYPE_FLOAT
from redash.query_runner.google_spreadsheets import TYPE_BOOLEAN, TYPE_STRING, _get_columns_and_column_names, _value_eval_list, parse_query
from redash.query_runner.google_spreadsheets import TYPE_BOOLEAN, TYPE_STRING, _get_columns_and_column_names, _value_eval_list, is_url_key, parse_query
from redash.query_runner.google_spreadsheets import WorksheetNotFoundError, parse_spreadsheet, parse_worksheet


Expand Down Expand Up @@ -100,3 +100,12 @@ def test_get_columns_with_blank(self):
columns, column_names = _get_columns_and_column_names(_columns)

self.assertEqual(['foo', 'column_B', 'baz', 'column_D'], column_names)


class TestIsUrlKey(TestCase):
def test_is_url_key(self):
_key = 'https://docs.google.com/spreadsheets/d/key/edit#gid=12345678'
self.assertTrue(is_url_key(_key))

_key = 'key|0'
self.assertFalse(is_url_key(_key))

0 comments on commit 55417a6

Please sign in to comment.