From 48e12bd55ef178162c52a25bad97d826260b25b6 Mon Sep 17 00:00:00 2001 From: Victor Mota Date: Thu, 30 May 2019 02:37:43 +0000 Subject: [PATCH 1/4] Adds _connection object to bigquery magics context. --- bigquery/google/cloud/bigquery/magics.py | 4 ++ bigquery/tests/unit/test__helpers.py | 11 ++++++ bigquery/tests/unit/test_client.py | 11 +----- bigquery/tests/unit/test_magics.py | 50 ++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/bigquery/google/cloud/bigquery/magics.py b/bigquery/google/cloud/bigquery/magics.py index 27626752d8e7..fbb8f86f66d9 100644 --- a/bigquery/google/cloud/bigquery/magics.py +++ b/bigquery/google/cloud/bigquery/magics.py @@ -159,6 +159,7 @@ class Context(object): def __init__(self): self._credentials = None self._project = None + self._connection = None self._use_bqstorage_api = None @property @@ -270,6 +271,7 @@ def _run_query(client, query, job_config=None): while True: print("\rQuery executing: {:0.2f}s".format(time.time() - start_time), end="") try: + print(query_job) query_job.result(timeout=0.5) break except futures.TimeoutError: @@ -363,6 +365,8 @@ def _cell_magic(line, query): project = args.project or context.project client = bigquery.Client(project=project, credentials=context.credentials) + if context._connection: + client._connection = context._connection bqstorage_client = _make_bqstorage_client( args.use_bqstorage_api or context.use_bqstorage_api, context.credentials ) diff --git a/bigquery/tests/unit/test__helpers.py b/bigquery/tests/unit/test__helpers.py index 3884695d83af..aa6d981f043b 100644 --- a/bigquery/tests/unit/test__helpers.py +++ b/bigquery/tests/unit/test__helpers.py @@ -782,6 +782,17 @@ def _make_field(field_type, mode="NULLABLE", name="testing", fields=()): return SchemaField(name=name, field_type=field_type, mode=mode, fields=fields) +def _make_connection(*responses): + import google.cloud.bigquery._http + import mock + from google.cloud.exceptions import NotFound + + mock_conn = mock.create_autospec(google.cloud.bigquery._http.Connection) + mock_conn.user_agent = "testing 1.2.3" + mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")] + return mock_conn + + class Test_scalar_field_to_json(unittest.TestCase): def _call_fut(self, field, value): from google.cloud.bigquery._helpers import _scalar_field_to_json diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index 13889f90d7e8..0c6a3c71e188 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -40,6 +40,7 @@ import google.api_core.exceptions from google.api_core.gapic_v1 import client_info import google.cloud._helpers +from tests.unit.test__helpers import _make_connection from google.cloud.bigquery.dataset import DatasetReference @@ -49,16 +50,6 @@ def _make_credentials(): return mock.Mock(spec=google.auth.credentials.Credentials) -def _make_connection(*responses): - import google.cloud.bigquery._http - from google.cloud.exceptions import NotFound - - mock_conn = mock.create_autospec(google.cloud.bigquery._http.Connection) - mock_conn.user_agent = "testing 1.2.3" - mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")] - return mock_conn - - def _make_list_partitons_meta_info(project, dataset_id, table_id, num_rows=0): return { "tableReference": { diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py index 0db0bc5ebf52..343cc293d487 100644 --- a/bigquery/tests/unit/test_magics.py +++ b/bigquery/tests/unit/test_magics.py @@ -14,6 +14,7 @@ import re import mock +import six from concurrent import futures import pytest @@ -38,6 +39,7 @@ bigquery_storage_v1beta1 = None from google.cloud.bigquery import table from google.cloud.bigquery import magics +from tests.unit.test__helpers import _make_connection pytestmark = pytest.mark.skipif(IPython is None, reason="Requires `ipython`") @@ -100,6 +102,54 @@ def test_context_credentials_and_project_can_be_set_explicitly(): # default should not be called if credentials & project are explicitly set assert default_mock.call_count == 0 +@pytest.mark.usefixtures("ipython_interactive") +def test_context_connection_can_be_overriden(): + ip = IPython.get_ipython() + ip.extension_manager.load_extension("google.cloud.bigquery") + magics.context._project = None + magics.context._credentials = None + + credentials_mock = mock.create_autospec( + google.auth.credentials.Credentials, instance=True + ) + project = "project-123" + default_patch = mock.patch( + "google.auth.default", return_value=(credentials_mock, project) + ) + + query = "select * from persons" + job_reference = {"projectId": project, "jobId": "some-random-id"} + table = {"projectId": project, "datasetId": "ds", "tableId": "persons"} + resource = { + "jobReference": job_reference, + "configuration": {"query": {"destinationTable": table, "query": query, "queryParameters": [], "useLegacySql": False}}, + "status": {"state": "DONE"} + } + data = { + "jobReference": job_reference, + "totalRows": 0, + "rows": [] + } + + conn = magics.context._connection = _make_connection(resource, data) + list_rows_patch = mock.patch( + "google.cloud.bigquery.client.Client.list_rows", return_value=google.cloud.bigquery.table._EmptyRowIterator() + ) + with list_rows_patch as list_rows, default_patch: + ip.run_cell_magic("bigquery", "", query) + + + # Check that query actually starts the job. + list_rows.assert_called() + assert len(conn.api_request.call_args_list) == 2 + _, req = conn.api_request.call_args_list[0] + assert req["method"] == "POST" + assert req["path"] == "/projects/{}/jobs".format(project) + sent = req["data"] + assert isinstance(sent["jobReference"]["jobId"], six.string_types) + sent_config = sent["configuration"]["query"] + assert sent_config["query"] == query + def test__run_query(): magics.context._credentials = None From c4581f3475ae80eb534d2dbdcf1290090c9be200 Mon Sep 17 00:00:00 2001 From: Victor Mota Date: Thu, 30 May 2019 02:39:02 +0000 Subject: [PATCH 2/4] Fixes formatting. --- bigquery/tests/unit/test_magics.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py index 343cc293d487..b52bce155a0f 100644 --- a/bigquery/tests/unit/test_magics.py +++ b/bigquery/tests/unit/test_magics.py @@ -102,6 +102,7 @@ def test_context_credentials_and_project_can_be_set_explicitly(): # default should not be called if credentials & project are explicitly set assert default_mock.call_count == 0 + @pytest.mark.usefixtures("ipython_interactive") def test_context_connection_can_be_overriden(): ip = IPython.get_ipython() @@ -122,23 +123,26 @@ def test_context_connection_can_be_overriden(): table = {"projectId": project, "datasetId": "ds", "tableId": "persons"} resource = { "jobReference": job_reference, - "configuration": {"query": {"destinationTable": table, "query": query, "queryParameters": [], "useLegacySql": False}}, - "status": {"state": "DONE"} - } - data = { - "jobReference": job_reference, - "totalRows": 0, - "rows": [] + "configuration": { + "query": { + "destinationTable": table, + "query": query, + "queryParameters": [], + "useLegacySql": False, + } + }, + "status": {"state": "DONE"}, } + data = {"jobReference": job_reference, "totalRows": 0, "rows": []} conn = magics.context._connection = _make_connection(resource, data) list_rows_patch = mock.patch( - "google.cloud.bigquery.client.Client.list_rows", return_value=google.cloud.bigquery.table._EmptyRowIterator() + "google.cloud.bigquery.client.Client.list_rows", + return_value=google.cloud.bigquery.table._EmptyRowIterator(), ) with list_rows_patch as list_rows, default_patch: ip.run_cell_magic("bigquery", "", query) - # Check that query actually starts the job. list_rows.assert_called() assert len(conn.api_request.call_args_list) == 2 From 46a6f433001dbcefe4f372a3560a08fe67b84b56 Mon Sep 17 00:00:00 2001 From: Victor Mota Date: Thu, 30 May 2019 02:52:06 +0000 Subject: [PATCH 3/4] Removes print line. --- bigquery/google/cloud/bigquery/magics.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bigquery/google/cloud/bigquery/magics.py b/bigquery/google/cloud/bigquery/magics.py index fbb8f86f66d9..6bd1c45dfcd5 100644 --- a/bigquery/google/cloud/bigquery/magics.py +++ b/bigquery/google/cloud/bigquery/magics.py @@ -271,7 +271,6 @@ def _run_query(client, query, job_config=None): while True: print("\rQuery executing: {:0.2f}s".format(time.time() - start_time), end="") try: - print(query_job) query_job.result(timeout=0.5) break except futures.TimeoutError: From 2578e54beed14d8b0942cd2116863983ce69abc5 Mon Sep 17 00:00:00 2001 From: Victor Mota Date: Fri, 31 May 2019 06:49:38 +0000 Subject: [PATCH 4/4] Moves make_connection to helpers.py and adds unit test for no connection in context. --- bigquery/tests/unit/helpers.py | 24 +++ bigquery/tests/unit/test__helpers.py | 11 -- bigquery/tests/unit/test_client.py | 236 +++++++++++++-------------- bigquery/tests/unit/test_magics.py | 59 ++++++- 4 files changed, 199 insertions(+), 131 deletions(-) create mode 100644 bigquery/tests/unit/helpers.py diff --git a/bigquery/tests/unit/helpers.py b/bigquery/tests/unit/helpers.py new file mode 100644 index 000000000000..5b731a763a99 --- /dev/null +++ b/bigquery/tests/unit/helpers.py @@ -0,0 +1,24 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def make_connection(*responses): + import google.cloud.bigquery._http + import mock + from google.cloud.exceptions import NotFound + + mock_conn = mock.create_autospec(google.cloud.bigquery._http.Connection) + mock_conn.user_agent = "testing 1.2.3" + mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")] + return mock_conn diff --git a/bigquery/tests/unit/test__helpers.py b/bigquery/tests/unit/test__helpers.py index aa6d981f043b..3884695d83af 100644 --- a/bigquery/tests/unit/test__helpers.py +++ b/bigquery/tests/unit/test__helpers.py @@ -782,17 +782,6 @@ def _make_field(field_type, mode="NULLABLE", name="testing", fields=()): return SchemaField(name=name, field_type=field_type, mode=mode, fields=fields) -def _make_connection(*responses): - import google.cloud.bigquery._http - import mock - from google.cloud.exceptions import NotFound - - mock_conn = mock.create_autospec(google.cloud.bigquery._http.Connection) - mock_conn.user_agent = "testing 1.2.3" - mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")] - return mock_conn - - class Test_scalar_field_to_json(unittest.TestCase): def _call_fut(self, field, value): from google.cloud.bigquery._helpers import _scalar_field_to_json diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index 0c6a3c71e188..e5429df5a5f8 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -40,7 +40,7 @@ import google.api_core.exceptions from google.api_core.gapic_v1 import client_info import google.cloud._helpers -from tests.unit.test__helpers import _make_connection +from tests.unit.helpers import make_connection from google.cloud.bigquery.dataset import DatasetReference @@ -154,7 +154,7 @@ def test__get_query_results_miss_w_explicit_project_and_timeout(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client._get_query_results( @@ -176,7 +176,7 @@ def test__get_query_results_miss_w_client_location(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds, location=self.LOCATION) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client._get_query_results("nothere", None) @@ -207,7 +207,7 @@ def test__get_query_results_hit(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - client._connection = _make_connection(data) + client._connection = make_connection(data) query_results = client._get_query_results(job_id, None) self.assertEqual(query_results.total_rows, 10) @@ -220,7 +220,7 @@ def test_get_service_account_email(self): client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) email = "bq-123@bigquery-encryption.iam.gserviceaccount.com" resource = {"kind": "bigquery#getServiceAccountResponse", "email": email} - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) service_account_email = client.get_service_account_email() @@ -235,7 +235,7 @@ def test_get_service_account_email_w_alternate_project(self): client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) email = "bq-123@bigquery-encryption.iam.gserviceaccount.com" resource = {"kind": "bigquery#getServiceAccountResponse", "email": email} - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) service_account_email = client.get_service_account_email(project=project) @@ -269,7 +269,7 @@ def test_list_projects_defaults(self): } creds = _make_credentials() client = self._make_one(PROJECT_1, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_projects() page = six.next(iterator.pages) @@ -293,7 +293,7 @@ def test_list_projects_explicit_response_missing_projects_key(self): DATA = {} creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_projects(max_results=3, page_token=TOKEN) page = six.next(iterator.pages) @@ -341,7 +341,7 @@ def test_list_datasets_defaults(self): } creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_datasets() page = six.next(iterator.pages) @@ -362,7 +362,7 @@ def test_list_datasets_defaults(self): def test_list_datasets_w_project(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) list(client.list_datasets(project="other-project")) @@ -377,7 +377,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self): DATA = {} creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_datasets( include_all=True, filter=FILTER, max_results=3, page_token=TOKEN @@ -433,7 +433,7 @@ def test_get_dataset(self): "id": "%s:%s" % (self.PROJECT, self.DS_ID), "datasetReference": {"projectId": self.PROJECT, "datasetId": self.DS_ID}, } - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset_ref = client.dataset(self.DS_ID) dataset = client.get_dataset(dataset_ref) @@ -444,31 +444,31 @@ def test_get_dataset(self): # Test retry. # Not a cloud API exception (missing 'errors' field). - client._connection = _make_connection(Exception(""), resource) + client._connection = make_connection(Exception(""), resource) with self.assertRaises(Exception): client.get_dataset(dataset_ref) # Zero-length errors field. - client._connection = _make_connection(ServerError(""), resource) + client._connection = make_connection(ServerError(""), resource) with self.assertRaises(ServerError): client.get_dataset(dataset_ref) # Non-retryable reason. - client._connection = _make_connection( + client._connection = make_connection( ServerError("", errors=[{"reason": "serious"}]), resource ) with self.assertRaises(ServerError): client.get_dataset(dataset_ref) # Retryable reason, but retry is disabled. - client._connection = _make_connection( + client._connection = make_connection( ServerError("", errors=[{"reason": "backendError"}]), resource ) with self.assertRaises(ServerError): client.get_dataset(dataset_ref, retry=None) # Retryable reason, default retry: success. - client._connection = _make_connection( + client._connection = make_connection( ServerError("", errors=[{"reason": "backendError"}]), resource ) dataset = client.get_dataset( @@ -488,7 +488,7 @@ def test_create_dataset_minimal(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) ds_ref = client.dataset(self.DS_ID) before = Dataset(ds_ref) @@ -539,7 +539,7 @@ def test_create_dataset_w_attrs(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) entries = [ AccessEntry("OWNER", "userByEmail", USER_EMAIL), AccessEntry(None, "view", VIEW), @@ -598,7 +598,7 @@ def test_create_dataset_w_custom_property(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) ds_ref = client.dataset(self.DS_ID) before = Dataset(ds_ref) @@ -637,7 +637,7 @@ def test_create_dataset_w_client_location_wo_dataset_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) ds_ref = client.dataset(self.DS_ID) before = Dataset(ds_ref) @@ -678,7 +678,7 @@ def test_create_dataset_w_client_location_w_dataset_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) ds_ref = client.dataset(self.DS_ID) before = Dataset(ds_ref) @@ -717,7 +717,7 @@ def test_create_dataset_w_reference(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = client.create_dataset(client.dataset(self.DS_ID)) @@ -752,7 +752,7 @@ def test_create_dataset_w_fully_qualified_string(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = client.create_dataset("{}.{}".format(self.PROJECT, self.DS_ID)) @@ -787,7 +787,7 @@ def test_create_dataset_w_string(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = client.create_dataset(self.DS_ID) @@ -815,7 +815,7 @@ def test_create_dataset_alreadyexists_w_exists_ok_false(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - client._connection = _make_connection( + client._connection = make_connection( google.api_core.exceptions.AlreadyExists("dataset already exists") ) @@ -835,7 +835,7 @@ def test_create_dataset_alreadyexists_w_exists_ok_true(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.AlreadyExists("dataset already exists"), resource ) @@ -873,7 +873,7 @@ def test_create_table_w_day_partition(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table.time_partitioning = TimePartitioning() @@ -905,7 +905,7 @@ def test_create_table_w_custom_property(self): client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() resource["newAlphaProperty"] = "unreleased property" - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table._properties["newAlphaProperty"] = "unreleased property" @@ -935,7 +935,7 @@ def test_create_table_w_encryption_configuration(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table.encryption_configuration = EncryptionConfiguration( kms_key_name=self.KMS_KEY_NAME @@ -966,7 +966,7 @@ def test_create_table_w_day_partition_and_expire(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table.time_partitioning = TimePartitioning(expiration_ms=100) @@ -1022,7 +1022,7 @@ def test_create_table_w_schema_and_query(self): SchemaField("full_name", "STRING", mode="REQUIRED"), SchemaField("age", "INTEGER", mode="REQUIRED"), ] - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF, schema=schema) table.view_query = query @@ -1080,7 +1080,7 @@ def test_create_table_w_external(self): } } ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) ec = ExternalConfig("CSV") ec.autodetect = True @@ -1117,7 +1117,7 @@ def test_create_table_w_reference(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) got = client.create_table(self.TABLE_REF) @@ -1140,7 +1140,7 @@ def test_create_table_w_fully_qualified_string(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) got = client.create_table( "{}.{}.{}".format(self.PROJECT, self.DS_ID, self.TABLE_ID) @@ -1165,7 +1165,7 @@ def test_create_table_w_string(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) got = client.create_table("{}.{}".format(self.DS_ID, self.TABLE_ID)) @@ -1189,7 +1189,7 @@ def test_create_table_alreadyexists_w_exists_ok_false(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.AlreadyExists("table already exists") ) @@ -1219,7 +1219,7 @@ def test_create_table_alreadyexists_w_exists_ok_true(self): client = self._make_one( project=self.PROJECT, credentials=creds, location=self.LOCATION ) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.AlreadyExists("table already exists"), resource ) @@ -1265,7 +1265,7 @@ def test_get_model(self): "modelId": self.MODEL_ID, } } - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) model_ref = client.dataset(self.DS_ID).model(self.MODEL_ID) got = client.get_model(model_ref) @@ -1289,7 +1289,7 @@ def test_get_model_w_string(self): "modelId": self.MODEL_ID, } } - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) model_id = "{}.{}.{}".format(self.PROJECT, self.DS_ID, self.MODEL_ID) got = client.get_model(model_id) @@ -1307,7 +1307,7 @@ def test_get_table(self): http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) resource = self._make_table_resource() - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = client.get_table(self.TABLE_REF) conn.api_request.assert_called_once_with(method="GET", path="/%s" % path) @@ -1375,7 +1375,7 @@ def test_update_dataset(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(RESOURCE, RESOURCE) + conn = client._connection = make_connection(RESOURCE, RESOURCE) ds = Dataset(client.dataset(self.DS_ID)) ds.description = DESCRIPTION ds.friendly_name = FRIENDLY_NAME @@ -1422,7 +1422,7 @@ def test_update_dataset_w_custom_property(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = Dataset(client.dataset(self.DS_ID)) dataset._properties["newAlphaProperty"] = "unreleased property" @@ -1465,7 +1465,7 @@ def test_update_model(self): } creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource, resource) + conn = client._connection = make_connection(resource, resource) model_id = "{}.{}.{}".format(self.PROJECT, self.DS_ID, self.MODEL_ID) model = Model(model_id) model.description = description @@ -1539,7 +1539,7 @@ def test_update_table(self): ] creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource, resource) + conn = client._connection = make_connection(resource, resource) table = Table(self.TABLE_REF, schema=schema) table.description = description table.friendly_name = title @@ -1596,7 +1596,7 @@ def test_update_table_w_custom_property(self): resource["newAlphaProperty"] = "unreleased property" creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table._properties["newAlphaProperty"] = "unreleased property" @@ -1624,7 +1624,7 @@ def test_update_table_only_use_legacy_sql(self): resource["view"] = {"useLegacySql": True} creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF) table.view_use_legacy_sql = True @@ -1683,7 +1683,7 @@ def test_update_table_w_query(self): ) creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) table = Table(self.TABLE_REF, schema=schema) table.expires = exp_time table.view_query = query @@ -1731,7 +1731,7 @@ def test_update_table_w_schema_None(self): resource2 = self._make_table_resource() creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource1, resource2) + conn = client._connection = make_connection(resource1, resource2) table = client.get_table( # Test with string for table ID "{}.{}.{}".format( @@ -1768,7 +1768,7 @@ def test_update_table_delete_property(self): resource2["description"] = None creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(resource1, resource2) + conn = client._connection = make_connection(resource1, resource2) table = Table(self.TABLE_REF) table.description = description table.friendly_name = title @@ -1789,7 +1789,7 @@ def test_list_tables_empty(self): path = "/projects/{}/datasets/{}/tables".format(self.PROJECT, self.DS_ID) creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) dataset = client.dataset(self.DS_ID) iterator = client.list_tables(dataset) @@ -1808,7 +1808,7 @@ def test_list_models_empty(self): path = "/projects/{}/datasets/{}/models".format(self.PROJECT, self.DS_ID) creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) dataset_id = "{}.{}".format(self.PROJECT, self.DS_ID) iterator = client.list_models(dataset_id) @@ -1851,7 +1851,7 @@ def test_list_models_defaults(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) dataset = client.dataset(self.DS_ID) iterator = client.list_models(dataset) @@ -1911,7 +1911,7 @@ def test_list_tables_defaults(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) dataset = client.dataset(self.DS_ID) iterator = client.list_tables(dataset) @@ -1965,7 +1965,7 @@ def test_list_tables_explicit(self): creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) dataset = client.dataset(self.DS_ID) iterator = client.list_tables( @@ -2007,7 +2007,7 @@ def test_delete_dataset(self): PATH = "projects/%s/datasets/%s" % (self.PROJECT, self.DS_ID) creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection(*([{}] * len(datasets))) + conn = client._connection = make_connection(*([{}] * len(datasets))) for arg in datasets: client.delete_dataset(arg) conn.api_request.assert_called_with( @@ -2020,7 +2020,7 @@ def test_delete_dataset_delete_contents(self): PATH = "projects/%s/datasets/%s" % (self.PROJECT, self.DS_ID) creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) - conn = client._connection = _make_connection({}, {}) + conn = client._connection = make_connection({}, {}) ds_ref = client.dataset(self.DS_ID) for arg in (ds_ref, Dataset(ds_ref)): client.delete_dataset(arg, delete_contents=True) @@ -2041,7 +2041,7 @@ def test_delete_dataset_w_not_found_ok_false(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("dataset not found") ) @@ -2055,7 +2055,7 @@ def test_delete_dataset_w_not_found_ok_true(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("dataset not found") ) @@ -2080,7 +2080,7 @@ def test_delete_model(self): client.dataset(self.DS_ID).model(self.MODEL_ID), Model(model_id), ) - conn = client._connection = _make_connection(*([{}] * len(models))) + conn = client._connection = make_connection(*([{}] * len(models))) for arg in models: client.delete_model(arg) @@ -2099,7 +2099,7 @@ def test_delete_model_w_not_found_ok_false(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("model not found") ) @@ -2115,7 +2115,7 @@ def test_delete_model_w_not_found_ok_true(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("model not found") ) @@ -2145,7 +2145,7 @@ def test_delete_table(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(*([{}] * len(tables))) + conn = client._connection = make_connection(*([{}] * len(tables))) for arg in tables: client.delete_table(arg) @@ -2164,7 +2164,7 @@ def test_delete_table_w_not_found_ok_false(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("table not found") ) @@ -2180,7 +2180,7 @@ def test_delete_table_w_not_found_ok_true(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection( + conn = client._connection = make_connection( google.api_core.exceptions.NotFound("table not found") ) @@ -2206,7 +2206,7 @@ def test_get_job_miss_w_explict_project(self): JOB_ID = "NONESUCH" creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client.get_job(JOB_ID, project=OTHER_PROJECT, location=self.LOCATION) @@ -2224,7 +2224,7 @@ def test_get_job_miss_w_client_location(self): JOB_ID = "NONESUCH" creds = _make_credentials() client = self._make_one(self.PROJECT, creds, location=self.LOCATION) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client.get_job(JOB_ID, project=OTHER_PROJECT) @@ -2262,7 +2262,7 @@ def test_get_job_hit(self): } creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(ASYNC_QUERY_DATA) + conn = client._connection = make_connection(ASYNC_QUERY_DATA) job = client.get_job(JOB_ID) @@ -2284,7 +2284,7 @@ def test_cancel_job_miss_w_explict_project(self): JOB_ID = "NONESUCH" creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client.cancel_job(JOB_ID, project=OTHER_PROJECT, location=self.LOCATION) @@ -2302,7 +2302,7 @@ def test_cancel_job_miss_w_client_location(self): JOB_ID = "NONESUCH" creds = _make_credentials() client = self._make_one(self.PROJECT, creds, location=self.LOCATION) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() with self.assertRaises(NotFound): client.cancel_job(JOB_ID, project=OTHER_PROJECT) @@ -2327,7 +2327,7 @@ def test_cancel_job_hit(self): RESOURCE = {"job": QUERY_JOB_RESOURCE} creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) job = client.cancel_job(JOB_ID) @@ -2437,7 +2437,7 @@ def test_list_jobs_defaults(self): } creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_jobs() page = six.next(iterator.pages) @@ -2479,7 +2479,7 @@ def test_list_jobs_load_job_wo_sourceUris(self): DATA = {"nextPageToken": TOKEN, "jobs": [LOAD_DATA]} creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_jobs() page = six.next(iterator.pages) @@ -2503,7 +2503,7 @@ def test_list_jobs_explicit_missing(self): TOKEN = "TOKEN" creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) iterator = client.list_jobs( max_results=1000, page_token=TOKEN, all_users=True, state_filter="done" @@ -2530,7 +2530,7 @@ def test_list_jobs_explicit_missing(self): def test_list_jobs_w_project(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) list(client.list_jobs(project="other-project")) @@ -2543,7 +2543,7 @@ def test_list_jobs_w_project(self): def test_list_jobs_w_time_filter(self): creds = _make_credentials() client = self._make_one(self.PROJECT, creds) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) # One millisecond after the unix epoch. start_time = datetime.datetime(1970, 1, 1, 0, 0, 0, 1000) @@ -2585,7 +2585,7 @@ def test_load_table_from_uri(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) destination = client.dataset(self.DS_ID).table(DESTINATION) job = client.load_table_from_uri(SOURCE_URI, destination, job_id=JOB) @@ -2601,7 +2601,7 @@ def test_load_table_from_uri(self): self.assertEqual(list(job.source_uris), [SOURCE_URI]) self.assertIs(job.destination, destination) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) job = client.load_table_from_uri([SOURCE_URI], destination, job_id=JOB) self.assertIsInstance(job, LoadJob) @@ -2634,7 +2634,7 @@ def test_load_table_from_uri_w_explicit_project(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) destination = client.dataset(self.DS_ID).table(destination_id) client.load_table_from_uri( @@ -2676,7 +2676,7 @@ def test_load_table_from_uri_w_client_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, _http=http, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.load_table_from_uri( source_uri, @@ -2722,7 +2722,7 @@ def _initiate_resumable_upload_helper(self, num_retries=None): response_headers = {"location": resumable_url} fake_transport = self._mock_transport(http_client.OK, response_headers) client = self._make_one(project=self.PROJECT, _http=fake_transport) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() # Create some mock arguments and call the method under test. data = b"goodbye gudbi gootbee" @@ -2788,7 +2788,7 @@ def _do_multipart_upload_success_helper(self, get_boundary, num_retries=None): fake_transport = self._mock_transport(http_client.OK, {}) client = self._make_one(project=self.PROJECT, _http=fake_transport) - conn = client._connection = _make_connection() + conn = client._connection = make_connection() # Create some mock arguments. data = b"Bzzzz-zap \x00\x01\xf4" @@ -2863,7 +2863,7 @@ def test_copy_table(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) dataset = client.dataset(self.DS_ID) source = dataset.table(SOURCE) destination = dataset.table(DESTINATION) @@ -2881,7 +2881,7 @@ def test_copy_table(self): self.assertEqual(list(job.sources), [source]) self.assertIs(job.destination, destination) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) source2 = dataset.table(SOURCE + "2") job = client.copy_table([source, source2], destination, job_id=JOB) self.assertIsInstance(job, CopyJob) @@ -2920,7 +2920,7 @@ def test_copy_table_w_explicit_project(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = client.dataset(self.DS_ID) source = dataset.table(source_id) destination = dataset.table(destination_id) @@ -2970,7 +2970,7 @@ def test_copy_table_w_client_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, _http=http, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.copy_table( # Test with string for table IDs. @@ -2989,7 +2989,7 @@ def test_copy_table_w_source_strings(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - client._connection = _make_connection({}) + client._connection = make_connection({}) sources = [ "dataset_wo_proj.some_table", "other_project.other_dataset.other_table", @@ -3034,7 +3034,7 @@ def test_extract_table(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) dataset = client.dataset(self.DS_ID) source = dataset.table(SOURCE) @@ -3076,7 +3076,7 @@ def test_extract_table_w_explicit_project(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) dataset = client.dataset(self.DS_ID) source = dataset.table(source_id) @@ -3119,7 +3119,7 @@ def test_extract_table_w_client_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, _http=http, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.extract_table( # Test with string for table ID. @@ -3159,7 +3159,7 @@ def test_extract_table_generated_job_id(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) dataset = client.dataset(self.DS_ID) source = dataset.table(SOURCE) job_config = ExtractJobConfig() @@ -3203,7 +3203,7 @@ def test_extract_table_w_destination_uris(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) dataset = client.dataset(self.DS_ID) source = dataset.table(SOURCE) @@ -3233,7 +3233,7 @@ def test_query_defaults(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) job = client.query(QUERY) @@ -3269,7 +3269,7 @@ def test_query_w_explicit_project(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.query( query, job_id=job_id, project="other-project", location=self.LOCATION @@ -3320,7 +3320,7 @@ def test_query_w_explicit_job_config(self): _http=http, default_query_job_config=default_job_config, ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) job_config = QueryJobConfig() job_config.use_query_cache = True @@ -3372,7 +3372,7 @@ def test_query_w_explicit_job_config_override(self): _http=http, default_query_job_config=default_job_config, ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) job_config = QueryJobConfig() job_config.use_query_cache = True @@ -3420,7 +3420,7 @@ def test_query_w_client_default_config_no_incoming(self): _http=http, default_query_job_config=default_job_config, ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.query(query, job_id=job_id, location=self.LOCATION) @@ -3445,7 +3445,7 @@ def test_query_w_client_location(self): client = self._make_one( project=self.PROJECT, credentials=creds, _http=http, location=self.LOCATION ) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) client.query(query, job_id=job_id, project="other-project") @@ -3469,7 +3469,7 @@ def test_query_detect_location(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(resource) + conn = client._connection = make_connection(resource) job = client.query(query) @@ -3502,7 +3502,7 @@ def test_query_w_udf_resources(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) udf_resources = [UDFResource("resourceUri", RESOURCE_URI)] config = QueryJobConfig() config.udf_resources = udf_resources @@ -3558,7 +3558,7 @@ def test_query_w_query_parameters(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESOURCE) + conn = client._connection = make_connection(RESOURCE) query_parameters = [ScalarQueryParameter("foo", "INT64", 123)] config = QueryJobConfig() config.query_parameters = query_parameters @@ -3627,7 +3627,7 @@ def test_insert_rows_w_schema(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) schema = [ SchemaField("full_name", "STRING", mode="REQUIRED"), SchemaField("age", "INTEGER", mode="REQUIRED"), @@ -3683,7 +3683,7 @@ def test_insert_rows_w_list_of_dictionaries(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) schema = [ SchemaField("full_name", "STRING", mode="REQUIRED"), SchemaField("age", "INTEGER", mode="REQUIRED"), @@ -3744,7 +3744,7 @@ def test_insert_rows_w_list_of_Rows(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) schema = [ SchemaField("full_name", "STRING", mode="REQUIRED"), SchemaField("age", "INTEGER", mode="REQUIRED"), @@ -3802,7 +3802,7 @@ def test_insert_rows_w_skip_invalid_and_ignore_unknown(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(RESPONSE) + conn = client._connection = make_connection(RESPONSE) schema = [ SchemaField("full_name", "STRING", mode="REQUIRED"), SchemaField("age", "INTEGER", mode="REQUIRED"), @@ -3863,7 +3863,7 @@ def test_insert_rows_w_repeated_fields(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) color = SchemaField("color", "STRING", mode="REPEATED") items = SchemaField("items", "INTEGER", mode="REPEATED") score = SchemaField("score", "INTEGER") @@ -3956,7 +3956,7 @@ def test_insert_rows_w_record_schema(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) full_name = SchemaField("full_name", "STRING", mode="REQUIRED") area_code = SchemaField("area_code", "STRING", "REQUIRED") local_number = SchemaField("local_number", "STRING", "REQUIRED") @@ -4048,7 +4048,7 @@ def test_insert_rows_w_numeric(self): creds = _make_credentials() http = object() client = self._make_one(project=project, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) table_ref = DatasetReference(project, ds_id).table(table_id) schema = [ table.SchemaField("account", "STRING"), @@ -4098,7 +4098,7 @@ def test_insert_rows_json(self): creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) table_ref = DatasetReference(PROJECT, DS_ID).table(TABLE_ID) schema = [ SchemaField("full_name", "STRING", mode="REQUIRED"), @@ -4136,7 +4136,7 @@ def test_insert_rows_json_with_string_id(self): client = self._make_one( project="default-project", credentials=creds, _http=http ) - conn = client._connection = _make_connection({}) + conn = client._connection = make_connection({}) with mock.patch("uuid.uuid4", side_effect=map(str, range(len(rows)))): errors = client.insert_rows_json("proj.dset.tbl", rows) @@ -4170,7 +4170,7 @@ def test_list_partitions(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - client._connection = _make_connection(meta_info, data) + client._connection = make_connection(meta_info, data) table = Table(self.TABLE_REF) partition_list = client.list_partitions(table) @@ -4185,7 +4185,7 @@ def test_list_partitions_with_string_id(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - client._connection = _make_connection(meta_info, {}) + client._connection = make_connection(meta_info, {}) partition_list = client.list_partitions( "{}.{}".format(self.DS_ID, self.TABLE_ID) @@ -4247,7 +4247,7 @@ def _bigquery_timestamp_float_repr(ts_float): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(DATA, DATA) + conn = client._connection = make_connection(DATA, DATA) full_name = SchemaField("full_name", "STRING", mode="REQUIRED") age = SchemaField("age", "INTEGER", mode="NULLABLE") joined = SchemaField("joined", "TIMESTAMP", mode="NULLABLE") @@ -4277,7 +4277,7 @@ def test_list_rows_empty_table(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - client._connection = _make_connection(response, response) + client._connection = make_connection(response, response) # Table that has no schema because it's an empty table. rows = client.list_rows( @@ -4311,7 +4311,7 @@ def test_list_rows_query_params(self): ({"max_results": 2}, {"maxResults": 2}), ({"start_index": 1, "max_results": 2}, {"startIndex": 1, "maxResults": 2}), ] - conn = client._connection = _make_connection(*len(tests) * [{}]) + conn = client._connection = make_connection(*len(tests) * [{}]) for i, test in enumerate(tests): iterator = client.list_rows(table, **test[0]) six.next(iterator.pages) @@ -4354,7 +4354,7 @@ def test_list_rows_repeated_fields(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) color = SchemaField("color", "STRING", mode="REPEATED") index = SchemaField("index", "INTEGER", "REPEATED") score = SchemaField("score", "FLOAT", "REPEATED") @@ -4410,7 +4410,7 @@ def test_list_rows_w_record_schema(self): creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(DATA) + conn = client._connection = make_connection(DATA) full_name = SchemaField("full_name", "STRING", mode="REQUIRED") area_code = SchemaField("area_code", "STRING", "REQUIRED") local_number = SchemaField("local_number", "STRING", "REQUIRED") @@ -4491,7 +4491,7 @@ def test_list_rows_with_missing_schema(self): for table in schemaless_tables: client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) - conn = client._connection = _make_connection(table_data, rows_data) + conn = client._connection = make_connection(table_data, rows_data) row_iter = client.list_rows(table) diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py index b52bce155a0f..70848cbcae64 100644 --- a/bigquery/tests/unit/test_magics.py +++ b/bigquery/tests/unit/test_magics.py @@ -39,7 +39,7 @@ bigquery_storage_v1beta1 = None from google.cloud.bigquery import table from google.cloud.bigquery import magics -from tests.unit.test__helpers import _make_connection +from tests.unit.helpers import make_connection pytestmark = pytest.mark.skipif(IPython is None, reason="Requires `ipython`") @@ -135,7 +135,7 @@ def test_context_connection_can_be_overriden(): } data = {"jobReference": job_reference, "totalRows": 0, "rows": []} - conn = magics.context._connection = _make_connection(resource, data) + conn = magics.context._connection = make_connection(resource, data) list_rows_patch = mock.patch( "google.cloud.bigquery.client.Client.list_rows", return_value=google.cloud.bigquery.table._EmptyRowIterator(), @@ -155,6 +155,61 @@ def test_context_connection_can_be_overriden(): assert sent_config["query"] == query +@pytest.mark.usefixtures("ipython_interactive") +def test_context_no_connection(): + ip = IPython.get_ipython() + ip.extension_manager.load_extension("google.cloud.bigquery") + magics.context._project = None + magics.context._credentials = None + magics.context._connection = None + + credentials_mock = mock.create_autospec( + google.auth.credentials.Credentials, instance=True + ) + project = "project-123" + default_patch = mock.patch( + "google.auth.default", return_value=(credentials_mock, project) + ) + + query = "select * from persons" + job_reference = {"projectId": project, "jobId": "some-random-id"} + table = {"projectId": project, "datasetId": "ds", "tableId": "persons"} + resource = { + "jobReference": job_reference, + "configuration": { + "query": { + "destinationTable": table, + "query": query, + "queryParameters": [], + "useLegacySql": False, + } + }, + "status": {"state": "DONE"}, + } + data = {"jobReference": job_reference, "totalRows": 0, "rows": []} + + conn_mock = make_connection(resource, data, data, data) + conn_patch = mock.patch("google.cloud.bigquery.client.Connection", autospec=True) + list_rows_patch = mock.patch( + "google.cloud.bigquery.client.Client.list_rows", + return_value=google.cloud.bigquery.table._EmptyRowIterator(), + ) + with conn_patch as conn, list_rows_patch as list_rows, default_patch: + conn.return_value = conn_mock + ip.run_cell_magic("bigquery", "", query) + + # Check that query actually starts the job. + list_rows.assert_called() + assert len(conn_mock.api_request.call_args_list) == 2 + _, req = conn_mock.api_request.call_args_list[0] + assert req["method"] == "POST" + assert req["path"] == "/projects/{}/jobs".format(project) + sent = req["data"] + assert isinstance(sent["jobReference"]["jobId"], six.string_types) + sent_config = sent["configuration"]["query"] + assert sent_config["query"] == query + + def test__run_query(): magics.context._credentials = None