41
41
)
42
42
from pyarrow .csv import write_csv
43
43
44
- MB = 1024 * 1024
44
+ MB = 1024 * 1024
45
+
46
+
45
47
@pytest .fixture
46
48
def ctx ():
47
49
return SessionContext ()
@@ -116,6 +118,7 @@ def clean_formatter_state():
116
118
"""Reset the HTML formatter after each test."""
117
119
reset_formatter ()
118
120
121
+
119
122
# custom style for testing with html formatter
120
123
class CustomStyleProvider :
121
124
def get_cell_style (self ) -> str :
@@ -130,17 +133,17 @@ def get_header_style(self) -> str:
130
133
"padding: 10px; border: 1px solid #3367d6;"
131
134
)
132
135
136
+
133
137
def count_table_rows (html_content : str ) -> int :
134
138
"""Count the number of table rows in HTML content.
135
-
136
139
Args:
137
140
html_content: HTML string to analyze
138
-
139
141
Returns:
140
142
Number of table rows found (number of <tr> tags)
141
143
"""
142
144
return len (re .findall (r"<tr" , html_content ))
143
145
146
+
144
147
def test_select (df ):
145
148
df_1 = df .select (
146
149
column ("a" ) + column ("b" ),
@@ -929,23 +932,17 @@ def get_header_style(self) -> str:
929
932
930
933
def test_html_formatter_memory (df , clean_formatter_state ):
931
934
"""Test the memory and row control parameters in DataFrameHtmlFormatter."""
932
- configure_formatter (
933
- max_memory_bytes = 10 ,
934
- min_rows_display = 1
935
- )
935
+ configure_formatter (max_memory_bytes = 10 , min_rows_display = 1 )
936
936
html_output = df ._repr_html_ ()
937
-
937
+
938
938
# Count the number of table rows in the output
939
939
tr_count = count_table_rows (html_output )
940
940
# With a tiny memory limit of 10 bytes, the formatter should display
941
941
# the minimum number of rows (1) plus a message about truncation
942
942
assert tr_count == 2 # 1 for header row, 1 for data row
943
- assert "data truncated" in html_output .lower ()
944
-
945
- configure_formatter (
946
- max_memory_bytes = 10 * MB ,
947
- min_rows_display = 1
948
- )
943
+ assert "data truncated" in html_output .lower ()
944
+
945
+ configure_formatter (max_memory_bytes = 10 * MB , min_rows_display = 1 )
949
946
html_output = df ._repr_html_ ()
950
947
# With larger memory limit and min_rows=2, should display all rows
951
948
tr_count = count_table_rows (html_output )
@@ -954,40 +951,35 @@ def test_html_formatter_memory(df, clean_formatter_state):
954
951
# No truncation message should appear
955
952
assert "data truncated" not in html_output .lower ()
956
953
954
+
957
955
def test_html_formatter_repr_rows (df , clean_formatter_state ):
958
- configure_formatter (
959
- min_rows_display = 2 ,
960
- repr_rows = 2
961
- )
956
+ configure_formatter (min_rows_display = 2 , repr_rows = 2 )
962
957
html_output = df ._repr_html_ ()
963
-
958
+
964
959
tr_count = count_table_rows (html_output )
965
960
# Tabe should have header row (1) + 2 data rows = 3 rows
966
961
assert tr_count == 3
967
-
968
- configure_formatter (
969
- min_rows_display = 2 ,
970
- repr_rows = 3
971
- )
962
+
963
+ configure_formatter (min_rows_display = 2 , repr_rows = 3 )
972
964
html_output = df ._repr_html_ ()
973
-
965
+
974
966
tr_count = count_table_rows (html_output )
975
967
# Tabe should have header row (1) + 3 data rows = 4 rows
976
968
assert tr_count == 4
977
-
978
-
969
+
970
+
979
971
def test_html_formatter_validation ():
980
972
# Test validation for invalid parameters
981
-
973
+
982
974
with pytest .raises (ValueError , match = "max_cell_length must be a positive integer" ):
983
975
DataFrameHtmlFormatter (max_cell_length = 0 )
984
-
976
+
985
977
with pytest .raises (ValueError , match = "max_width must be a positive integer" ):
986
978
DataFrameHtmlFormatter (max_width = 0 )
987
-
979
+
988
980
with pytest .raises (ValueError , match = "max_height must be a positive integer" ):
989
981
DataFrameHtmlFormatter (max_height = 0 )
990
-
982
+
991
983
with pytest .raises (ValueError , match = "max_memory_bytes must be a positive integer" ):
992
984
DataFrameHtmlFormatter (max_memory_bytes = 0 )
993
985
@@ -1012,51 +1004,51 @@ def test_configure_formatter(df, clean_formatter_state):
1012
1004
parameters."""
1013
1005
1014
1006
# these are non-default values
1015
- MAX_CELL_LENGTH = 10
1016
- MAX_WIDTH = 500
1017
- MAX_HEIGHT = 30
1018
- MAX_MEMORY_BYTES = 3 * MB
1019
- MIN_ROWS_DISPLAY = 2
1020
- REPR_ROWS = 2
1021
- ENABLE_CELL_EXPANSION = False
1022
- SHOW_TRUNCATION_MESSAGE = False
1023
- USE_SHARED_STYLES = False
1024
-
1007
+ max_cell_length = 10
1008
+ max_width = 500
1009
+ max_height = 30
1010
+ max_memory_bytes = 3 * MB
1011
+ min_rows_display = 2
1012
+ repr_rows = 2
1013
+ enable_cell_expansion = False
1014
+ show_truncation_message = False
1015
+ use_shared_styles = False
1016
+
1025
1017
reset_formatter ()
1026
1018
formatter_default = get_formatter ()
1027
-
1028
- assert formatter_default .max_cell_length != MAX_CELL_LENGTH
1029
- assert formatter_default .max_width != MAX_WIDTH
1030
- assert formatter_default .max_height != MAX_HEIGHT
1031
- assert formatter_default .max_memory_bytes != MAX_MEMORY_BYTES
1032
- assert formatter_default .min_rows_display != MIN_ROWS_DISPLAY
1033
- assert formatter_default .repr_rows != REPR_ROWS
1034
- assert formatter_default .enable_cell_expansion != ENABLE_CELL_EXPANSION
1035
- assert formatter_default .show_truncation_message != SHOW_TRUNCATION_MESSAGE
1036
- assert formatter_default .use_shared_styles != USE_SHARED_STYLES
1037
-
1019
+
1020
+ assert formatter_default .max_cell_length != max_cell_length
1021
+ assert formatter_default .max_width != max_width
1022
+ assert formatter_default .max_height != max_height
1023
+ assert formatter_default .max_memory_bytes != max_memory_bytes
1024
+ assert formatter_default .min_rows_display != min_rows_display
1025
+ assert formatter_default .repr_rows != repr_rows
1026
+ assert formatter_default .enable_cell_expansion != enable_cell_expansion
1027
+ assert formatter_default .show_truncation_message != show_truncation_message
1028
+ assert formatter_default .use_shared_styles != use_shared_styles
1029
+
1038
1030
# Configure with custom style provider and additional parameters
1039
1031
configure_formatter (
1040
- max_cell_length = MAX_CELL_LENGTH ,
1041
- max_width = MAX_WIDTH ,
1042
- max_height = MAX_HEIGHT ,
1043
- max_memory_bytes = MAX_MEMORY_BYTES ,
1044
- min_rows_display = MIN_ROWS_DISPLAY ,
1045
- repr_rows = REPR_ROWS ,
1046
- enable_cell_expansion = ENABLE_CELL_EXPANSION ,
1047
- show_truncation_message = SHOW_TRUNCATION_MESSAGE ,
1048
- use_shared_styles = USE_SHARED_STYLES
1032
+ max_cell_length = max_cell_length ,
1033
+ max_width = max_width ,
1034
+ max_height = max_height ,
1035
+ max_memory_bytes = max_memory_bytes ,
1036
+ min_rows_display = min_rows_display ,
1037
+ repr_rows = repr_rows ,
1038
+ enable_cell_expansion = enable_cell_expansion ,
1039
+ show_truncation_message = show_truncation_message ,
1040
+ use_shared_styles = use_shared_styles ,
1049
1041
)
1050
1042
formatter_custom = get_formatter ()
1051
- assert formatter_custom .max_cell_length == MAX_CELL_LENGTH
1052
- assert formatter_custom .max_width == MAX_WIDTH
1053
- assert formatter_custom .max_height == MAX_HEIGHT
1054
- assert formatter_custom .max_memory_bytes == MAX_MEMORY_BYTES
1055
- assert formatter_custom .min_rows_display == MIN_ROWS_DISPLAY
1056
- assert formatter_custom .repr_rows == REPR_ROWS
1057
- assert formatter_custom .enable_cell_expansion == ENABLE_CELL_EXPANSION
1058
- assert formatter_custom .show_truncation_message == SHOW_TRUNCATION_MESSAGE
1059
- assert formatter_custom .use_shared_styles == USE_SHARED_STYLES
1043
+ assert formatter_custom .max_cell_length == max_cell_length
1044
+ assert formatter_custom .max_width == max_width
1045
+ assert formatter_custom .max_height == max_height
1046
+ assert formatter_custom .max_memory_bytes == max_memory_bytes
1047
+ assert formatter_custom .min_rows_display == min_rows_display
1048
+ assert formatter_custom .repr_rows == repr_rows
1049
+ assert formatter_custom .enable_cell_expansion == enable_cell_expansion
1050
+ assert formatter_custom .show_truncation_message == show_truncation_message
1051
+ assert formatter_custom .use_shared_styles == use_shared_styles
1060
1052
1061
1053
1062
1054
def test_get_dataframe (tmp_path ):
@@ -1788,4 +1780,3 @@ def test_html_formatter_manual_format_html(clean_formatter_state):
1788
1780
1789
1781
assert "<style>" in local_html_1
1790
1782
assert "<style>" in local_html_2
1791
-
0 commit comments