From 28d5c678ee711804585839eff7d1e2937341f974 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 19 Dec 2016 12:53:53 -0500 Subject: [PATCH] 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()