|
45 | 45 | except (ImportError, AttributeError): # pragma: NO COVER
|
46 | 46 | tqdm = None
|
47 | 47 |
|
| 48 | +import google.cloud.bigquery.query |
| 49 | + |
48 | 50 |
|
49 | 51 | def _make_credentials():
|
50 | 52 | import google.auth.credentials
|
@@ -3942,10 +3944,6 @@ def _make_resource(self, started=False, ended=False):
|
3942 | 3944 | resource = super(TestQueryJob, self)._make_resource(started, ended)
|
3943 | 3945 | config = resource["configuration"]["query"]
|
3944 | 3946 | config["query"] = self.QUERY
|
3945 |
| - |
3946 |
| - if ended: |
3947 |
| - resource["status"] = {"state": "DONE"} |
3948 |
| - |
3949 | 3947 | return resource
|
3950 | 3948 |
|
3951 | 3949 | def _verifyBooleanResourceProperties(self, job, config):
|
@@ -4211,6 +4209,9 @@ def test_done(self):
|
4211 | 4209 | client = _make_client(project=self.PROJECT)
|
4212 | 4210 | resource = self._make_resource(ended=True)
|
4213 | 4211 | job = self._get_target_class().from_api_repr(resource, client)
|
| 4212 | + job._query_results = google.cloud.bigquery.query._QueryResults.from_api_repr( |
| 4213 | + {"jobComplete": True, "jobReference": resource["jobReference"]} |
| 4214 | + ) |
4214 | 4215 | self.assertTrue(job.done())
|
4215 | 4216 |
|
4216 | 4217 | def test_done_w_timeout(self):
|
@@ -4668,35 +4669,110 @@ def test_result(self):
|
4668 | 4669 | from google.cloud.bigquery.table import RowIterator
|
4669 | 4670 |
|
4670 | 4671 | query_resource = {
|
| 4672 | + "jobComplete": False, |
| 4673 | + "jobReference": {"projectId": self.PROJECT, "jobId": self.JOB_ID}, |
| 4674 | + } |
| 4675 | + query_resource_done = { |
4671 | 4676 | "jobComplete": True,
|
4672 | 4677 | "jobReference": {"projectId": self.PROJECT, "jobId": self.JOB_ID},
|
4673 | 4678 | "schema": {"fields": [{"name": "col1", "type": "STRING"}]},
|
4674 | 4679 | "totalRows": "2",
|
4675 | 4680 | }
|
| 4681 | + job_resource = self._make_resource(started=True) |
| 4682 | + job_resource_done = self._make_resource(started=True, ended=True) |
| 4683 | + job_resource_done["configuration"]["query"]["destinationTable"] = { |
| 4684 | + "projectId": "dest-project", |
| 4685 | + "datasetId": "dest_dataset", |
| 4686 | + "tableId": "dest_table", |
| 4687 | + } |
4676 | 4688 | tabledata_resource = {
|
4677 |
| - # Explicitly set totalRows to be different from the query response. |
4678 |
| - # to test update during iteration. |
| 4689 | + # Explicitly set totalRows to be different from the initial |
| 4690 | + # response to test update during iteration. |
4679 | 4691 | "totalRows": "1",
|
4680 | 4692 | "pageToken": None,
|
4681 | 4693 | "rows": [{"f": [{"v": "abc"}]}],
|
4682 | 4694 | }
|
4683 |
| - connection = _make_connection(query_resource, tabledata_resource) |
4684 |
| - client = _make_client(self.PROJECT, connection=connection) |
4685 |
| - resource = self._make_resource(ended=True) |
4686 |
| - job = self._get_target_class().from_api_repr(resource, client) |
| 4695 | + conn = _make_connection( |
| 4696 | + query_resource, query_resource_done, job_resource_done, tabledata_resource |
| 4697 | + ) |
| 4698 | + client = _make_client(self.PROJECT, connection=conn) |
| 4699 | + job = self._get_target_class().from_api_repr(job_resource, client) |
4687 | 4700 |
|
4688 | 4701 | result = job.result()
|
4689 | 4702 |
|
4690 | 4703 | self.assertIsInstance(result, RowIterator)
|
4691 | 4704 | self.assertEqual(result.total_rows, 2)
|
4692 |
| - |
4693 | 4705 | rows = list(result)
|
4694 | 4706 | self.assertEqual(len(rows), 1)
|
4695 | 4707 | self.assertEqual(rows[0].col1, "abc")
|
4696 | 4708 | # Test that the total_rows property has changed during iteration, based
|
4697 | 4709 | # on the response from tabledata.list.
|
4698 | 4710 | self.assertEqual(result.total_rows, 1)
|
4699 | 4711 |
|
| 4712 | + query_results_call = mock.call( |
| 4713 | + method="GET", |
| 4714 | + path=f"/projects/{self.PROJECT}/queries/{self.JOB_ID}", |
| 4715 | + query_params={"maxResults": 0}, |
| 4716 | + timeout=None, |
| 4717 | + ) |
| 4718 | + reload_call = mock.call( |
| 4719 | + method="GET", |
| 4720 | + path=f"/projects/{self.PROJECT}/jobs/{self.JOB_ID}", |
| 4721 | + query_params={}, |
| 4722 | + timeout=None, |
| 4723 | + ) |
| 4724 | + tabledata_call = mock.call( |
| 4725 | + method="GET", |
| 4726 | + path="/projects/dest-project/datasets/dest_dataset/tables/dest_table/data", |
| 4727 | + query_params={}, |
| 4728 | + timeout=None, |
| 4729 | + ) |
| 4730 | + conn.api_request.assert_has_calls( |
| 4731 | + [query_results_call, query_results_call, reload_call, tabledata_call] |
| 4732 | + ) |
| 4733 | + |
| 4734 | + def test_result_with_done_job_calls_get_query_results(self): |
| 4735 | + query_resource_done = { |
| 4736 | + "jobComplete": True, |
| 4737 | + "jobReference": {"projectId": self.PROJECT, "jobId": self.JOB_ID}, |
| 4738 | + "schema": {"fields": [{"name": "col1", "type": "STRING"}]}, |
| 4739 | + "totalRows": "1", |
| 4740 | + } |
| 4741 | + job_resource = self._make_resource(started=True, ended=True) |
| 4742 | + job_resource["configuration"]["query"]["destinationTable"] = { |
| 4743 | + "projectId": "dest-project", |
| 4744 | + "datasetId": "dest_dataset", |
| 4745 | + "tableId": "dest_table", |
| 4746 | + } |
| 4747 | + tabledata_resource = { |
| 4748 | + "totalRows": "1", |
| 4749 | + "pageToken": None, |
| 4750 | + "rows": [{"f": [{"v": "abc"}]}], |
| 4751 | + } |
| 4752 | + conn = _make_connection(query_resource_done, tabledata_resource) |
| 4753 | + client = _make_client(self.PROJECT, connection=conn) |
| 4754 | + job = self._get_target_class().from_api_repr(job_resource, client) |
| 4755 | + |
| 4756 | + result = job.result() |
| 4757 | + |
| 4758 | + rows = list(result) |
| 4759 | + self.assertEqual(len(rows), 1) |
| 4760 | + self.assertEqual(rows[0].col1, "abc") |
| 4761 | + |
| 4762 | + query_results_call = mock.call( |
| 4763 | + method="GET", |
| 4764 | + path=f"/projects/{self.PROJECT}/queries/{self.JOB_ID}", |
| 4765 | + query_params={"maxResults": 0}, |
| 4766 | + timeout=None, |
| 4767 | + ) |
| 4768 | + tabledata_call = mock.call( |
| 4769 | + method="GET", |
| 4770 | + path="/projects/dest-project/datasets/dest_dataset/tables/dest_table/data", |
| 4771 | + query_params={}, |
| 4772 | + timeout=None, |
| 4773 | + ) |
| 4774 | + conn.api_request.assert_has_calls([query_results_call, tabledata_call]) |
| 4775 | + |
4700 | 4776 | def test_result_with_max_results(self):
|
4701 | 4777 | from google.cloud.bigquery.table import RowIterator
|
4702 | 4778 |
|
@@ -4938,6 +5014,9 @@ def test_result_error(self):
|
4938 | 5014 | "errors": [error_result],
|
4939 | 5015 | "state": "DONE",
|
4940 | 5016 | }
|
| 5017 | + job._query_results = google.cloud.bigquery.query._QueryResults.from_api_repr( |
| 5018 | + {"jobComplete": True, "jobReference": job._properties["jobReference"]} |
| 5019 | + ) |
4941 | 5020 | job._set_future_result()
|
4942 | 5021 |
|
4943 | 5022 | with self.assertRaises(exceptions.GoogleCloudError) as exc_info:
|
|
0 commit comments