Skip to content

Commit 17bef41

Browse files
authored
test(bigquery-magics): make table_id parsing check version-agnostic (#17562)
> [!NOTE] > These changes are at a high level unrelated. They are being submitted as a single corrective action. Separating them leads to a chicken or the egg set of failures (i.e. no one PR will pass all CI/CD tests with out the other changes). --- ### test(bigquery-magics): make table_id parsing check version-agnostic The unit test `test_bigquery_magic_query_variable_not_identifier` was asserting that a specific error message ("must be a fully-qualified ID") was in the stderr output. However, in a recent versions of dependencies, this error message changed (depending on whether tests were run under Python 3.10 or 3.11+). There is a key term that is common to both of the error messages: `table_id`. This change confirms that we get one of the two expected error messages by checking for `table_id`, making it compatible regardless of which runtime the tests run under. --- ### refactor(magics): replace fragile psutil socket check with client close mock spy The system test `test_bigquery_magic` was using `psutil` to count open sockets before and after the test to detect leaks (i.e. ensure all the system resources were released). This approach proved to be fragile across different environments (e.g., Kokoro). This change replaces the socket counting with a mock spy on `google.cloud.bigquery.Client.close` to verify that the client is explicitly closed, which is the intended mechanism for releasing system resources. --- ### test(bigquery-magics): simplify IPython initialization in deprecation test The unit test `test_cell_magic_engine_bigframes_warning` in `test_deprecation.py` was conditionally initializing IPython. Only one of the code paths was being exercised so we were getting a coverage issue. This change simplifies the initialization by unconditionally calling `start_ipython()` and assigning the returned instance to `ip` ensuring a clean and consistent state for the test and avoiding the need for a conditional check.
1 parent e688531 commit 17bef41

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

packages/bigquery-magics/tests/system/test_bigquery.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
"""System tests for Jupyter/IPython connector."""
1616

1717
import re
18+
from unittest import mock
1819

20+
import google.cloud.bigquery
21+
import pandas
1922
from IPython.testing import globalipapp
2023
from IPython.utils import io
21-
import pandas
22-
import psutil
2324

2425

2526
def test_bigquery_magic():
2627
globalipapp.start_ipython()
2728
ip = globalipapp.get_ipython()
28-
current_process = psutil.Process()
29-
conn_count_start = len(current_process.net_connections())
3029

3130
ip.extension_manager.load_extension("bigquery_magics")
3231
sql = """
@@ -40,10 +39,17 @@ def test_bigquery_magic():
4039
ORDER BY view_count DESC
4140
LIMIT 10
4241
"""
43-
with io.capture_output() as captured:
44-
result = ip.run_cell_magic("bigquery", "--use_rest_api", sql)
45-
46-
conn_count_end = len(current_process.net_connections())
42+
with mock.patch.object(
43+
google.cloud.bigquery.Client,
44+
"close",
45+
autospec=True,
46+
side_effect=google.cloud.bigquery.Client.close,
47+
) as mock_close:
48+
with io.capture_output() as captured:
49+
result = ip.run_cell_magic("bigquery", "--use_rest_api", sql)
50+
51+
# Verify that client close is explicitly called to release sockets.
52+
mock_close.assert_called_once()
4753

4854
lines = re.split("\n|\r", captured.stdout)
4955
# Removes blanks & terminal code (result of display clearing)
@@ -53,8 +59,3 @@ def test_bigquery_magic():
5359
assert isinstance(result, pandas.DataFrame)
5460
assert len(result) == 10 # verify row count
5561
assert list(result) == ["url", "view_count"] # verify column names
56-
57-
# NOTE: For some reason, the number of open sockets is sometimes one *less*
58-
# than expected when running system tests on Kokoro, thus using the <= assertion.
59-
# That's still fine, however, since the sockets are apparently not leaked.
60-
assert conn_count_end <= conn_count_start # system resources are released

packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ def test_bigquery_magic_query_variable_not_identifier():
29742974
# considered a table name, thus we expect an error that the table ID is not valid.
29752975
output = captured_io.stderr
29762976
assert "ERROR:" in output
2977-
assert "must be a fully-qualified ID" in output
2977+
assert "table_id" in output
29782978

29792979

29802980
@pytest.mark.usefixtures("mock_credentials")

packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ def test_query_with_bigframes_warning(mock_ipython):
5454
def test_cell_magic_engine_bigframes_warning(mock_ipython):
5555
from unittest import mock
5656

57-
from IPython.testing.globalipapp import get_ipython
57+
from IPython.testing.globalipapp import get_ipython, start_ipython
5858

59+
start_ipython()
5960
ip = get_ipython()
60-
if ip is None:
61-
from IPython.testing.globalipapp import start_ipython
62-
63-
ip = start_ipython()
6461

6562
ip.extension_manager.load_extension("bigquery_magics")
6663

0 commit comments

Comments
 (0)