Skip to content

Commit c4aa9df

Browse files
author
Jon Wayne Parrott
authored
Raise ValueError if credentials are not from google-auth (googleapis#2828)
1 parent 2d9c109 commit c4aa9df

27 files changed

Lines changed: 464 additions & 337 deletions

File tree

bigquery/unit_tests/test_client.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import unittest
1616

17+
import mock
18+
19+
20+
def _make_credentials():
21+
import google.auth.credentials
22+
return mock.Mock(spec=google.auth.credentials.Credentials)
23+
1724

1825
class TestClient(unittest.TestCase):
1926

@@ -28,7 +35,7 @@ def _make_one(self, *args, **kw):
2835
def test_ctor(self):
2936
from google.cloud.bigquery._http import Connection
3037
PROJECT = 'PROJECT'
31-
creds = object()
38+
creds = _make_credentials()
3239
http = object()
3340
client = self._make_one(project=PROJECT, credentials=creds, http=http)
3441
self.assertIsInstance(client._connection, Connection)
@@ -57,7 +64,7 @@ def test_list_projects_defaults(self):
5764
'friendlyName': 'Two'},
5865
]
5966
}
60-
creds = object()
67+
creds = _make_credentials()
6168
client = self._make_one(PROJECT_1, creds)
6269
conn = client._connection = _Connection(DATA)
6370

@@ -86,7 +93,7 @@ def test_list_projects_explicit_response_missing_projects_key(self):
8693
PATH = 'projects'
8794
TOKEN = 'TOKEN'
8895
DATA = {}
89-
creds = object()
96+
creds = _make_credentials()
9097
client = self._make_one(PROJECT, creds)
9198
conn = client._connection = _Connection(DATA)
9299

@@ -128,7 +135,7 @@ def test_list_datasets_defaults(self):
128135
'friendlyName': 'Two'},
129136
]
130137
}
131-
creds = object()
138+
creds = _make_credentials()
132139
client = self._make_one(PROJECT, creds)
133140
conn = client._connection = _Connection(DATA)
134141

@@ -156,7 +163,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
156163
PATH = 'projects/%s/datasets' % PROJECT
157164
TOKEN = 'TOKEN'
158165
DATA = {}
159-
creds = object()
166+
creds = _make_credentials()
160167
client = self._make_one(PROJECT, creds)
161168
conn = client._connection = _Connection(DATA)
162169

@@ -176,36 +183,21 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
176183
self.assertEqual(req['query_params'],
177184
{'all': True, 'maxResults': 3, 'pageToken': TOKEN})
178185

179-
def test_dataset_defaults(self):
186+
def test_dataset(self):
180187
from google.cloud.bigquery.dataset import Dataset
181188
PROJECT = 'PROJECT'
182189
DATASET = 'dataset_name'
183-
creds = object()
190+
creds = _make_credentials()
184191
http = object()
185192
client = self._make_one(project=PROJECT, credentials=creds, http=http)
186193
dataset = client.dataset(DATASET)
187194
self.assertIsInstance(dataset, Dataset)
188195
self.assertEqual(dataset.name, DATASET)
189196
self.assertIs(dataset._client, client)
190-
self.assertEqual(dataset.project, PROJECT)
191-
192-
def test_dataset_explicit(self):
193-
from google.cloud.bigquery.dataset import Dataset
194-
PROJECT = 'my-project-123'
195-
OTHER_PROJECT = 'other-project-456'
196-
DATASET = 'dataset_name'
197-
creds = object()
198-
http = object()
199-
client = self._make_one(project=PROJECT, credentials=creds, http=http)
200-
dataset = client.dataset(DATASET, project=OTHER_PROJECT)
201-
self.assertIsInstance(dataset, Dataset)
202-
self.assertEqual(dataset.name, DATASET)
203-
self.assertIs(dataset._client, client)
204-
self.assertEqual(dataset.project, OTHER_PROJECT)
205197

206198
def test_job_from_resource_unknown_type(self):
207199
PROJECT = 'PROJECT'
208-
creds = object()
200+
creds = _make_credentials()
209201
client = self._make_one(PROJECT, creds)
210202
with self.assertRaises(ValueError):
211203
client.job_from_resource({'configuration': {'nonesuch': {}}})
@@ -319,7 +311,7 @@ def test_list_jobs_defaults(self):
319311
LOAD_DATA,
320312
]
321313
}
322-
creds = object()
314+
creds = _make_credentials()
323315
client = self._make_one(PROJECT, creds)
324316
conn = client._connection = _Connection(DATA)
325317

