-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make GoogleSheets __init__ compatible with other Google API connectors
Retain all original GoogleSheets compatibility, but add ability for it to take a string as its init, same as GoogleBigQuery and GoogleCloudStorage. Also have it look in GOOGLE_APPLICATION_CREDENTIALS for its credentials (after checking it's original env variable GOOGLE_DRIVE_CREDENTIALS). So it should retain full backward compatibility, and no one currently using it should notice any difference, but it will now also be able to be instantiated using the same calling convention as other Google connectors.
- Loading branch information
1 parent
cc449e3
commit dc6d2f2
Showing
2 changed files
with
162 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import unittest | ||
import gspread | ||
import logging | ||
import os | ||
|
||
from parsons.google.google_sheets import GoogleSheets | ||
|
@@ -83,12 +84,12 @@ def test_append_to_spreadsheet(self): | |
|
||
# First check that we didn't muck with the original data | ||
for i in range(self.test_table.num_rows): | ||
self.assertEqual(self.test_table.data[i], result_table.data[i]) | ||
self.assertEqual(list(self.test_table.data[i]), result_table.data[i]) | ||
orig_row_count = self.test_table.num_rows | ||
|
||
# Then check that we appended the data properly | ||
for i in range(append_table.num_rows): | ||
self.assertEqual(append_table.data[i], result_table.data[orig_row_count+i]) | ||
self.assertEqual(list(append_table.data[i]), result_table.data[orig_row_count+i]) | ||
|
||
# Test that we can append to an empty sheet | ||
self.google_sheets.add_sheet(self.spreadsheet_id, 'Sheet3') | ||
|
@@ -132,3 +133,63 @@ def test_share_spreadsheet(self): | |
self.spreadsheet_id, '[email protected]', role='reader', notify=True) | ||
permissions = self.google_sheets.get_spreadsheet_permissions(self.spreadsheet_id) | ||
self.assertIn('[email protected]', permissions['emailAddress']) | ||
|
||
# Test GoogleSheets helper function _get_credentials_dict() | ||
import json | ||
import tempfile | ||
|
||
from parsons.google.google_sheets import logger, _get_google_credentials | ||
|
||
class TestGoogleSheetsGetCredentials(unittest.TestCase): | ||
def test_get_credentials_dict(self): | ||
"""Test that the script gets its credentials correctly from all the | ||
different places it could get them.""" | ||
|
||
credential_dict = {'key1':'value1', 'key2':'value2'} | ||
json_credentials = json.dumps(credential_dict) | ||
|
||
self.assertDictEqual(_get_google_credentials(credential_dict), | ||
credential_dict) | ||
|
||
# Read JSON credentials | ||
self.assertDictEqual(_get_google_credentials(json_credentials), | ||
credential_dict) | ||
|
||
# Read credentials from a file | ||
cred_file = tempfile.NamedTemporaryFile(mode='w') | ||
cred_file.write(json_credentials) | ||
cred_file.flush() | ||
self.assertDictEqual(_get_google_credentials(cred_file.name), | ||
credential_dict) | ||
|
||
os.environ['TEMP_ENV'] = json_credentials | ||
os.environ['OTHER_TEMP_ENV'] = 'foo' | ||
|
||
self.assertDictEqual( | ||
_get_google_credentials(credentials=None, | ||
env_variables=['TEMP_ENV', 'OTHER_TEMP_ENV']), | ||
credential_dict) | ||
|
||
# Should fail - bad credentials come first | ||
with self.assertRaises(ValueError): | ||
with self.assertLogs(logger, logging.ERROR): | ||
_get_google_credentials(credentials=None, | ||
env_variables=['OTHER_TEMP_ENV', 'TEMP_ENV']) | ||
# Env credentials are a filename | ||
os.environ['OTHER_TEMP_ENV'] = cred_file.name | ||
self.assertDictEqual( | ||
_get_google_credentials(credentials=None, | ||
env_variables=['OTHER_TEMP_ENV', 'TEMP_ENV']), | ||
credential_dict) | ||
|
||
# Fail when nonexistent filename | ||
with self.assertRaises(ValueError): | ||
with self.assertLogs(logger, logging.ERROR): | ||
_get_google_credentials(credentials='foo') | ||
|
||
# Fail when no env variables and no credentials | ||
with self.assertRaises(KeyError): | ||
with self.assertLogs(logger, logging.ERROR): | ||
_get_google_credentials(credentials=None,env_variables=[]) | ||
|
||
|