@@ -56,6 +56,27 @@ def _skip_if_no_private_key_contents():
56
56
_skip_if_no_private_key_contents ()
57
57
58
58
59
+ def _skip_if_get_correct_default_credentials (can = False ):
60
+ got_credentials = False
61
+ try :
62
+ from oauth2client .client import GoogleCredentials
63
+ from apiclient .discovery import build
64
+ credentials = GoogleCredentials .get_application_default ()
65
+ bigquery_service = build ('bigquery' , 'v2' , credentials = credentials )
66
+ jobs = bigquery_service .jobs ()
67
+ job_data = {'configuration' : {'query' : {'query' : 'SELECT 1' }}}
68
+ jobs .insert (projectId = PROJECT_ID , body = job_data ).execute ()
69
+ got_credentials = True
70
+ except :
71
+ pass
72
+ if can and got_credentials :
73
+ raise nose .SkipTest ("Cannot get default_credentials "
74
+ "from the environment!" )
75
+ if (not can ) and (not got_credentials ):
76
+ raise nose .SkipTest ("Can get default_credentials "
77
+ "from the environment!" )
78
+
79
+
59
80
def _test_imports ():
60
81
global _GOOGLE_API_CLIENT_INSTALLED , _GOOGLE_API_CLIENT_VALID_VERSION , \
61
82
_HTTPLIB2_INSTALLED , _SETUPTOOLS_INSTALLED
@@ -218,11 +239,16 @@ def test_should_be_able_to_get_results_from_query(self):
218
239
schema , pages = self .sut .run_query ('SELECT 1' )
219
240
self .assertTrue (pages is not None )
220
241
221
- def test_get_application_default_credentials_should_not_throw_error (self ):
242
+ def test_get_application_default_credentials_does_not_throw_error (self ):
243
+ _skip_if_get_correct_default_credentials (can = True )
244
+ credentials = self .sut .get_application_default_credentials ()
245
+ self .assertIsNone (credentials )
246
+
247
+ def test_get_application_default_credentials_returns_credentials (self ):
248
+ _skip_if_get_correct_default_credentials (can = False )
222
249
from oauth2client .client import GoogleCredentials
223
250
credentials = self .sut .get_application_default_credentials ()
224
- valid_types = (type (None ), GoogleCredentials )
225
- assert isinstance (credentials , valid_types )
251
+ self .assertTrue (isinstance (credentials , GoogleCredentials ))
226
252
227
253
228
254
class TestGBQConnectorServiceAccountKeyPathIntegration (tm .TestCase ):
0 commit comments