|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +import bigframes.dataframe |
| 18 | +import bigframes.pandas.io.api as bf_io_api |
| 19 | +import bigframes.session |
| 20 | + |
| 21 | + |
| 22 | +@mock.patch("bigframes.pandas.io.api._set_default_session_location_if_possible") |
| 23 | +@mock.patch("bigframes.core.global_session.with_default_session") |
| 24 | +def test_read_gbq_colab_calls_set_location( |
| 25 | + mock_with_default_session, mock_set_location |
| 26 | +): |
| 27 | + # Configure the mock for with_default_session to return a DataFrame mock |
| 28 | + mock_df = mock.create_autospec(bigframes.dataframe.DataFrame) |
| 29 | + mock_with_default_session.return_value = mock_df |
| 30 | + |
| 31 | + query_or_table = "SELECT {param1} AS param1" |
| 32 | + sample_pyformat_args = {"param1": "value1"} |
| 33 | + result = bf_io_api._read_gbq_colab( |
| 34 | + query_or_table, pyformat_args=sample_pyformat_args, dry_run=False |
| 35 | + ) |
| 36 | + |
| 37 | + # Make sure that we format the SQL first to prevent syntax errors. |
| 38 | + formatted_query = "SELECT 'value1' AS param1" |
| 39 | + mock_set_location.assert_called_once_with(formatted_query) |
| 40 | + mock_with_default_session.assert_called_once() |
| 41 | + |
| 42 | + # Check the actual arguments passed to with_default_session |
| 43 | + args, kwargs = mock_with_default_session.call_args |
| 44 | + assert args[0] == bigframes.session.Session._read_gbq_colab |
| 45 | + assert args[1] == query_or_table |
| 46 | + assert kwargs["pyformat_args"] == sample_pyformat_args |
| 47 | + assert not kwargs["dry_run"] |
| 48 | + assert isinstance(result, bigframes.dataframe.DataFrame) |
0 commit comments