Skip to content

Commit 1179bf3

Browse files
committed
test: add unit tests for image_array_to_data_uri
1 parent 5cdb606 commit 1179bf3

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Tests for _plotly_utils.data_utils.image_array_to_data_uri.
3+
4+
Uses the pypng backend so the tests do not require Pillow.
5+
"""
6+
7+
import numpy as np
8+
import pytest
9+
10+
from _plotly_utils.data_utils import image_array_to_data_uri
11+
12+
PNG_PREFIX = "data:image/png;base64,"
13+
14+
15+
def test_greyscale_array_returns_png_data_uri():
16+
img = np.zeros((2, 2), dtype=np.uint8)
17+
uri = image_array_to_data_uri(img, backend="pypng")
18+
assert uri.startswith(PNG_PREFIX)
19+
20+
21+
def test_rgb_array_returns_png_data_uri():
22+
img = np.zeros((2, 2, 3), dtype=np.uint8)
23+
uri = image_array_to_data_uri(img, backend="pypng")
24+
assert uri.startswith(PNG_PREFIX)
25+
26+
27+
def test_rgba_array_returns_png_data_uri():
28+
img = np.zeros((2, 2, 4), dtype=np.uint8)
29+
uri = image_array_to_data_uri(img, backend="pypng")
30+
assert uri.startswith(PNG_PREFIX)
31+
32+
33+
@pytest.mark.parametrize("compression", [-1, 10])
34+
def test_invalid_compression_raises(compression):
35+
img = np.zeros((2, 2), dtype=np.uint8)
36+
with pytest.raises(ValueError, match="compression level"):
37+
image_array_to_data_uri(img, backend="pypng", compression=compression)
38+
39+
40+
def test_invalid_shape_raises():
41+
img = np.zeros(5, dtype=np.uint8)
42+
with pytest.raises(ValueError, match="Invalid image shape"):
43+
image_array_to_data_uri(img, backend="pypng")
44+
45+
46+
def test_jpg_without_pil_backend_raises():
47+
img = np.zeros((2, 2), dtype=np.uint8)
48+
with pytest.raises(ValueError, match="jpg binary strings"):
49+
image_array_to_data_uri(img, backend="pypng", ext="jpg")

0 commit comments

Comments
 (0)