|
16 | 16 |
|
17 | 17 | import pytest |
18 | 18 |
|
| 19 | +import bigframes.core.global_session |
19 | 20 | import bigframes.dataframe |
20 | 21 | import bigframes.pandas.io.api as bf_io_api |
21 | 22 | import bigframes.session |
|
24 | 25 | pytest.importorskip("polars") |
25 | 26 |
|
26 | 27 |
|
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(): |
34 | 29 | """ |
35 | 30 | Ensure that we don't bind to a location too early. If it's a dry run, the |
36 | 31 | user might not be done typing. |
37 | 32 | """ |
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 |
40 | 40 |
|
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 | + ) |
46 | 46 |
|
47 | | - mock_set_location.assert_not_called() |
| 47 | + mock_with_default_session.assert_not_called() |
| 48 | + mock_set_location.assert_not_called() |
48 | 49 |
|
49 | 50 |
|
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 |
60 | 60 |
|
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 | + ) |
66 | 66 |
|
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() |
73 | 73 |
|
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) |
0 commit comments