Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move is_url_key method to function for testability. #3750

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))