@@ -375,7 +367,7 @@ def test_list_jobs_load_job_wo_sourceUris(self):
375367
LOAD_DATA,
376368
]
377369
}
378-
creds = object()
370+
creds = _make_credentials()
379371
client = self._make_one(PROJECT, creds)
380372
conn = client._connection = _Connection(DATA)
381373

@@ -403,7 +395,7 @@ def test_list_jobs_explicit_missing(self):
403395
PATH = 'projects/%s/jobs' % PROJECT
404396
DATA = {}
405397
TOKEN = 'TOKEN'
406-
creds = object()
398+
creds = _make_credentials()
407399
client = self._make_one(PROJECT, creds)
408400
conn = client._connection = _Connection(DATA)
409401

@@ -434,7 +426,7 @@ def test_load_table_from_storage(self):
434426
DATASET = 'dataset_name'
435427
DESTINATION = 'destination_table'
436428
SOURCE_URI = 'http://example.com/source.csv'
437-
creds = object()
429+
creds = _make_credentials()
438430
http = object()
439431
client = self._make_one(project=PROJECT, credentials=creds, http=http)
440432
dataset = client.dataset(DATASET)
@@ -453,7 +445,7 @@ def test_copy_table(self):
453445
DATASET = 'dataset_name'
454446
SOURCE = 'source_table'
455447
DESTINATION = 'destination_table'
456-
creds = object()
448+
creds = _make_credentials()
457449
http = object()
458450
client = self._make_one(project=PROJECT, credentials=creds, http=http)
459451
dataset = client.dataset(DATASET)
@@ -473,7 +465,7 @@ def test_extract_table_to_storage(self):
473465
DATASET = 'dataset_name'
474466
SOURCE = 'source_table'
475467
DESTINATION = 'gs://bucket_name/object_name'
476-
creds = object()
468+
creds = _make_credentials()
477469
http = object()
478470
client = self._make_one(project=PROJECT, credentials=creds, http=http)
479471
dataset = client.dataset(DATASET)
@@ -490,7 +482,7 @@ def test_run_async_query_defaults(self):
490482
PROJECT = 'PROJECT'
491483
JOB = 'job_name'
492484
QUERY = 'select count(*) from persons'
493-
creds = object()
485+
creds = _make_credentials()
494486
http = object()
495487
client = self._make_one(project=PROJECT, credentials=creds, http=http)
496488
job = client.run_async_query(JOB, QUERY)
@@ -508,7 +500,7 @@ def test_run_async_w_udf_resources(self):
508500
PROJECT = 'PROJECT'
509501
JOB = 'job_name'
510502
QUERY = 'select count(*) from persons'
511-
creds = object()
503+
creds = _make_credentials()
512504
http = object()
513505
client = self._make_one(project=PROJECT, credentials=creds, http=http)
514506
udf_resources = [UDFResource("resourceUri", RESOURCE_URI)]
@@ -526,7 +518,7 @@ def test_run_async_w_query_parameters(self):
526518
PROJECT = 'PROJECT'
527519
JOB = 'job_name'
528520
QUERY = 'select count(*) from persons'
529-
creds = object()
521+
creds = _make_credentials()
530522
http = object()
531523
client = self._make_one(project=PROJECT, credentials=creds, http=http)
532524
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)]
@@ -543,7 +535,7 @@ def test_run_sync_query_defaults(self):
543535
from google.cloud.bigquery.query import QueryResults
544536
PROJECT = 'PROJECT'
545537
QUERY = 'select count(*) from persons'
546-
creds = object()
538+
creds = _make_credentials()
547539
http = object()
548540
client = self._make_one(project=PROJECT, credentials=creds, http=http)
549541
query = client.run_sync_query(QUERY)
@@ -560,7 +552,7 @@ def test_run_sync_query_w_udf_resources(self):
560552
RESOURCE_URI = 'gs://some-bucket/js/lib.js'
561553
PROJECT = 'PROJECT'
562554
QUERY = 'select count(*) from persons'
563-
creds = object()
555+
creds = _make_credentials()
564556
http = object()
565557
client = self._make_one(project=PROJECT, credentials=creds, http=http)
566558
udf_resources = [UDFResource("resourceUri", RESOURCE_URI)]
@@ -577,7 +569,7 @@ def test_run_sync_query_w_query_parameters(self):
577569
from google.cloud.bigquery.query import QueryResults
578570
PROJECT = 'PROJECT'
579571
QUERY = 'select count(*) from persons'
580-
creds = object()
572+
creds = _make_credentials()
581573
http = object()
582574
client = self._make_one(project=PROJECT, credentials=creds, http=http)
583575
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)]

0 commit comments

Comments
 (0)