Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 49dfe55

Browse files
committed
chore: lock global session usage in unit tests
1 parent 9af7130 commit 49dfe55

File tree

2 files changed

+50
-51
lines changed

2 files changed

+50
-51
lines changed

tests/unit/pandas/io/test_api.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818

19+
import bigframes.core.global_session
1920
import bigframes.dataframe
2021
import bigframes.pandas.io.api as bf_io_api
2122
import bigframes.session
@@ -24,57 +25,56 @@
2425
pytest.importorskip("polars")
2526

2627

27-
@mock.patch(
28-
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
29-
)
30-
@mock.patch("bigframes.core.global_session.with_default_session")
31-
def test_read_gbq_colab_dry_run_doesnt_call_set_location(
32-
mock_with_default_session, mock_set_location
33-
):
28+
def test_read_gbq_colab_dry_run_doesnt_call_set_location():
3429
"""
3530
Ensure that we don't bind to a location too early. If it's a dry run, the
3631
user might not be done typing.
3732
"""
38-
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
39-
mock_with_default_session.return_value = mock_df
33+
with mock.patch(
34+
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
35+
) as mock_set_location, mock.patch(
36+
"bigframes.core.global_session.with_default_session"
37+
) as mock_with_default_session, bigframes.core.global_session._global_session_lock:
38+
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
39+
mock_with_default_session.return_value = mock_df
4040

41-
query_or_table = "SELECT {param1} AS param1"
42-
sample_pyformat_args = {"param1": "value1"}
43-
bf_io_api._read_gbq_colab(
44-
query_or_table, pyformat_args=sample_pyformat_args, dry_run=True
45-
)
41+
query_or_table = "SELECT {param1} AS param1"
42+
sample_pyformat_args = {"param1": "value1"}
43+
bf_io_api._read_gbq_colab(
44+
query_or_table, pyformat_args=sample_pyformat_args, dry_run=True
45+
)
4646

47-
mock_set_location.assert_not_called()
47+
mock_with_default_session.assert_not_called()
48+
mock_set_location.assert_not_called()
4849

4950

50-
@mock.patch(
51-
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
52-
)
53-
@mock.patch("bigframes.core.global_session.with_default_session")
54-
def test_read_gbq_colab_calls_set_location(
55-
mock_with_default_session, mock_set_location
56-
):
57-
# Configure the mock for with_default_session to return a DataFrame mock
58-
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
59-
mock_with_default_session.return_value = mock_df
51+
def test_read_gbq_colab_calls_set_location():
52+
with mock.patch(
53+
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
54+
) as mock_set_location, mock.patch(
55+
"bigframes.core.global_session.with_default_session"
56+
) as mock_with_default_session, bigframes.core.global_session._global_session_lock:
57+
# Configure the mock for with_default_session to return a DataFrame mock
58+
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
59+
mock_with_default_session.return_value = mock_df
6060

61-
query_or_table = "SELECT {param1} AS param1"
62-
sample_pyformat_args = {"param1": "value1"}
63-
result = bf_io_api._read_gbq_colab(
64-
query_or_table, pyformat_args=sample_pyformat_args, dry_run=False
65-
)
61+
query_or_table = "SELECT {param1} AS param1"
62+
sample_pyformat_args = {"param1": "value1"}
63+
result = bf_io_api._read_gbq_colab(
64+
query_or_table, pyformat_args=sample_pyformat_args, dry_run=False
65+
)
6666

67-
# Make sure that we format the SQL first to prevent syntax errors.
68-
formatted_query = "SELECT 'value1' AS param1"
69-
mock_set_location.assert_called_once()
70-
args, _ = mock_set_location.call_args
71-
assert formatted_query == args[0]()
72-
mock_with_default_session.assert_called_once()
67+
# Make sure that we format the SQL first to prevent syntax errors.
68+
formatted_query = "SELECT 'value1' AS param1"
69+
mock_set_location.assert_called_once()
70+
args, _ = mock_set_location.call_args
71+
assert formatted_query == args[0]()
72+
mock_with_default_session.assert_called_once()
7373

74-
# Check the actual arguments passed to with_default_session
75-
args, kwargs = mock_with_default_session.call_args
76-
assert args[0] == bigframes.session.Session._read_gbq_colab
77-
assert args[1] == query_or_table
78-
assert kwargs["pyformat_args"] == sample_pyformat_args
79-
assert not kwargs["dry_run"]
80-
assert isinstance(result, bigframes.dataframe.DataFrame)
74+
# Check the actual arguments passed to with_default_session
75+
args, kwargs = mock_with_default_session.call_args
76+
assert args[0] == bigframes.session.Session._read_gbq_colab
77+
assert args[1] == query_or_table
78+
assert kwargs["pyformat_args"] == sample_pyformat_args
79+
assert not kwargs["dry_run"]
80+
assert isinstance(result, bigframes.dataframe.DataFrame)

tests/unit/test_pandas.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import pandas as pd
2121
import pytest
2222

23-
import bigframes.core.global_session
2423
import bigframes.dataframe
25-
import bigframes.pandas as bpd
24+
import bigframes.pandas
2625
import bigframes.session
2726

2827
leading_whitespace = re.compile(r"^\s+", flags=re.MULTILINE)
@@ -165,9 +164,9 @@ def test_cut_raises_with_invalid_bins(bins: int, error_message: str):
165164

166165

167166
def test_pandas_attribute():
168-
assert bpd.NA is pd.NA
169-
assert bpd.BooleanDtype is pd.BooleanDtype
170-
assert bpd.Float64Dtype is pd.Float64Dtype
171-
assert bpd.Int64Dtype is pd.Int64Dtype
172-
assert bpd.StringDtype is pd.StringDtype
173-
assert bpd.ArrowDtype is pd.ArrowDtype
167+
assert bigframes.pandas.NA is pd.NA
168+
assert bigframes.pandas.BooleanDtype is pd.BooleanDtype
169+
assert bigframes.pandas.Float64Dtype is pd.Float64Dtype
170+
assert bigframes.pandas.Int64Dtype is pd.Int64Dtype
171+
assert bigframes.pandas.StringDtype is pd.StringDtype
172+
assert bigframes.pandas.ArrowDtype is pd.ArrowDtype

0 commit comments

Comments
 (0)