File tree 4 files changed +40
-4
lines changed 4 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -306,8 +306,13 @@ class Dataset(object):
306
306
https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets
307
307
308
308
Args:
309
- dataset_ref (google.cloud.bigquery.dataset.DatasetReference):
310
- a pointer to a dataset
309
+ dataset_ref (Union[ \
310
+ :class:`~google.cloud.bigquery.dataset.DatasetReference`, \
311
+ str, \
312
+ ]):
313
+ A pointer to a dataset. If ``dataset_ref`` is a string, it must
314
+ include both the project ID and the dataset ID, separated by
315
+ ``.``.
311
316
"""
312
317
313
318
_PROPERTY_TO_API_FIELD = {
@@ -318,6 +323,8 @@ class Dataset(object):
318
323
}
319
324
320
325
def __init__ (self , dataset_ref ):
326
+ if isinstance (dataset_ref , six .string_types ):
327
+ dataset_ref = DatasetReference .from_string (dataset_ref )
321
328
self ._properties = {"datasetReference" : dataset_ref .to_api_repr (), "labels" : {}}
322
329
323
330
@property
Original file line number Diff line number Diff line change @@ -348,8 +348,13 @@ class Table(object):
348
348
https://cloud.google.com/bigquery/docs/reference/rest/v2/tables
349
349
350
350
Args:
351
- table_ref (google.cloud.bigquery.table.TableReference):
352
- A pointer to a table
351
+ table_ref (Union[ \
352
+ :class:`~google.cloud.bigquery.table.TableReference`, \
353
+ str, \
354
+ ]):
355
+ A pointer to a table. If ``table_ref`` is a string, it must
356
+ included a project ID, dataset ID, and table ID, each separated
357
+ by ``.``.
353
358
schema (List[google.cloud.bigquery.schema.SchemaField]):
354
359
The table's schema
355
360
"""
@@ -367,6 +372,8 @@ class Table(object):
367
372
}
368
373
369
374
def __init__ (self , table_ref , schema = None ):
375
+ if isinstance (table_ref , six .string_types ):
376
+ table_ref = TableReference .from_string (table_ref )
370
377
self ._properties = {"tableReference" : table_ref .to_api_repr (), "labels" : {}}
371
378
# Let the @property do validation.
372
379
if schema is not None :
Original file line number Diff line number Diff line change 15
15
import unittest
16
16
17
17
import mock
18
+ import pytest
18
19
19
20
20
21
class TestAccessEntry (unittest .TestCase ):
@@ -364,6 +365,16 @@ def test_ctor_defaults(self):
364
365
self .assertIsNone (dataset .friendly_name )
365
366
self .assertIsNone (dataset .location )
366
367
368
+ def test_ctor_string (self ):
369
+ dataset = self ._make_one ("some-project.some_dset" )
370
+ self .assertEqual (dataset .project , "some-project" )
371
+ self .assertEqual (dataset .dataset_id , "some_dset" )
372
+
373
+ def test_ctor_string_wo_project_id (self ):
374
+ with pytest .raises (ValueError ):
375
+ # Project ID is missing.
376
+ self ._make_one ("some_dset" )
377
+
367
378
def test_ctor_explicit (self ):
368
379
from google .cloud .bigquery .dataset import DatasetReference , AccessEntry
369
380
Original file line number Diff line number Diff line change @@ -504,6 +504,17 @@ def test_ctor_w_schema(self):
504
504
505
505
self .assertEqual (table .schema , [full_name , age ])
506
506
507
+ def test_ctor_string (self ):
508
+ table = self ._make_one ("some-project.some_dset.some_tbl" )
509
+ self .assertEqual (table .project , "some-project" )
510
+ self .assertEqual (table .dataset_id , "some_dset" )
511
+ self .assertEqual (table .table_id , "some_tbl" )
512
+
513
+ def test_ctor_string_wo_project_id (self ):
514
+ with pytest .raises (ValueError ):
515
+ # Project ID is missing.
516
+ self ._make_one ("some_dset.some_tbl" )
517
+
507
518
def test_num_bytes_getter (self ):
508
519
dataset = DatasetReference (self .PROJECT , self .DS_ID )
509
520
table_ref = dataset .table (self .TABLE_NAME )
You can’t perform that action at this time.
0 commit comments