Skip to content

Commit 4e181ce

Browse files
committed
tescase update
1 parent 8e31c6c commit 4e181ce

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

tests/system/small/test_anywidget.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
# Test constants to avoid change detector tests
2525
EXPECTED_ROW_COUNT = 6
2626
EXPECTED_PAGE_SIZE = 2
27-
EXPECTED_TOTAL_PAGES = 3
2827

2928

3029
@pytest.fixture(scope="module")
@@ -111,6 +110,21 @@ def empty_bf_df(
111110
return session.read_pandas(empty_pandas_df)
112111

113112

113+
@pytest.fixture(scope="module")
114+
def json_df(session: bf.Session) -> bf.dataframe.DataFrame:
115+
"""Create a DataFrame with a JSON column for testing."""
116+
import bigframes.dtypes
117+
118+
pandas_df = pd.DataFrame(
119+
{
120+
"a": [1],
121+
"b": ['{"c": 2, "d": 3}'],
122+
}
123+
)
124+
pandas_df["b"] = pandas_df["b"].astype(bigframes.dtypes.JSON_DTYPE)
125+
return session.read_pandas(pandas_df)
126+
127+
114128
def _assert_html_matches_pandas_slice(
115129
table_html: str,
116130
expected_pd_slice: pd.DataFrame,
@@ -420,12 +434,6 @@ def test_setting_page_size_above_max_should_be_clamped(table_widget):
420434
# The page size is clamped to the maximum.
421435
assert table_widget.page_size == expected_clamped_size
422436

423-
"""
424-
Test that the widget's CSS is loaded correctly.
425-
"""
426-
css_content = table_widget._css
427-
assert ".bigframes-widget .footer" in css_content
428-
429437

430438
@mock.patch("bigframes.display.TableWidget")
431439
def test_sql_anywidget_mode(mock_table_widget, session: bf.Session):
@@ -483,6 +491,43 @@ def test_struct_column_anywidget_mode(mock_display, session: bf.Session):
483491
assert result == ""
484492

485493

494+
def test_widget_creation_should_load_css_for_rendering(table_widget):
495+
"""
496+
Test that the widget's CSS is loaded correctly.
497+
"""
498+
css_content = table_widget._css
499+
assert ".bigframes-widget .footer" in css_content
500+
501+
502+
@mock.patch("IPython.display.display")
503+
def test_json_column_anywidget_mode(mock_display, json_df: bf.dataframe.DataFrame):
504+
"""
505+
Test that a DataFrame with a JSON column is displayed in anywidget mode
506+
by converting JSON to string, and does not fall back to deferred representation.
507+
"""
508+
with bf.option_context("display.repr_mode", "anywidget"):
509+
with mock.patch(
510+
"bigframes.dataframe.formatter.repr_query_job"
511+
) as mock_repr_query_job:
512+
result = json_df._repr_html_()
513+
514+
# Assert no fallback
515+
mock_repr_query_job.assert_not_called()
516+
517+
# Assert TableWidget was created and displayed
518+
mock_display.assert_called_once()
519+
widget = mock_display.call_args[0][0]
520+
from bigframes.display import TableWidget
521+
522+
assert isinstance(widget, TableWidget)
523+
524+
# Assert JSON was converted to string in the HTML
525+
html = widget.table_html
526+
assert "{"c":2,"d":3}" in html
527+
528+
assert result == ""
529+
530+
486531
# TODO(shuowei): Add tests for custom index and multiindex
487532
# This may not be necessary for the SQL Cell use case but should be
488533
# considered for completeness.

0 commit comments

Comments
 (0)