|
| 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 | +import math |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import bigframes as bf |
| 20 | + |
| 21 | + |
| 22 | +def test_repr_anywidget_initial_state( |
| 23 | + penguins_df_default_index: bf.dataframe.DataFrame, |
| 24 | +): |
| 25 | + pytest.importorskip("anywidget") |
| 26 | + with bf.option_context("display.repr_mode", "anywidget"): |
| 27 | + from bigframes.display import TableWidget |
| 28 | + |
| 29 | + widget = TableWidget(penguins_df_default_index) |
| 30 | + assert widget.page == 0 |
| 31 | + assert widget.page_size == bf.options.display.max_rows |
| 32 | + assert widget.row_count > 0 |
| 33 | + |
| 34 | + |
| 35 | +def test_repr_anywidget_pagination_navigation( |
| 36 | + penguins_df_default_index: bf.dataframe.DataFrame, |
| 37 | +): |
| 38 | + """Test basic prev/next navigation functionality.""" |
| 39 | + pytest.importorskip("anywidget") |
| 40 | + with bf.option_context("display.repr_mode", "anywidget"): |
| 41 | + from bigframes.display.anywidget import TableWidget |
| 42 | + |
| 43 | + widget = TableWidget(penguins_df_default_index) |
| 44 | + |
| 45 | + # Test initial state |
| 46 | + assert widget.page == 0 |
| 47 | + |
| 48 | + # Simulate next page click |
| 49 | + widget.page = 1 |
| 50 | + assert widget.page == 1 |
| 51 | + |
| 52 | + # Simulate prev page click |
| 53 | + widget.page = 0 |
| 54 | + assert widget.page == 0 |
| 55 | + |
| 56 | + |
| 57 | +def test_repr_anywidget_pagination_edge_cases( |
| 58 | + penguins_df_default_index: bf.dataframe.DataFrame, |
| 59 | +): |
| 60 | + """Test pagination at boundaries.""" |
| 61 | + pytest.importorskip("anywidget") |
| 62 | + with bf.option_context("display.repr_mode", "anywidget"): |
| 63 | + from bigframes.display.anywidget import TableWidget |
| 64 | + |
| 65 | + widget = TableWidget(penguins_df_default_index) |
| 66 | + |
| 67 | + # Test going below page 0 |
| 68 | + widget.page = -1 |
| 69 | + # Should stay at 0 (handled by frontend) |
| 70 | + |
| 71 | + # Test going beyond last page |
| 72 | + total_pages = math.ceil(widget.row_count / widget.page_size) |
| 73 | + widget.page = total_pages + 1 |
| 74 | + # Should be clamped to last valid page |
| 75 | + |
| 76 | + |
| 77 | +def test_repr_anywidget_pagination_different_page_sizes( |
| 78 | + penguins_df_default_index: bf.dataframe.DataFrame, |
| 79 | +): |
| 80 | + """Test pagination with different page sizes.""" |
| 81 | + pytest.importorskip("anywidget") |
| 82 | + |
| 83 | + # Test with smaller page size |
| 84 | + with bf.option_context("display.repr_mode", "anywidget", "display.max_rows", 5): |
| 85 | + from bigframes.display.anywidget import TableWidget |
| 86 | + |
| 87 | + widget = TableWidget(penguins_df_default_index) |
| 88 | + |
| 89 | + assert widget.page_size == 5 |
| 90 | + total_pages = math.ceil(widget.row_count / 5) |
| 91 | + assert total_pages > 1 # Should have multiple pages |
| 92 | + |
| 93 | + # Navigate through several pages |
| 94 | + for page in range(min(3, total_pages)): |
| 95 | + widget.page = page |
| 96 | + assert widget.page == page |
| 97 | + |
| 98 | + |
| 99 | +def test_repr_anywidget_pagination_buttons_functionality( |
| 100 | + penguins_df_default_index: bf.dataframe.DataFrame, |
| 101 | +): |
| 102 | + """Test complete pagination button functionality.""" |
| 103 | + pytest.importorskip("anywidget") |
| 104 | + with bf.option_context("display.repr_mode", "anywidget", "display.max_rows", 10): |
| 105 | + from bigframes.display.anywidget import TableWidget |
| 106 | + |
| 107 | + widget = TableWidget(penguins_df_default_index) |
| 108 | + |
| 109 | + # Test initial state |
| 110 | + assert widget.page == 0 |
| 111 | + assert widget.page_size == 10 |
| 112 | + assert widget.row_count > 0 |
| 113 | + |
| 114 | + # Calculate expected pages |
| 115 | + total_pages = math.ceil(widget.row_count / widget.page_size) |
| 116 | + |
| 117 | + # Test navigation through all pages |
| 118 | + for page_num in range(min(total_pages, 5)): # Test first 5 pages |
| 119 | + widget.page = page_num |
| 120 | + assert widget.page == page_num |
| 121 | + # Verify table_html is updated |
| 122 | + assert len(widget.table_html) > 0 |
0 commit comments