Skip to content

Commit c10d7eb

Browse files
committed
feat: add tests for new parameters and validation in DataFrameHtmlFormatter
1 parent 174374a commit c10d7eb

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

python/tests/test_dataframe.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,3 +1647,86 @@ def test_html_formatter_manual_format_html(clean_formatter_state):
16471647

16481648
assert "<style>" in local_html_1
16491649
assert "<style>" in local_html_2
1650+
1651+
1652+
def test_html_formatter_memory_and_row_parameters():
1653+
"""Test the memory and row control parameters in DataFrameHtmlFormatter."""
1654+
1655+
# Test default values
1656+
formatter = DataFrameHtmlFormatter()
1657+
assert formatter.max_memory_bytes == 2 * 1024 * 1024 # 2 MB
1658+
assert formatter.min_rows_display == 20
1659+
assert formatter.repr_rows == 10
1660+
1661+
# Test custom values
1662+
formatter = DataFrameHtmlFormatter(
1663+
max_memory_bytes=1024 * 1024, # 1 MB
1664+
min_rows_display=10,
1665+
repr_rows=5
1666+
)
1667+
assert formatter.max_memory_bytes == 1024 * 1024
1668+
assert formatter.min_rows_display == 10
1669+
assert formatter.repr_rows == 5
1670+
1671+
1672+
def test_html_formatter_memory_and_row_validation():
1673+
"""Test validation for the memory and row control parameters in DataFrameHtmlFormatter."""
1674+
1675+
# Test invalid max_memory_bytes
1676+
with pytest.raises(ValueError, match="max_memory_bytes must be a positive integer"):
1677+
DataFrameHtmlFormatter(max_memory_bytes=0)
1678+
1679+
with pytest.raises(ValueError, match="max_memory_bytes must be a positive integer"):
1680+
DataFrameHtmlFormatter(max_memory_bytes=-100)
1681+
1682+
# Test invalid min_rows_display
1683+
with pytest.raises(ValueError, match="min_rows_display must be a positive integer"):
1684+
DataFrameHtmlFormatter(min_rows_display=0)
1685+
1686+
with pytest.raises(ValueError, match="min_rows_display must be a positive integer"):
1687+
DataFrameHtmlFormatter(min_rows_display=-5)
1688+
1689+
# Test invalid repr_rows
1690+
with pytest.raises(ValueError, match="repr_rows must be a positive integer"):
1691+
DataFrameHtmlFormatter(repr_rows=0)
1692+
1693+
with pytest.raises(ValueError, match="repr_rows must be a positive integer"):
1694+
DataFrameHtmlFormatter(repr_rows=-10)
1695+
1696+
1697+
def test_html_formatter_custom_style_provider_with_parameters(df, clean_formatter_state):
1698+
"""Test using custom style providers with the HTML formatter and configured parameters."""
1699+
1700+
class CustomStyleProvider:
1701+
def get_cell_style(self) -> str:
1702+
return (
1703+
"background-color: #f5f5f5; color: #333; padding: 8px; border: "
1704+
"1px solid #ddd;"
1705+
)
1706+
1707+
def get_header_style(self) -> str:
1708+
return (
1709+
"background-color: #4285f4; color: white; font-weight: bold; "
1710+
"padding: 10px; border: 1px solid #3367d6;"
1711+
)
1712+
1713+
# Configure with custom style provider and memory/row parameters
1714+
configure_formatter(
1715+
style_provider=CustomStyleProvider(),
1716+
max_memory_bytes=3 * 1024 * 1024, # 3 MB
1717+
min_rows_display=15,
1718+
repr_rows=7
1719+
)
1720+
1721+
html_output = df._repr_html_()
1722+
1723+
# Verify our custom styles were applied
1724+
assert "background-color: #4285f4" in html_output
1725+
assert "color: white" in html_output
1726+
assert "background-color: #f5f5f5" in html_output
1727+
1728+
# Test memory and row parameters
1729+
formatter = get_formatter()
1730+
assert formatter.max_memory_bytes == 3 * 1024 * 1024 # 3 MB
1731+
assert formatter.min_rows_display == 15
1732+
assert formatter.repr_rows == 7

0 commit comments

Comments
 (0)