From cf81734607f9d28a6fbda8eac92df87ab19378df Mon Sep 17 00:00:00 2001 From: ariarijp Date: Wed, 1 May 2019 12:24:41 +0900 Subject: [PATCH] Move is_url_key method to function for testability. --- redash/query_runner/google_spreadsheets.py | 11 +++++------ tests/query_runner/test_google_spreadsheets.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/redash/query_runner/google_spreadsheets.py b/redash/query_runner/google_spreadsheets.py index e0d7396f1b..3c22f45b7b 100644 --- a/redash/query_runner/google_spreadsheets.py +++ b/redash/query_runner/google_spreadsheets.py @@ -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) @@ -171,11 +175,6 @@ 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) @@ -183,7 +182,7 @@ def run_query(self, query, user): 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) diff --git a/tests/query_runner/test_google_spreadsheets.py b/tests/query_runner/test_google_spreadsheets.py index 9b4bd28313..4848af0af3 100644 --- a/tests/query_runner/test_google_spreadsheets.py +++ b/tests/query_runner/test_google_spreadsheets.py @@ -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 @@ -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))