From 675f9b77a8d598de0528714b33a783766f74f0ec Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 19 Jul 2019 17:32:53 +0200 Subject: [PATCH 1/4] Set ipython user agent when in IPython cell --- bigquery/google/cloud/bigquery/magics.py | 6 +++++- bigquery/tests/unit/test_magics.py | 27 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/bigquery/google/cloud/bigquery/magics.py b/bigquery/google/cloud/bigquery/magics.py index 0acde4f21b5f..b8af6d8a5101 100644 --- a/bigquery/google/cloud/bigquery/magics.py +++ b/bigquery/google/cloud/bigquery/magics.py @@ -1,4 +1,4 @@ -# Copyright 2018 Google LLC +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -144,6 +144,7 @@ except ImportError: # pragma: NO COVER bigquery_storage_v1beta1 = None +from google.api_core import client_info import google.auth from google.cloud import bigquery from google.cloud.bigquery.dbapi import _helpers @@ -398,6 +399,9 @@ def _cell_magic(line, query): project=project, credentials=context.credentials, default_query_job_config=context.default_query_job_config, + client_info=client_info.ClientInfo( + user_agent="ipython-{}".format(IPython.__version__) + ), ) if context._connection: client._connection = context._connection diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py index 6e8c941bbc02..14dced2a835f 100644 --- a/bigquery/tests/unit/test_magics.py +++ b/bigquery/tests/unit/test_magics.py @@ -1,4 +1,4 @@ -# Copyright 2018 Google LLC +# Copyright 2019 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -320,6 +320,31 @@ def test_bigquery_magic_without_optional_arguments(monkeypatch): assert list(return_value) == list(result) # verify column names +@pytest.mark.usefixtures("ipython_interactive") +def test_bigquery_magic_default_connection_user_agent(): + ip = IPython.get_ipython() + ip.extension_manager.load_extension("google.cloud.bigquery") + magics.context._connection = None + + credentials_mock = mock.create_autospec( + google.auth.credentials.Credentials, instance=True + ) + default_patch = mock.patch( + "google.auth.default", return_value=(credentials_mock, "general-project") + ) + run_query_patch = mock.patch( + "google.cloud.bigquery.magics._run_query", autospec=True + ) + conn_patch = mock.patch("google.cloud.bigquery.client.Connection", autospec=True) + + with conn_patch as conn, run_query_patch, default_patch: + ip.run_cell_magic("bigquery", "", "SELECT 17 as num") + + client_info_arg = conn.call_args.kwargs.get("client_info") + assert client_info_arg is not None + assert client_info_arg.user_agent == "ipython-" + IPython.__version__ + + @pytest.mark.usefixtures("ipython_interactive") def test_bigquery_magic_with_legacy_sql(): ip = IPython.get_ipython() From 8c8384facdea56c2d18f7f5a48ecf47b2260152f Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 19 Jul 2019 20:07:24 +0200 Subject: [PATCH 2/4] Revert year bump in license headers --- bigquery/google/cloud/bigquery/magics.py | 2 +- bigquery/tests/unit/test_magics.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/google/cloud/bigquery/magics.py b/bigquery/google/cloud/bigquery/magics.py index b8af6d8a5101..9bf2019c5c2e 100644 --- a/bigquery/google/cloud/bigquery/magics.py +++ b/bigquery/google/cloud/bigquery/magics.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# 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. diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py index 14dced2a835f..1b2a4dd5932f 100644 --- a/bigquery/tests/unit/test_magics.py +++ b/bigquery/tests/unit/test_magics.py @@ -1,4 +1,4 @@ -# Copyright 2019 Google LLC +# 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. From 52624b26e79e6047d3022d17b8b2117cc5d69e63 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 19 Jul 2019 20:31:36 +0200 Subject: [PATCH 3/4] Fix false positive failure in pyarrow warning test Any additional warnings on top of the expected pyarrow warning would cause one of the tests to fail. This commit makes that test more robust by only focusing on the existence of the specific warning of interest. --- bigquery/tests/unit/test_client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index 8ad9dc8858c6..843406300b6e 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -5353,10 +5353,11 @@ def test_load_table_from_dataframe_w_schema_wo_pyarrow(self): dataframe, self.TABLE_REF, job_config=job_config, location=self.LOCATION ) - assert len(warned) == 1 - warning = warned[0] - assert warning.category is PendingDeprecationWarning - assert "pyarrow" in str(warning) + # there might be other warnings unrelated to the expected pyarrow warning, + # thus some filtering is necessary + pyarrow_warning = next((w for w in warned if "pyarrow" in str(w)), None) + assert pyarrow_warning is not None + assert pyarrow_warning.category is PendingDeprecationWarning load_table_from_file.assert_called_once_with( client, From 7b6aecc10ed355b7b81ceb7bbe56000ebc0dd778 Mon Sep 17 00:00:00 2001 From: Peter Lamut Date: Fri, 19 Jul 2019 23:23:28 +0200 Subject: [PATCH 4/4] Modify test - *all* warnings should be pyarrow's --- bigquery/tests/unit/test_client.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py index 843406300b6e..2be40a52e1fc 100644 --- a/bigquery/tests/unit/test_client.py +++ b/bigquery/tests/unit/test_client.py @@ -5353,11 +5353,10 @@ def test_load_table_from_dataframe_w_schema_wo_pyarrow(self): dataframe, self.TABLE_REF, job_config=job_config, location=self.LOCATION ) - # there might be other warnings unrelated to the expected pyarrow warning, - # thus some filtering is necessary - pyarrow_warning = next((w for w in warned if "pyarrow" in str(w)), None) - assert pyarrow_warning is not None - assert pyarrow_warning.category is PendingDeprecationWarning + assert warned # there should be at least one warning + for warning in warned: + assert "pyarrow" in str(warning) + assert warning.category in (DeprecationWarning, PendingDeprecationWarning) load_table_from_file.assert_called_once_with( client,