Skip to content
Open
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
27 changes: 14 additions & 13 deletions packages/bigquery-magics/tests/system/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
"""System tests for Jupyter/IPython connector."""

import re
from unittest import mock

import google.cloud.bigquery
import pandas
from IPython.testing import globalipapp
from IPython.utils import io
import pandas
import psutil


def test_bigquery_magic():
globalipapp.start_ipython()
ip = globalipapp.get_ipython()
current_process = psutil.Process()
conn_count_start = len(current_process.net_connections())

ip.extension_manager.load_extension("bigquery_magics")
sql = """
Expand All @@ -40,10 +39,17 @@ def test_bigquery_magic():
ORDER BY view_count DESC
LIMIT 10
"""
with io.capture_output() as captured:
result = ip.run_cell_magic("bigquery", "--use_rest_api", sql)

conn_count_end = len(current_process.net_connections())
with mock.patch.object(
google.cloud.bigquery.Client,
"close",
autospec=True,
side_effect=google.cloud.bigquery.Client.close,
) as mock_close:
with io.capture_output() as captured:
result = ip.run_cell_magic("bigquery", "--use_rest_api", sql)

# Verify that client close is explicitly called to release sockets.
mock_close.assert_called_once()

lines = re.split("\n|\r", captured.stdout)
# Removes blanks & terminal code (result of display clearing)
Expand All @@ -53,8 +59,3 @@ def test_bigquery_magic():
assert isinstance(result, pandas.DataFrame)
assert len(result) == 10 # verify row count
assert list(result) == ["url", "view_count"] # verify column names

# NOTE: For some reason, the number of open sockets is sometimes one *less*
# than expected when running system tests on Kokoro, thus using the <= assertion.
# That's still fine, however, since the sockets are apparently not leaked.
assert conn_count_end <= conn_count_start # system resources are released
Original file line number Diff line number Diff line change
Expand Up @@ -2974,7 +2974,7 @@ def test_bigquery_magic_query_variable_not_identifier():
# considered a table name, thus we expect an error that the table ID is not valid.
output = captured_io.stderr
assert "ERROR:" in output
assert "must be a fully-qualified ID" in output
assert "table_id" in output


@pytest.mark.usefixtures("mock_credentials")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ def test_query_with_bigframes_warning(mock_ipython):
def test_cell_magic_engine_bigframes_warning(mock_ipython):
from unittest import mock

from IPython.testing.globalipapp import get_ipython
from IPython.testing.globalipapp import get_ipython, start_ipython

start_ipython()
ip = get_ipython()
if ip is None:
from IPython.testing.globalipapp import start_ipython

ip = start_ipython()

ip.extension_manager.load_extension("bigquery_magics")

Expand Down
Loading