Skip to content

Commit a02b620

Browse files
committed
unit test fix - if libraries can not be imported
1 parent 9740938 commit a02b620

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

pandas/io/gbq.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,25 @@ def get_credentials(self):
168168

169169
@staticmethod
170170
def get_application_default_credentials(project_id):
171-
from oauth2client.client import GoogleCredentials
172-
from oauth2client.client import AccessTokenRefreshError
173-
from oauth2client.client import ApplicationDefaultCredentialsError
174-
from apiclient.discovery import build
175-
from apiclient.errors import HttpError
176-
171+
"""
172+
Given a project_id tries to retrieve the
173+
"default application credentials"
174+
Could be useful for running code on Google Cloud Platform
175+
"""
177176
credentials = None
177+
try:
178+
from oauth2client.client import GoogleCredentials
179+
from oauth2client.client import AccessTokenRefreshError
180+
from oauth2client.client import ApplicationDefaultCredentialsError
181+
try:
182+
from googleapiclient.discovery import build
183+
from googleapiclient.errors import HttpError
184+
except:
185+
from apiclient.discovery import build
186+
from apiclient.errors import HttpError
187+
except ImportError:
188+
return None
189+
178190
try:
179191
credentials = GoogleCredentials.get_application_default()
180192
except ApplicationDefaultCredentialsError:

pandas/io/tests/test_gbq.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _test_imports():
8383
from oauth2client.client import GoogleCredentials # noqa
8484
from oauth2client.client import OAuth2WebServerFlow # noqa
8585
from oauth2client.client import AccessTokenRefreshError # noqa
86-
from oauth2client.client import ApplicationDefaultCredentialsError # noqa
86+
from oauth2client.client import \
87+
ApplicationDefaultCredentialsError # noqa
8788

8889
from oauth2client.file import Storage # noqa
8990
from oauth2client.tools import run_flow # noqa
@@ -137,7 +138,8 @@ def _test_imports():
137138
oauth2client_v1 = False
138139

139140
try:
140-
from oauth2client.service_account import ServiceAccountCredentials # noqa
141+
from oauth2client.service_account import \
142+
ServiceAccountCredentials # noqa
141143
except ImportError:
142144
oauth2client_v2 = False
143145

@@ -159,7 +161,8 @@ def clean_gbq_environment(private_key=None):
159161
for i in range(1, 10):
160162
if DATASET_ID + str(i) in dataset.datasets():
161163
dataset_id = DATASET_ID + str(i)
162-
table = gbq._Table(PROJECT_ID, dataset_id, private_key=private_key)
164+
table = gbq._Table(PROJECT_ID, dataset_id,
165+
private_key=private_key)
163166
for j in range(1, 20):
164167
if TABLE_ID + str(j) in dataset.tables(dataset_id):
165168
table.delete(TABLE_ID + str(j))

0 commit comments

Comments
 (0)