Skip to content

Commit 43a938c

Browse files
committed
feat: Add test case for STRUCT column in anywidget
Adds a test case to verify that a DataFrame with a STRUCT column is correctly displayed in anywidget mode. This test confirms that displaying a STRUCT column does not raise an exception that would trigger the fallback to the deferred representation. It mocks `IPython.display.display` to capture the `TableWidget` instance and asserts that the rendered HTML contains the expected string representation of the STRUCT data.
1 parent 088a089 commit 43a938c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/system/small/test_anywidget.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,47 @@ def test_sql_anywidget_mode(mock_table_widget, session: bf.Session):
442442
mock_table_widget.assert_called_once()
443443

444444

445+
@mock.patch("IPython.display.display")
446+
def test_struct_column_anywidget_mode(mock_display, session: bf.Session):
447+
"""
448+
Test that a DataFrame with a STRUCT column is displayed in anywidget mode
449+
and does not fall back to the deferred representation. This confirms that
450+
anywidget can handle complex types without raising an exception that would
451+
trigger the fallback mechanism.
452+
"""
453+
pandas_df = pd.DataFrame(
454+
{
455+
"a": [1],
456+
"b": [{"c": 2, "d": 3}],
457+
}
458+
)
459+
bf_df = session.read_pandas(pandas_df)
460+
461+
with bf.option_context("display.repr_mode", "anywidget"):
462+
with mock.patch(
463+
"bigframes.dataframe.formatter.repr_query_job"
464+
) as mock_repr_query_job:
465+
# Trigger the display logic.
466+
result = bf_df._repr_html_()
467+
468+
# Assert that we did NOT fall back to the deferred representation.
469+
mock_repr_query_job.assert_not_called()
470+
471+
# Assert that display was called with a TableWidget
472+
mock_display.assert_called_once()
473+
widget = mock_display.call_args[0][0]
474+
from bigframes.display import TableWidget
475+
476+
assert isinstance(widget, TableWidget)
477+
478+
# Assert that the widget's html contains the struct
479+
html = widget.table_html
480+
assert "{'c': 2, 'd': 3}" in html
481+
482+
# Assert that _repr_html_ returns an empty string
483+
assert result == ""
484+
485+
445486
# TODO(shuowei): Add tests for custom index and multiindex
446487
# This may not be necessary for the SQL Cell use case but should be
447488
# considered for completeness.

0 commit comments

Comments
 (0)