1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import traceback
16-
1715import pandas as pd
1816import pytest
1917
2220pytest .importorskip ("anywidget" )
2321
2422
25- @pytest .fixture (scope = "module" , autouse = True )
26- def cleanup_session (session ):
27- """Ensure comprehensive cleanup happens after all tests in this module."""
28- yield
29- try :
30- # Force cleanup of anonymous dataset and all temporary tables
31- if hasattr (session , "_anon_dataset_manager" ) and session ._anon_dataset_manager :
32- session ._anon_dataset_manager .close ()
33-
34- # Also call the main session cleanup
35- session .close ()
36- except Exception as e :
37- traceback .print_exception (type (e ), e , None )
38-
39-
4023@pytest .fixture (scope = "module" )
4124def paginated_pandas_df () -> pd .DataFrame :
4225 """Create a test DataFrame with exactly 3 pages of manually defined data."""
26+ """Create a minimal test DataFrame with exactly 3 pages of 2 rows each."""
4327 test_data = pd .DataFrame (
4428 {
45- "id" : [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ],
29+ "id" : [0 , 1 , 2 , 3 , 4 , 5 ],
4630 "page_indicator" : [
47- # Page 1 (rows 1-5 )
31+ # Page 1 (rows 1-2 )
4832 "page_1_row_1" ,
4933 "page_1_row_2" ,
50- "page_1_row_3" ,
51- "page_1_row_4" ,
52- "page_1_row_5" ,
53- # Page 2 (rows 6-10)
34+ # Page 2 (rows 3-4)
5435 "page_2_row_1" ,
5536 "page_2_row_2" ,
56- "page_2_row_3" ,
57- "page_2_row_4" ,
58- "page_2_row_5" ,
59- # Page 3 (rows 11-15)
37+ # Page 3 (rows 5-6)
6038 "page_3_row_1" ,
6139 "page_3_row_2" ,
62- "page_3_row_3" ,
63- "page_3_row_4" ,
64- "page_3_row_5" ,
65- ],
66- "value" : [
67- "data_001" ,
68- "data_002" ,
69- "data_003" ,
70- "data_004" ,
71- "data_005" ,
72- "data_006" ,
73- "data_007" ,
74- "data_008" ,
75- "data_009" ,
76- "data_010" ,
77- "data_011" ,
78- "data_012" ,
79- "data_013" ,
80- "data_014" ,
81- "data_015" ,
8240 ],
41+ "value" : [0 , 1 , 2 , 3 , 4 , 5 ],
8342 }
8443 )
8544 return test_data
@@ -100,7 +59,7 @@ def table_widget(paginated_bf_df: bf.dataframe.DataFrame):
10059 """
10160 from bigframes .display import TableWidget
10261
103- with bf .option_context ("display.repr_mode" , "anywidget" , "display.max_rows" , 5 ):
62+ with bf .option_context ("display.repr_mode" , "anywidget" , "display.max_rows" , 2 ):
10463 widget = TableWidget (paginated_bf_df )
10564 return widget
10665
@@ -117,18 +76,14 @@ def _assert_html_matches_pandas_slice(
11776 """
11877 # Check that the unique indicator from each expected row is present.
11978 for _ , row in expected_pd_slice .iterrows ():
120- assert (
121- row ["page_indicator" ] in table_html
122- ), f"Expected row '{ row ['page_indicator' ]} ' to be in the table HTML."
79+ assert row ["page_indicator" ] in table_html
12380
12481 # Create a DataFrame of all rows that should NOT be present.
12582 unexpected_pd_df = full_pd_df .drop (expected_pd_slice .index )
12683
12784 # Check that no unique indicators from unexpected rows are present.
12885 for _ , row in unexpected_pd_df .iterrows ():
129- assert (
130- row ["page_indicator" ] not in table_html
131- ), f"Expected row '{ row ['page_indicator' ]} ' NOT to be in the table HTML."
86+ assert row ["page_indicator" ] not in table_html
13287
13388
13489def test_repr_anywidget_initialization_set_correct_defaults (
@@ -143,21 +98,17 @@ def test_repr_anywidget_initialization_set_correct_defaults(
14398
14499 widget = TableWidget (paginated_bf_df )
145100
146- assert widget .page == 0 , "Initial page should be 0."
147- assert (
148- widget .page_size == bf .options .display .max_rows
149- ), "Page size should default to max_rows option."
150- assert widget .row_count == len (
151- paginated_pandas_df
152- ), "Row count should match the source DataFrame."
101+ assert widget .page == 0
102+ assert widget .page_size == bf .options .display .max_rows
103+ assert widget .row_count == len (paginated_pandas_df )
153104
154105
155106def test_repr_anywidget_display_first_page_on_load (table_widget , paginated_pandas_df ):
156107 """
157108 Given a widget, when it is first loaded, then it should display
158109 the first page of data.
159110 """
160- expected_slice = paginated_pandas_df .iloc [0 :5 ]
111+ expected_slice = paginated_pandas_df .iloc [0 :2 ]
161112
162113 html = table_widget .table_html
163114
@@ -169,7 +120,7 @@ def test_repr_anywidget_navigate_to_second_page(table_widget, paginated_pandas_d
169120 Given a widget, when the page is set to 1, then it should display
170121 the second page of data.
171122 """
172- expected_slice = paginated_pandas_df .iloc [5 : 10 ]
123+ expected_slice = paginated_pandas_df .iloc [2 : 4 ]
173124
174125 table_widget .page = 1
175126 html = table_widget .table_html
@@ -183,7 +134,7 @@ def test_repr_anywidget_navigate_to_last_page(table_widget, paginated_pandas_df)
183134 Given a widget, when the page is set to the last page (2),
184135 then it should display the final page of data.
185136 """
186- expected_slice = paginated_pandas_df .iloc [10 : 15 ]
137+ expected_slice = paginated_pandas_df .iloc [4 : 6 ]
187138
188139 table_widget .page = 2
189140 html = table_widget .table_html
@@ -199,12 +150,12 @@ def test_repr_anywidget_page_clamp_to_zero_for_negative_input(
199150 Given a widget, when a negative page number is set,
200151 then the page number should be clamped to 0 and display the first page.
201152 """
202- expected_slice = paginated_pandas_df .iloc [0 :5 ]
153+ expected_slice = paginated_pandas_df .iloc [0 :2 ]
203154
204155 table_widget .page = - 1
205156 html = table_widget .table_html
206157
207- assert table_widget .page == 0 , "Page should be clamped to 0."
158+ assert table_widget .page == 0
208159 _assert_html_matches_pandas_slice (html , expected_slice , paginated_pandas_df )
209160
210161
@@ -215,38 +166,31 @@ def test_repr_anywidget_page_clamp_to_last_page_for_out_of_bounds_input(
215166 Given a widget, when a page number greater than the max is set,
216167 then the page number should be clamped to the last valid page.
217168 """
218- expected_slice = paginated_pandas_df .iloc [10 : 15 ]
169+ expected_slice = paginated_pandas_df .iloc [4 : 6 ]
219170
220171 table_widget .page = 100
221172 html = table_widget .table_html
222173
223- assert table_widget .page == 2 , "Page should be clamped to the last valid page."
174+ assert table_widget .page == 2
224175 _assert_html_matches_pandas_slice (html , expected_slice , paginated_pandas_df )
225176
226177
227178@pytest .mark .parametrize (
228179 "page, start_row, end_row" ,
229180 [
230- (0 , 0 , 3 ),
231- (1 , 3 , 6 ),
232- (2 , 6 , 9 ),
233- (3 , 9 , 12 ),
234- (4 , 12 , 15 ),
181+ (0 , 0 , 3 ), # Page 0: rows 0-2
182+ (1 , 3 , 6 ), # Page 1: rows 3-5
235183 ],
236184 ids = [
237- "Page 0 (Rows 1-3)" ,
238- "Page 1 (Rows 4-6)" ,
239- "Page 2 (Rows 7-9)" ,
240- "Page 3 (Rows 10-12)" ,
241- "Page 4 (Rows 13-15)" ,
185+ "Page 0 (Rows 0-2)" ,
186+ "Page 1 (Rows 3-5)" ,
242187 ],
243188)
244189def test_repr_anywidget_paginate_correctly_with_custom_page_size (
245190 paginated_bf_df , paginated_pandas_df , page , start_row , end_row
246191):
247192 """
248193 A widget should paginate correctly with a custom page size of 3.
249- This uses pytest parameterization, a strong pattern from the examples.
250194 """
251195 with bf .option_context ("display.repr_mode" , "anywidget" , "display.max_rows" , 3 ):
252196 from bigframes .display import TableWidget
0 commit comments