diff --git a/packages/bigquery-magics/tests/system/test_bigquery.py b/packages/bigquery-magics/tests/system/test_bigquery.py index e870a9535ade..17946827a3e8 100644 --- a/packages/bigquery-magics/tests/system/test_bigquery.py +++ b/packages/bigquery-magics/tests/system/test_bigquery.py @@ -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 = """ @@ -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) @@ -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 diff --git a/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py b/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py index 3be93afdb721..55dd8de50119 100644 --- a/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py +++ b/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py @@ -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") diff --git a/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py b/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py index f8fd25b7b625..deca89943ebc 100644 --- a/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py +++ b/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py @@ -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")