|
3 | 3 | import tempfile
|
4 | 4 | from contextlib import redirect_stdout
|
5 | 5 | import base64
|
6 |
| -import unittest |
7 |
| -from unittest import mock |
8 | 6 |
|
9 | 7 | from pdfrw import PdfReader
|
10 | 8 | from PIL import Image
|
11 | 9 | import plotly.io as pio
|
| 10 | +from plotly.io.kaleido import kaleido_available, kaleido_major |
| 11 | +import pytest |
12 | 12 |
|
13 | 13 | fig = {"data": [], "layout": {"title": {"text": "figure title"}}}
|
14 | 14 |
|
@@ -91,6 +91,30 @@ def test_kaleido_engine_write_image_kwargs(tmp_path):
|
91 | 91 | check_image(out_path, size=(700 * 2, 600 * 2), format="JPEG")
|
92 | 92 |
|
93 | 93 |
|
| 94 | +@pytest.mark.skipif( |
| 95 | + not kaleido_available() or kaleido_major() < 1, |
| 96 | + reason="requires Kaleido v1.0.0 or higher", |
| 97 | +) |
| 98 | +def test_kaleido_engine_write_images(tmp_path): |
| 99 | + fig1 = {"data": [], "layout": {"title": {"text": "figure 1"}}} |
| 100 | + fig2 = {"data": [], "layout": {"title": {"text": "figure 2"}}} |
| 101 | + |
| 102 | + path_str = tempfile.mkstemp(suffix=".png", dir=tmp_path)[1] |
| 103 | + path_path = Path(tempfile.mkstemp(suffix=".png", dir=tmp_path)[1]) |
| 104 | + |
| 105 | + pio.write_images( |
| 106 | + [fig1, fig2], |
| 107 | + [path_str, path_path], |
| 108 | + format=["jpg", "png"], |
| 109 | + width=[700, 900], |
| 110 | + height=600, |
| 111 | + scale=2, |
| 112 | + validate=False, |
| 113 | + ) |
| 114 | + check_image(path_str, size=(700 * 2, 600 * 2), format="JPEG") |
| 115 | + check_image(str(path_path), size=(900 * 2, 600 * 2), format="PNG") |
| 116 | + |
| 117 | + |
94 | 118 | def test_image_renderer():
|
95 | 119 | """Verify that the image renderer returns the expected mimebundle."""
|
96 | 120 | with redirect_stdout(StringIO()) as f:
|
@@ -125,70 +149,6 @@ def test_bytesio():
|
125 | 149 | assert bio_bytes == to_image_bytes
|
126 | 150 |
|
127 | 151 |
|
128 |
| -@mock.patch("plotly.io._kaleido.write_image") |
129 |
| -def test_write_images_single(mock_write_image): |
130 |
| - """Test write_images with only single arguments""" |
131 |
| - pio.write_images( |
132 |
| - fig, |
133 |
| - "output.png", |
134 |
| - format="png", |
135 |
| - width=800, |
136 |
| - height=600, |
137 |
| - scale=2, |
138 |
| - ) |
139 |
| - |
140 |
| - # Verify that write_image was called once with the correct arguments |
141 |
| - expected_calls = [ |
142 |
| - mock.call( |
143 |
| - fig, |
144 |
| - "output.png", |
145 |
| - format="png", |
146 |
| - width=800, |
147 |
| - height=600, |
148 |
| - scale=2, |
149 |
| - ) |
150 |
| - ] |
151 |
| - mock_write_image.assert_has_calls(expected_calls, any_order=False) |
152 |
| - assert mock_write_image.call_count == 1 |
153 |
| - |
154 |
| - |
155 |
| -@mock.patch("plotly.io._kaleido.write_image") |
156 |
| -def test_write_images_multiple(mock_write_image): |
157 |
| - """Test write_images with list arguments""" |
158 |
| - fig1 = {"data": [], "layout": {"title": {"text": "figure 1"}}} |
159 |
| - fig2 = {"data": [], "layout": {"title": {"text": "figure 2"}}} |
160 |
| - pio.write_images( |
161 |
| - [fig1, fig2], |
162 |
| - ["output1.png", "output2.jpg"], |
163 |
| - format=["png", "jpeg"], |
164 |
| - width=800, |
165 |
| - height=[600, 400], |
166 |
| - scale=2, |
167 |
| - ) |
168 |
| - |
169 |
| - # Verify that write_image was called with the correct arguments in the correct order |
170 |
| - expected_calls = [ |
171 |
| - mock.call( |
172 |
| - fig1, |
173 |
| - "output1.png", |
174 |
| - format="png", |
175 |
| - width=800, |
176 |
| - height=600, |
177 |
| - scale=2, |
178 |
| - ), |
179 |
| - mock.call( |
180 |
| - fig2, |
181 |
| - "output2.jpg", |
182 |
| - format="jpeg", |
183 |
| - width=800, |
184 |
| - height=400, |
185 |
| - scale=2, |
186 |
| - ), |
187 |
| - ] |
188 |
| - mock_write_image.assert_has_calls(expected_calls, any_order=False) |
189 |
| - assert mock_write_image.call_count == 2 |
190 |
| - |
191 |
| - |
192 | 152 | def test_defaults():
|
193 | 153 | """Test that image output defaults can be set using pio.defaults.*"""
|
194 | 154 | try:
|
|
0 commit comments