|
3 | 3 | import tempfile
|
4 | 4 | from contextlib import redirect_stdout
|
5 | 5 | import base64
|
| 6 | +import unittest |
| 7 | +from unittest import mock |
6 | 8 |
|
7 | 9 | from pdfrw import PdfReader
|
8 | 10 | from PIL import Image
|
@@ -121,3 +123,131 @@ def test_bytesio():
|
121 | 123 | bio_bytes = bio.read()
|
122 | 124 | to_image_bytes = pio.to_image(fig, format="jpg", engine="kaleido", validate=False)
|
123 | 125 | assert bio_bytes == to_image_bytes
|
| 126 | + |
| 127 | + |
| 128 | +@mock.patch("plotly.io._kaleido.to_image") |
| 129 | +def test_to_images_single(mock_to_image): |
| 130 | + """Test to_images with a single figure""" |
| 131 | + pio.to_images( |
| 132 | + fig, |
| 133 | + format="png", |
| 134 | + width=800, |
| 135 | + height=600, |
| 136 | + scale=2, |
| 137 | + validate=True, |
| 138 | + ) |
| 139 | + |
| 140 | + # Verify that to_image was called once with the correct arguments |
| 141 | + expected_calls = [ |
| 142 | + mock.call( |
| 143 | + fig, |
| 144 | + format="png", |
| 145 | + width=800, |
| 146 | + height=600, |
| 147 | + scale=2, |
| 148 | + validate=True, |
| 149 | + ) |
| 150 | + ] |
| 151 | + mock_to_image.assert_has_calls(expected_calls, any_order=False) |
| 152 | + assert mock_to_image.call_count == 1 |
| 153 | + |
| 154 | + |
| 155 | +@mock.patch("plotly.io._kaleido.to_image") |
| 156 | +def test_to_images_multiple(mock_to_image): |
| 157 | + """Test to_images with lists""" |
| 158 | + fig1 = {"data": [], "layout": {"title": {"text": "figure 1"}}} |
| 159 | + fig2 = {"data": [], "layout": {"title": {"text": "figure 2"}}} |
| 160 | + pio.to_images( |
| 161 | + [fig1, fig2], |
| 162 | + "png", |
| 163 | + width=[800, 400], |
| 164 | + height=600, |
| 165 | + scale=[1, 2], |
| 166 | + validate=True, |
| 167 | + ) |
| 168 | + |
| 169 | + # Verify that to_image was called with the correct arguments in the correct order |
| 170 | + expected_calls = [ |
| 171 | + mock.call( |
| 172 | + fig1, |
| 173 | + "png", |
| 174 | + width=800, |
| 175 | + height=600, |
| 176 | + scale=1, |
| 177 | + validate=True, |
| 178 | + ), |
| 179 | + mock.call( |
| 180 | + fig2, |
| 181 | + "png", |
| 182 | + width=400, |
| 183 | + height=600, |
| 184 | + scale=2, |
| 185 | + validate=True, |
| 186 | + ), |
| 187 | + ] |
| 188 | + mock_to_image.assert_has_calls(expected_calls, any_order=False) |
| 189 | + assert mock_to_image.call_count == 2 |
| 190 | + |
| 191 | + |
| 192 | +@mock.patch("plotly.io._kaleido.write_image") |
| 193 | +def test_write_images_single(mock_write_image): |
| 194 | + """Test write_images with only single arguments""" |
| 195 | + pio.write_images( |
| 196 | + fig, |
| 197 | + "output.png", |
| 198 | + format="png", |
| 199 | + width=800, |
| 200 | + height=600, |
| 201 | + scale=2, |
| 202 | + ) |
| 203 | + |
| 204 | + # Verify that write_image was called once with the correct arguments |
| 205 | + expected_calls = [ |
| 206 | + mock.call( |
| 207 | + fig, |
| 208 | + "output.png", |
| 209 | + format="png", |
| 210 | + width=800, |
| 211 | + height=600, |
| 212 | + scale=2, |
| 213 | + ) |
| 214 | + ] |
| 215 | + mock_write_image.assert_has_calls(expected_calls, any_order=False) |
| 216 | + assert mock_write_image.call_count == 1 |
| 217 | + |
| 218 | + |
| 219 | +@mock.patch("plotly.io._kaleido.write_image") |
| 220 | +def test_write_images_multiple(mock_write_image): |
| 221 | + """Test write_images with list arguments""" |
| 222 | + fig1 = {"data": [], "layout": {"title": {"text": "figure 1"}}} |
| 223 | + fig2 = {"data": [], "layout": {"title": {"text": "figure 2"}}} |
| 224 | + pio.write_images( |
| 225 | + [fig1, fig2], |
| 226 | + ["output1.png", "output2.jpg"], |
| 227 | + format=["png", "jpeg"], |
| 228 | + width=800, |
| 229 | + height=[600, 400], |
| 230 | + scale=2, |
| 231 | + ) |
| 232 | + |
| 233 | + # Verify that write_image was called with the correct arguments in the correct order |
| 234 | + expected_calls = [ |
| 235 | + mock.call( |
| 236 | + fig1, |
| 237 | + "output1.png", |
| 238 | + format="png", |
| 239 | + width=800, |
| 240 | + height=600, |
| 241 | + scale=2, |
| 242 | + ), |
| 243 | + mock.call( |
| 244 | + fig2, |
| 245 | + "output2.jpg", |
| 246 | + format="jpeg", |
| 247 | + width=800, |
| 248 | + height=400, |
| 249 | + scale=2, |
| 250 | + ), |
| 251 | + ] |
| 252 | + mock_write_image.assert_has_calls(expected_calls, any_order=False) |
| 253 | + assert mock_write_image.call_count == 2 |
0 commit comments