Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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