From 28d5c678ee711804585839eff7d1e2937341f974 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 19 Dec 2016 12:53:53 -0500 Subject: [PATCH 1/2] Accept copy jobs with single 'sourceTable' config. Such jobs would be created via another client: we map that configuration onto a sequence of tables containing only the one item. Closes: #2882. --- bigquery/google/cloud/bigquery/job.py | 5 +++- bigquery/unit_tests/test_job.py | 33 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/bigquery/google/cloud/bigquery/job.py b/bigquery/google/cloud/bigquery/job.py index 5eff2b74ef90..8bc2941b365d 100644 --- a/bigquery/google/cloud/bigquery/job.py +++ b/bigquery/google/cloud/bigquery/job.py @@ -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) diff --git a/bigquery/unit_tests/test_job.py b/bigquery/unit_tests/test_job.py index a451e0040931..9b5e6b4e57a7 100644 --- a/bigquery/unit_tests/test_job.py +++ b/bigquery/unit_tests/test_job.py @@ -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']) @@ -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() From f003e0f60b07136c071f40d1a8733a8d44958eeb Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 21 Dec 2016 13:17:33 -0500 Subject: [PATCH 2/2] Raise explicit KeyError for copy job resource w/o source table(s). Addresses: https://github.com/GoogleCloudPlatform/google-cloud-python/pull/2884#discussion_r93360090 --- bigquery/google/cloud/bigquery/job.py | 6 +++++- bigquery/unit_tests/test_job.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bigquery/google/cloud/bigquery/job.py b/bigquery/google/cloud/bigquery/job.py index 8bc2941b365d..d5152fcee25b 100644 --- a/bigquery/google/cloud/bigquery/job.py +++ b/bigquery/google/cloud/bigquery/job.py @@ -744,7 +744,11 @@ def from_api_repr(cls, resource, client): sources = [] source_configs = config.get('sourceTables') if source_configs is None: - source_configs = [config['sourceTable']] + 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)) diff --git a/bigquery/unit_tests/test_job.py b/bigquery/unit_tests/test_job.py index 9b5e6b4e57a7..57b588a5801c 100644 --- a/bigquery/unit_tests/test_job.py +++ b/bigquery/unit_tests/test_job.py @@ -795,6 +795,29 @@ def test_from_api_repr_w_sourcetable(self): 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()