Skip to content

Commit

Permalink
bigquery: Job and Query default_dataset is DatasetReference (#4037)
Browse files Browse the repository at this point in the history
Change the type of the Job.default_dataset and Query.default_dataset
properties to DatasetReference.
  • Loading branch information
jba authored and tswast committed Sep 22, 2017
1 parent 9839a51 commit 8f0055f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
6 changes: 3 additions & 3 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ def __init__(self, job_id, query, client,
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.createDisposition
"""

default_dataset = _TypedProperty('default_dataset', Dataset)
default_dataset = _TypedProperty('default_dataset', DatasetReference)
"""See
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.defaultDataset
"""
Expand Down Expand Up @@ -1437,8 +1437,8 @@ def _copy_configuration_properties(self, configuration):
if self.default_dataset is not None:
del self.default_dataset
else:
self.default_dataset = Dataset(
DatasetReference(def_ds['projectId'], def_ds['datasetId']))
self.default_dataset = DatasetReference(
def_ds['projectId'], def_ds['datasetId'])
udf_resources = []
for udf_mapping in configuration.get(self._UDF_KEY, ()):
key_val, = udf_mapping.items()
Expand Down
4 changes: 2 additions & 2 deletions bigquery/google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from google.api.core import page_iterator
from google.cloud.bigquery._helpers import _TypedProperty
from google.cloud.bigquery._helpers import _rows_from_json
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.job import QueryJob
from google.cloud.bigquery.table import _parse_schema_resource
from google.cloud.bigquery._helpers import QueryParametersProperty
Expand Down Expand Up @@ -273,7 +273,7 @@ def schema(self):
"""
return _parse_schema_resource(self._properties.get('schema', {}))

default_dataset = _TypedProperty('default_dataset', Dataset)
default_dataset = _TypedProperty('default_dataset', DatasetReference)
"""See
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#defaultDataset
"""
Expand Down
14 changes: 6 additions & 8 deletions bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1608,10 +1608,10 @@ def _verifyResourceProperties(self, job, resource):
else:
self.assertIsNone(job.create_disposition)
if 'defaultDataset' in query_config:
dataset = job.default_dataset
ds_ref = job.default_dataset
ds_ref = {
'projectId': dataset.project,
'datasetId': dataset.dataset_id,
'projectId': ds_ref.project,
'datasetId': ds_ref.dataset_id,
}
self.assertEqual(ds_ref, query_config['defaultDataset'])
else:
Expand Down Expand Up @@ -2125,7 +2125,7 @@ def test_result_error(self):
self.assertEqual(exc_info.exception.code, http_client.BAD_REQUEST)

def test_begin_w_bound_client(self):
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import DatasetReference

PATH = '/projects/%s/jobs' % (self.PROJECT,)
DS_ID = 'DATASET'
Expand All @@ -2139,7 +2139,7 @@ def test_begin_w_bound_client(self):
client = _Client(project=self.PROJECT, connection=conn)

job = self._make_one(self.JOB_NAME, self.QUERY, client)
job.default_dataset = Dataset(DatasetReference(self.PROJECT, DS_ID))
job.default_dataset = DatasetReference(self.PROJECT, DS_ID)

job.begin()

Expand Down Expand Up @@ -2168,7 +2168,6 @@ def test_begin_w_bound_client(self):
self._verifyResourceProperties(job, RESOURCE)

def test_begin_w_alternate_client(self):
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import DatasetReference

PATH = '/projects/%s/jobs' % (self.PROJECT,)
Expand Down Expand Up @@ -2204,12 +2203,11 @@ def test_begin_w_alternate_client(self):
job = self._make_one(self.JOB_NAME, self.QUERY, client1)

dataset_ref = DatasetReference(self.PROJECT, DS_ID)
dataset = Dataset(dataset_ref)
table_ref = dataset_ref.table(TABLE)

job.allow_large_results = True
job.create_disposition = 'CREATE_NEVER'
job.default_dataset = dataset
job.default_dataset = dataset_ref
job.destination = table_ref
job.flatten_results = True
job.priority = 'INTERACTIVE'
Expand Down
12 changes: 6 additions & 6 deletions bigquery/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_ctor_w_query_parameters(self):
self.assertEqual(query.query_parameters, query_parameters)

def test_from_query_job(self):
from google.cloud.bigquery.dataset import Dataset, DatasetReference
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.job import QueryJob
from google.cloud.bigquery._helpers import UDFResource

Expand All @@ -206,8 +206,8 @@ def test_from_query_job(self):
job = QueryJob(
self.JOB_NAME, self.QUERY, client,
udf_resources=[UDFResource("resourceUri", RESOURCE_URI)])
dataset = Dataset(DatasetReference(self.PROJECT, DS_ID))
job.default_dataset = dataset
ds_ref = DatasetReference(self.PROJECT, DS_ID)
job.default_dataset = ds_ref
job.use_query_cache = True
job.use_legacy_sql = True
klass = self._get_target_class()
Expand All @@ -219,7 +219,7 @@ def test_from_query_job(self):
self.assertIs(query._client, client)
self.assertIs(query._job, job)
self.assertEqual(query.udf_resources, job.udf_resources)
self.assertIs(query.default_dataset, dataset)
self.assertIs(query.default_dataset, ds_ref)
self.assertTrue(query.use_query_cache)
self.assertTrue(query.use_legacy_sql)

Expand Down Expand Up @@ -743,9 +743,9 @@ def __init__(self, project='project', connection=None):
self._connection = connection

def dataset(self, dataset_id):
from google.cloud.bigquery.dataset import Dataset, DatasetReference
from google.cloud.bigquery.dataset import DatasetReference

return Dataset(DatasetReference(self.project, dataset_id))
return DatasetReference(self.project, dataset_id)


class _Connection(object):
Expand Down

0 comments on commit 8f0055f

Please sign in to comment.