Skip to content

Commit

Permalink
Merge pull request #2884 from tseaver/2882-bigquery-copyjob-w-single-…
Browse files Browse the repository at this point in the history
…source-table

Accept copy jobs with single 'sourceTable' config.
  • Loading branch information
tseaver authored Dec 28, 2016
2 parents becf0a2 + f003e0f commit d158cc7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
9 changes: 8 additions & 1 deletion bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,14 @@ 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:
single = config.get('sourceTable')
if single is None:
raise KeyError(
"Resource missing 'sourceTables' / 'sourceTable'")
source_configs = [single]
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
56 changes: 55 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,58 @@ 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_wo_sources(self):
self._setUpConstants()
client = _Client(self.PROJECT)
RESOURCE = {
'id': self.JOB_ID,
'jobReference': {
'projectId': self.PROJECT,
'jobId': self.JOB_NAME,
},
'configuration': {
'copy': {
'destinationTable': {
'projectId': self.PROJECT,
'datasetId': self.DS_NAME,
'tableId': self.DESTINATION_TABLE,
},
}
},
}
klass = self._get_target_class()
with self.assertRaises(KeyError):
klass.from_api_repr(RESOURCE, client=client)

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

0 comments on commit d158cc7

Please sign in to comment.