|
24 | 24 | # Test constants to avoid change detector tests |
25 | 25 | EXPECTED_ROW_COUNT = 6 |
26 | 26 | EXPECTED_PAGE_SIZE = 2 |
27 | | -EXPECTED_TOTAL_PAGES = 3 |
28 | 27 |
|
29 | 28 |
|
30 | 29 | @pytest.fixture(scope="module") |
@@ -111,6 +110,21 @@ def empty_bf_df( |
111 | 110 | return session.read_pandas(empty_pandas_df) |
112 | 111 |
|
113 | 112 |
|
| 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 | + |
114 | 128 | def _assert_html_matches_pandas_slice( |
115 | 129 | table_html: str, |
116 | 130 | expected_pd_slice: pd.DataFrame, |
@@ -420,12 +434,6 @@ def test_setting_page_size_above_max_should_be_clamped(table_widget): |
420 | 434 | # The page size is clamped to the maximum. |
421 | 435 | assert table_widget.page_size == expected_clamped_size |
422 | 436 |
|
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 | | - |
429 | 437 |
|
430 | 438 | @mock.patch("bigframes.display.TableWidget") |
431 | 439 | 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): |
483 | 491 | assert result == "" |
484 | 492 |
|
485 | 493 |
|
| 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 | + |
486 | 531 | # TODO(shuowei): Add tests for custom index and multiindex |
487 | 532 | # This may not be necessary for the SQL Cell use case but should be |
488 | 533 | # considered for completeness. |
0 commit comments