Skip to content

Commit

Permalink
BigQuery: client.extract_table use **kwargs for Python 2.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Sep 19, 2017
1 parent 2ec56fd commit 2aad717
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
26 changes: 18 additions & 8 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,8 @@ def copy_table(self, job_id, destination, *sources):
"""
return CopyJob(job_id, destination, sources, client=self)

def extract_table(
self, source, *destination_uris, job_config=None, job_id=None):
"""Construct a job for extracting a table into Cloud Storage files.
def extract_table(self, source, *destination_uris, **kwargs):
"""Start a job to extract a table into Cloud Storage files.
See
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.extract
Expand All @@ -402,16 +401,27 @@ def extract_table(
URIs of Cloud Storage file(s) into which table data is to be
extracted; in format ``gs://<bucket_name>/<object_name_or_glob>``.
:type job_config: :class:`google.cloud.bigquery.job.ExtractJobConfig`
:param job_config:
(Optional) Extra configuration options for the extract job.
:type kwargs: dict
:param kwargs: Additional keyword arguments.
:type job_id: str
:param job_id: (Optional) The ID of the job.
:Keyword Arguments:
* *job_config*
(:class:`google.cloud.bigquery.job.ExtractJobConfig`) --
(Optional) Extra configuration options for the extract job.
* *job_id* (``str``) --
Additional content
(Optional) The ID of the job.
:rtype: :class:`google.cloud.bigquery.job.ExtractJob`
:returns: a new ``ExtractJob`` instance
"""
job_config = None
if 'job_config' in kwargs:
job_config = kwargs['job_config']

job_id = None
if 'job_id' in kwargs:
job_id = kwargs['job_id']
if job_id is None:
job_id = str(uuid.uuid4())

Expand Down
5 changes: 2 additions & 3 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,7 @@ def to_api_repr(self):
:rtype: dict
:returns: A dictionary in the format used by the BigQuery API.
"""
resource = copy.deepcopy(self._properties)
return resource
return copy.deepcopy(self._properties)

@classmethod
def from_api_repr(cls, resource):
Expand Down Expand Up @@ -1120,7 +1119,7 @@ def _build_resource(self):
"""Generate a resource for :meth:`begin`."""

source_ref = {
'projectId': self.source.dataset.project_id,
'projectId': self.source.dataset.project,
'datasetId': self.source.dataset.dataset_id,
'tableId': self.source.table_id,
}
Expand Down
5 changes: 2 additions & 3 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,8 @@ def _load_table_for_extract_table(
blob.upload_from_file(csv_read, content_type='text/csv')
self.to_delete.insert(0, blob)

dataset = Dataset(
table.dataset.dataset_id, Config.CLIENT)
retry_403(dataset.create)()
dataset = retry_403(Config.CLIENT.create_dataset)(
Dataset(table.dataset.dataset_id))
self.to_delete.append(dataset)
table = dataset.table(table.table_id)
self.to_delete.insert(0, table)
Expand Down
8 changes: 7 additions & 1 deletion bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,8 @@ def test_extract_table(self):

def test_extract_table_generated_job_id(self):
from google.cloud.bigquery.job import ExtractJob
from google.cloud.bigquery.job import ExtractJobConfig
from google.cloud.bigquery.job import DestinationFormat

PROJECT = 'PROJECT'
JOB = 'job_name'
Expand All @@ -782,6 +784,7 @@ def test_extract_table_generated_job_id(self):
'tableId': SOURCE,
},
'destinationUris': [DESTINATION],
'destinationFormat': 'NEWLINE_DELIMITED_JSON',
},
},
}
Expand All @@ -791,8 +794,11 @@ def test_extract_table_generated_job_id(self):
conn = client._connection = _Connection(RESOURCE)
dataset = client.dataset(DATASET)
source = dataset.table(SOURCE)
job_config = ExtractJobConfig()
job_config.destination_format = (
DestinationFormat.NEWLINE_DELIMITED_JSON)

job = client.extract_table(source, DESTINATION)
job = client.extract_table(source, DESTINATION, job_config=job_config)

# Check that extract_table actually starts the job.
self.assertEqual(len(conn._requested), 1)
Expand Down
2 changes: 1 addition & 1 deletion bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ def _verifyResourceProperties(self, job, resource):
self.assertEqual(job.destination_uris, config['destinationUris'])

table_ref = config['sourceTable']
self.assertEqual(job.source.dataset.project_id, table_ref['projectId'])
self.assertEqual(job.source.dataset.project, table_ref['projectId'])
self.assertEqual(job.source.dataset.dataset_id, table_ref['datasetId'])
self.assertEqual(job.source.table_id, table_ref['tableId'])

Expand Down

0 comments on commit 2aad717

Please sign in to comment.