|
| 1 | +# Copyright 2026 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 | +import pytest |
| 16 | + |
| 17 | +from bigquery_magics import bigquery as magics |
| 18 | +import bigquery_magics.config |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def mock_bq_client_and_credentials(mock_credentials): |
| 23 | + from unittest import mock |
| 24 | + |
| 25 | + with mock.patch("google.cloud.bigquery.Client", autospec=True): |
| 26 | + with mock.patch("bigquery_magics.core.create_bq_client", autospec=True): |
| 27 | + yield |
| 28 | + |
| 29 | + |
| 30 | +def test_config_engine_setter_warning(): |
| 31 | + context = bigquery_magics.config.Context() |
| 32 | + with pytest.warns(FutureWarning, match="The bigframes engine is deprecated"): |
| 33 | + context.engine = "bigframes" |
| 34 | + |
| 35 | + |
| 36 | +def test_query_with_bigframes_warning(mock_ipython): |
| 37 | + # Mocking bigframes.pandas since it might not be installed |
| 38 | + from unittest import mock |
| 39 | + |
| 40 | + with mock.patch("bigquery_magics.bigquery.bpd") as mock_bpd: |
| 41 | + mock_bpd.read_gbq_query.return_value = mock.MagicMock() |
| 42 | + |
| 43 | + args = mock.MagicMock() |
| 44 | + args.engine = "bigframes" |
| 45 | + args.dry_run = False |
| 46 | + args.max_results = None |
| 47 | + args.destination_var = None |
| 48 | + args.destination_table = None |
| 49 | + |
| 50 | + with pytest.warns(FutureWarning, match="The bigframes engine is deprecated"): |
| 51 | + magics._query_with_bigframes("SELECT 1", [], args) |
| 52 | + |
| 53 | + |
| 54 | +def test_cell_magic_engine_bigframes_warning(mock_ipython): |
| 55 | + from unittest import mock |
| 56 | + |
| 57 | + from IPython.testing.globalipapp import get_ipython |
| 58 | + |
| 59 | + ip = get_ipython() |
| 60 | + if ip is None: |
| 61 | + from IPython.testing.globalipapp import start_ipython |
| 62 | + |
| 63 | + ip = start_ipython() |
| 64 | + |
| 65 | + ip.extension_manager.load_extension("bigquery_magics") |
| 66 | + |
| 67 | + # Mock the actual execution to avoid needing real credentials/data |
| 68 | + with mock.patch("bigquery_magics.bigquery.bpd") as mock_bpd: |
| 69 | + mock_bpd.read_gbq_query.return_value = mock.MagicMock() |
| 70 | + with pytest.warns(FutureWarning, match="The bigframes engine is deprecated"): |
| 71 | + ip.run_cell_magic("bigquery", "--engine bigframes", "SELECT 1") |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def mock_ipython(): |
| 76 | + from unittest import mock |
| 77 | + |
| 78 | + with mock.patch("bigquery_magics.bigquery.get_ipython") as mock_get_ipython: |
| 79 | + yield mock_get_ipython |
0 commit comments