This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathtest_api.py
More file actions
80 lines (67 loc) · 3.22 KB
/
test_api.py
File metadata and controls
80 lines (67 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
import pytest
import bigframes.core.global_session
import bigframes.dataframe
import bigframes.pandas.io.api as bf_io_api
import bigframes.session
# _read_gbq_colab requires the polars engine.
pytest.importorskip("polars")
def test_read_gbq_colab_dry_run_doesnt_call_set_location():
"""
Ensure that we don't bind to a location too early. If it's a dry run, the
user might not be done typing.
"""
with mock.patch(
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
) as mock_set_location, mock.patch(
"bigframes.core.global_session.with_default_session"
) as mock_with_default_session, bigframes.core.global_session._global_session_lock:
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
mock_with_default_session.return_value = mock_df
query_or_table = "SELECT {param1} AS param1"
sample_pyformat_args = {"param1": "value1"}
bf_io_api._read_gbq_colab(
query_or_table, pyformat_args=sample_pyformat_args, dry_run=True
)
mock_with_default_session.assert_not_called()
mock_set_location.assert_not_called()
def test_read_gbq_colab_calls_set_location():
with mock.patch(
"bigframes.pandas.io.api._set_default_session_location_if_possible_deferred_query"
) as mock_set_location, mock.patch(
"bigframes.core.global_session.with_default_session"
) as mock_with_default_session, bigframes.core.global_session._global_session_lock:
# Configure the mock for with_default_session to return a DataFrame mock
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame)
mock_with_default_session.return_value = mock_df
query_or_table = "SELECT {param1} AS param1"
sample_pyformat_args = {"param1": "value1"}
result = bf_io_api._read_gbq_colab(
query_or_table, pyformat_args=sample_pyformat_args, dry_run=False
)
# Make sure that we format the SQL first to prevent syntax errors.
formatted_query = "SELECT 'value1' AS param1"
mock_set_location.assert_called_once()
args, _ = mock_set_location.call_args
assert formatted_query == args[0]()
mock_with_default_session.assert_called_once()
# Check the actual arguments passed to with_default_session
args, kwargs = mock_with_default_session.call_args
assert args[0] == bigframes.session.Session._read_gbq_colab
assert args[1] == query_or_table
assert kwargs["pyformat_args"] == sample_pyformat_args
assert not kwargs["dry_run"]
assert isinstance(result, bigframes.dataframe.DataFrame)