Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add some width/height testing.
  • Loading branch information
ayjayt committed Aug 14, 2025
commit f1e76078ce4d87a5690c0b770cc71f6137b302b8
52 changes: 52 additions & 0 deletions tests/test_optional/test_kaleido/test_kaleido.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
import tempfile
from unittest.mock import patch
import xml.etree.ElementTree as ET

from pdfrw import PdfReader
from PIL import Image
Expand Down Expand Up @@ -314,3 +315,54 @@ def test_get_chrome():

# Verify that kaleido.get_chrome_sync was called
mock_get_chrome.assert_called_once()


def create_figure(width=None, height=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you move these two utility functions to the top of the file so they're not mixed in with the test functions?

"""Create a simple figure with optional layout dimensions."""
layout = {}
if width:
layout['width'] = width
if height:
layout['height'] = height

return go.Figure(
data=[go.Scatter(x=[1, 2, 3], y=[1, 2, 3])],
layout=layout
)


def parse_svg_dimensions(svg_bytes):
"""Parse width and height from SVG bytes."""
svg_str = svg_bytes.decode('utf-8')
root = ET.fromstring(svg_str)
width = root.get('width')
height = root.get('height')
return int(width) if width else None, int(height) if height else None


def test_width_height_priority():
"""Test width/height priority: arguments > layout.width/height > defaults."""

# Test case 1: Arguments override layout
fig = create_figure(layout_width=800, layout_height=600)
svg_bytes = pio.to_image(fig, format='svg', width=1000, height=900)
width, height = parse_svg_dimensions(svg_bytes)
assert width == 1000 and height == 900, "Arguments should override layout dimensions"

# Test case 2: Layout dimensions used when no arguments
fig = create_figure(layout_width=800, layout_height=600)
svg_bytes = pio.to_image(fig, format='svg')
width, height = parse_svg_dimensions(svg_bytes)
assert width == 800 and height == 600, "Layout dimensions should be used when no arguments provided"

# Test case 3: Partial override (only width argument)
fig = create_figure(layout_width=800, layout_height=600)
svg_bytes = pio.to_image(fig, format='svg', width=1200)
width, height = parse_svg_dimensions(svg_bytes)
assert width == 1200 and height == 600, "Width argument should override layout, height should use layout"

# Test case 4: Defaults used when no layout or arguments
fig = create_figure()
svg_bytes = pio.to_image(fig, format='svg')
width, height = parse_svg_dimensions(svg_bytes)
assert width is not None and height is not None, "Default dimensions should be used when no layout or arguments"