Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 06a54db

Browse files
committed
Raise explicit KeyError for copy job resource w/o source table(s).
Addresses: googleapis/google-cloud-python#2884 (comment)
1 parent 60ca568 commit 06a54db

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

google/cloud/bigquery/job.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,11 @@ def from_api_repr(cls, resource, client):
744744
sources = []
745745
source_configs = config.get('sourceTables')
746746
if source_configs is None:
747-
source_configs = [config['sourceTable']]
747+
single = config.get('sourceTable')
748+
if single is None:
749+
raise KeyError(
750+
"Resource missing 'sourceTables' / 'sourceTable'")
751+
source_configs = [single]
748752
for source_config in source_configs:
749753
dataset = Dataset(source_config['datasetId'], client)
750754
sources.append(Table(source_config['tableId'], dataset))

unit_tests/test_job.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,29 @@ def test_from_api_repr_w_sourcetable(self):
795795
self.assertIs(job._client, client)
796796
self._verifyResourceProperties(job, RESOURCE)
797797

798+
def test_from_api_repr_wo_sources(self):
799+
self._setUpConstants()
800+
client = _Client(self.PROJECT)
801+
RESOURCE = {
802+
'id': self.JOB_ID,
803+
'jobReference': {
804+
'projectId': self.PROJECT,
805+
'jobId': self.JOB_NAME,
806+
},
807+
'configuration': {
808+
'copy': {
809+
'destinationTable': {
810+
'projectId': self.PROJECT,
811+
'datasetId': self.DS_NAME,
812+
'tableId': self.DESTINATION_TABLE,
813+
},
814+
}
815+
},
816+
}
817+
klass = self._get_target_class()
818+
with self.assertRaises(KeyError):
819+
klass.from_api_repr(RESOURCE, client=client)
820+
798821
def test_from_api_repr_w_properties(self):
799822
client = _Client(self.PROJECT)
800823
RESOURCE = self._makeResource()

0 commit comments

Comments
 (0)