|
| 1 | +# Copyright 2025 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 pandas as pd |
| 18 | +import pytest |
| 19 | + |
| 20 | +from bigframes.core import blocks |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_session(): |
| 25 | + session = mock.MagicMock() |
| 26 | + session.bqclient = None |
| 27 | + return session |
| 28 | + |
| 29 | + |
| 30 | +def test_pd_index_to_array_value_with_empty_index_creates_columns(mock_session): |
| 31 | + """ |
| 32 | + Tests that `_pd_index_to_array_value` correctly handles an empty pandas Index by creating |
| 33 | + an ArrayValue with the expected columns (index column + offset column). |
| 34 | + This prevents crashes in `unpivot` which expects these columns to exist. |
| 35 | + """ |
| 36 | + empty_index = pd.Index([], name="test") |
| 37 | + |
| 38 | + array_val = blocks._pd_index_to_array_value(mock_session, empty_index) |
| 39 | + |
| 40 | + # Should be 2: one for index, one for offset |
| 41 | + assert len(array_val.column_ids) == 2 |
| 42 | + |
| 43 | + |
| 44 | +def test_pd_index_to_array_value_with_empty_multiindex_creates_columns(mock_session): |
| 45 | + """ |
| 46 | + Tests that `_pd_index_to_array_value` correctly handles an empty pandas MultiIndex by creating |
| 47 | + an ArrayValue with the expected columns (one for each level + offset column). |
| 48 | + """ |
| 49 | + empty_index = pd.MultiIndex.from_arrays([[], []], names=["a", "b"]) |
| 50 | + |
| 51 | + array_val = blocks._pd_index_to_array_value(mock_session, empty_index) |
| 52 | + |
| 53 | + # Should have 3 columns: a, b, offset |
| 54 | + assert len(array_val.column_ids) == 3 |
0 commit comments