Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQuery: Set IPython user agent when running queries with IPython cell magic #8713

Merged
merged 4 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bigquery/google/cloud/bigquery/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5353,10 +5353,10 @@ 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)
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,
Expand Down
25 changes: 25 additions & 0 deletions bigquery/tests/unit/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down