Skip to content

Commit

Permalink
Accept copy jobs with single 'sourceTable' config.
Browse files Browse the repository at this point in the history
Such jobs would be created via another client:  we map that configuration
onto a sequence of tables containing only the one item.

Closes: #2882.
  • Loading branch information
tseaver committed Dec 19, 2016
1 parent 3d354dc commit 28d5c67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ def from_api_repr(cls, resource, client):
dataset = Dataset(dest_config['datasetId'], client)
destination = Table(dest_config['tableId'], dataset)
sources = []
for source_config in config['sourceTables']:
source_configs = config.get('sourceTables')
if source_configs is None:
source_configs = [config['sourceTable']]
for source_config in source_configs:
dataset = Dataset(source_config['datasetId'], client)
sources.append(Table(source_config['tableId'], dataset))
job = cls(name, destination, sources, client=client)
Expand Down
33 changes: 32 additions & 1 deletion bigquery/unit_tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,9 @@ def _verifyResourceProperties(self, job, resource):
self.assertEqual(job.destination.dataset_name, table_ref['datasetId'])
self.assertEqual(job.destination.name, table_ref['tableId'])

sources = config['sourceTables']
sources = config.get('sourceTables')
if sources is None:
sources = [config['sourceTable']]
self.assertEqual(len(sources), len(job.sources))
for table_ref, table in zip(sources, job.sources):
self.assertEqual(table.project, table_ref['projectId'])
Expand Down Expand Up @@ -764,6 +766,35 @@ def test_from_api_repr_bare(self):
self.assertIs(job._client, client)
self._verifyResourceProperties(job, RESOURCE)

def test_from_api_repr_w_sourcetable(self):
self._setUpConstants()
client = _Client(self.PROJECT)
RESOURCE = {
'id': self.JOB_ID,
'jobReference': {
'projectId': self.PROJECT,
'jobId': self.JOB_NAME,
},
'configuration': {
'copy': {
'sourceTable': {
'projectId': self.PROJECT,
'datasetId': self.DS_NAME,
'tableId': self.SOURCE_TABLE,
},
'destinationTable': {
'projectId': self.PROJECT,
'datasetId': self.DS_NAME,
'tableId': self.DESTINATION_TABLE,
},
}
},
}
klass = self._get_target_class()
job = klass.from_api_repr(RESOURCE, client=client)
self.assertIs(job._client, client)
self._verifyResourceProperties(job, RESOURCE)

def test_from_api_repr_w_properties(self):
client = _Client(self.PROJECT)
RESOURCE = self._makeResource()
Expand Down

0 comments on commit 28d5c67

Please sign in to comment.