Skip to content

Commit 43158e7

Browse files
committed
refactor: streamline custom cell builder in HTML formatter tests for clarity and maintainability
1 parent 2d9b694 commit 43158e7

File tree

1 file changed

+62
-69
lines changed

1 file changed

+62
-69
lines changed

python/tests/test_dataframe.py

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -733,82 +733,75 @@ def format_int(value):
733733
def test_html_formatter_custom_cell_builder(df, clean_formatter_state):
734734
"""Test using a custom cell builder function."""
735735

736-
def test_html_formatter_custom_cell_builder(df, clean_formatter_state):
737-
"""Test using a custom cell builder function that changes style based on value."""
738-
739-
# Create a custom cell builder with distinct styling for different value ranges
740-
def custom_cell_builder(value, row, col, table_id):
741-
try:
742-
num_value = int(value)
743-
if num_value > 5: # Values > 5 get green background with indicator
744-
return f'<td style="background-color: #d9f0d3" data-test="high">{value}-high</td>'
745-
if num_value < 3: # Values < 3 get blue background with indicator
746-
return f'<td style="background-color: #d3e9f0" data-test="low">{value}-low</td>'
747-
except (ValueError, TypeError):
748-
pass
749-
750-
# Default styling for other cells (3, 4, 5)
751-
return (
752-
f'<td style="border: 1px solid #ddd" data-test="mid">{value}-mid</td>'
753-
)
736+
# Create a custom cell builder with distinct styling for different value ranges
737+
def custom_cell_builder(value, row, col, table_id):
738+
try:
739+
num_value = int(value)
740+
if num_value > 5: # Values > 5 get green background with indicator
741+
return f'<td style="background-color: #d9f0d3" data-test="high">{value}-high</td>'
742+
if num_value < 3: # Values < 3 get blue background with indicator
743+
return f'<td style="background-color: #d3e9f0" data-test="low">{value}-low</td>'
744+
except (ValueError, TypeError):
745+
pass
754746

755-
# Set our custom cell builder
756-
formatter = get_formatter()
757-
formatter.set_custom_cell_builder(custom_cell_builder)
747+
# Default styling for other cells (3, 4, 5)
748+
return f'<td style="border: 1px solid #ddd" data-test="mid">{value}-mid</td>'
758749

759-
html_output = df._repr_html_()
750+
# Set our custom cell builder
751+
formatter = get_formatter()
752+
formatter.set_custom_cell_builder(custom_cell_builder)
760753

761-
# Extract cells with specific styling using regex
762-
low_cells = re.findall(
763-
r'<td style="background-color: #d3e9f0"[^>]*>(\d+)-low</td>', html_output
764-
)
765-
mid_cells = re.findall(
766-
r'<td style="border: 1px solid #ddd"[^>]*>(\d+)-mid</td>', html_output
767-
)
768-
high_cells = re.findall(
769-
r'<td style="background-color: #d9f0d3"[^>]*>(\d+)-high</td>', html_output
770-
)
754+
html_output = df._repr_html_()
771755

772-
# Sort the extracted values for consistent comparison
773-
low_cells = sorted(map(int, low_cells))
774-
mid_cells = sorted(map(int, mid_cells))
775-
high_cells = sorted(map(int, high_cells))
756+
# Extract cells with specific styling using regex
757+
low_cells = re.findall(
758+
r'<td style="background-color: #d3e9f0"[^>]*>(\d+)-low</td>', html_output
759+
)
760+
mid_cells = re.findall(
761+
r'<td style="border: 1px solid #ddd"[^>]*>(\d+)-mid</td>', html_output
762+
)
763+
high_cells = re.findall(
764+
r'<td style="background-color: #d9f0d3"[^>]*>(\d+)-high</td>', html_output
765+
)
776766

777-
# Verify specific values have the correct styling applied
778-
assert low_cells == [1, 2] # Values < 3
779-
assert mid_cells == [3, 4, 5, 5] # Values 3-5
780-
assert high_cells == [6, 8, 8] # Values > 5
767+
# Sort the extracted values for consistent comparison
768+
low_cells = sorted(map(int, low_cells))
769+
mid_cells = sorted(map(int, mid_cells))
770+
high_cells = sorted(map(int, high_cells))
781771

782-
# Verify the exact content with styling appears in the output
783-
assert (
784-
'<td style="background-color: #d3e9f0" data-test="low">1-low</td>'
785-
in html_output
786-
)
787-
assert (
788-
'<td style="background-color: #d3e9f0" data-test="low">2-low</td>'
789-
in html_output
790-
)
791-
assert (
792-
'<td style="border: 1px solid #ddd" data-test="mid">3-mid</td>'
793-
in html_output
794-
)
795-
assert (
796-
'<td style="border: 1px solid #ddd" data-test="mid">4-mid</td>'
797-
in html_output
798-
)
799-
assert (
800-
'<td style="background-color: #d9f0d3" data-test="high">6-high</td>'
801-
in html_output
802-
)
803-
assert (
804-
'<td style="background-color: #d9f0d3" data-test="high">8-high</td>'
805-
in html_output
806-
)
772+
# Verify specific values have the correct styling applied
773+
assert low_cells == [1, 2] # Values < 3
774+
assert mid_cells == [3, 4, 5, 5] # Values 3-5
775+
assert high_cells == [6, 8, 8] # Values > 5
776+
777+
# Verify the exact content with styling appears in the output
778+
assert (
779+
'<td style="background-color: #d3e9f0" data-test="low">1-low</td>'
780+
in html_output
781+
)
782+
assert (
783+
'<td style="background-color: #d3e9f0" data-test="low">2-low</td>'
784+
in html_output
785+
)
786+
assert (
787+
'<td style="border: 1px solid #ddd" data-test="mid">3-mid</td>' in html_output
788+
)
789+
assert (
790+
'<td style="border: 1px solid #ddd" data-test="mid">4-mid</td>' in html_output
791+
)
792+
assert (
793+
'<td style="background-color: #d9f0d3" data-test="high">6-high</td>'
794+
in html_output
795+
)
796+
assert (
797+
'<td style="background-color: #d9f0d3" data-test="high">8-high</td>'
798+
in html_output
799+
)
807800

808-
# Count occurrences to ensure all cells are properly styled
809-
assert html_output.count("-low</td>") == 2 # Two low values (1, 2)
810-
assert html_output.count("-mid</td>") == 4 # Four mid values (3, 4, 5, 5)
811-
assert html_output.count("-high</td>") == 3 # Three high values (6, 8, 8)
801+
# Count occurrences to ensure all cells are properly styled
802+
assert html_output.count("-low</td>") == 2 # Two low values (1, 2)
803+
assert html_output.count("-mid</td>") == 4 # Four mid values (3, 4, 5, 5)
804+
assert html_output.count("-high</td>") == 3 # Three high values (6, 8, 8)
812805

813806
# Create a custom cell builder that changes background color based on value
814807
def custom_cell_builder(value, row, col, table_id):

0 commit comments

Comments
 (0)