Skip to content

Commit c52e9f4

Browse files
authored
Merge pull request #4707 from plotly/validator_tests
Base64 Validator tests
2 parents 644992f + 69729db commit c52e9f4

12 files changed

+139
-8
lines changed

packages/python/plotly/_plotly_utils/tests/validators/test_angle_validator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# from ..basevalidators import AngleValidator
44
from _plotly_utils.basevalidators import AngleValidator
55
import numpy as np
6+
from plotly.tests.b64 import b64
67

78

89
# Fixtures
@@ -51,6 +52,14 @@ def test_aok_acceptance(val, validator_aok):
5152
assert np.array_equal(validator_aok.validate_coerce(np.array(val)), np.array(val))
5253

5354

55+
# Test base64 array
56+
def test_aok_base64_array(validator_aok):
57+
val = b64(np.array([1, 2, 3], dtype="int64"))
58+
coerce_val = validator_aok.validate_coerce(val)
59+
assert coerce_val["bdata"] == "AQID"
60+
assert coerce_val["dtype"] == "i1"
61+
62+
5463
# ### Test coercion above 180 ###
5564
@pytest.mark.parametrize(
5665
"val,expected",

packages/python/plotly/_plotly_utils/tests/validators/test_any_validator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from _plotly_utils.basevalidators import AnyValidator
33
import numpy as np
4+
from plotly.tests.b64 import b64
45

56

67
# Fixtures
@@ -49,3 +50,10 @@ def test_acceptance_array(val, validator_aok):
4950
else:
5051
assert coerce_val == val
5152
assert validator_aok.present(coerce_val) == val
53+
54+
55+
def test_base64_array(validator_aok):
56+
val = b64(np.array([1, 2, 3], dtype="int64"))
57+
coerce_val = validator_aok.validate_coerce(val)
58+
assert coerce_val["bdata"] == "AQID"
59+
assert coerce_val["dtype"] == "i1"

packages/python/plotly/_plotly_utils/tests/validators/test_basetraces_validator.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from _plotly_utils.basevalidators import BaseDataValidator
33
from plotly.graph_objs import Scatter, Bar, Box
4-
4+
import numpy as np
55

66
# Fixtures
77
# --------
@@ -118,6 +118,19 @@ def test_rejection_element_tracetype(validator):
118118
assert "Invalid element(s)" in str(validation_failure.value)
119119

120120

121+
def test_b64(validator):
122+
val = [dict(type="scatter", x=np.array([1, 2, 3]))]
123+
res = validator.validate_coerce(val)
124+
res_present = validator.present(res)
125+
126+
assert isinstance(res, list)
127+
assert isinstance(res_present, tuple)
128+
129+
assert isinstance(res_present[0], Scatter)
130+
assert res_present[0].type == "scatter"
131+
assert res_present[0].x == {"bdata": "AQID", "dtype": "i1"}
132+
133+
121134
def test_skip_invalid(validator_nouid):
122135
val = (
123136
dict(

packages/python/plotly/_plotly_utils/tests/validators/test_color_validator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from _plotly_utils.basevalidators import ColorValidator
33
import numpy as np
4+
from plotly.tests.b64 import b64
45

56

67
# Fixtures
@@ -131,6 +132,19 @@ def test_acceptance_aok(val, validator_aok):
131132
assert coerce_val == val
132133

133134

135+
# Test that it doesn't use a base64 array
136+
# Numpy v2 has a StrDType but we don't want to convert it yet.
137+
# Change this test if you add support for it.
138+
def test_acceptance_aok_base64_array(validator_aok):
139+
val = b64(np.array(["aliceblue", "antiquewhite", "aqua", "aquamarine", "azure"]))
140+
coerce_val = validator_aok.validate_coerce(val)
141+
assert coerce_val[0] == "aliceblue"
142+
assert coerce_val[1] == "antiquewhite"
143+
assert coerce_val[2] == "aqua"
144+
assert coerce_val[3] == "aquamarine"
145+
assert coerce_val[4] == "azure"
146+
147+
134148
@pytest.mark.parametrize(
135149
"val",
136150
[

packages/python/plotly/_plotly_utils/tests/validators/test_colorlist_validator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44
from _plotly_utils.basevalidators import ColorlistValidator
5-
5+
from plotly.tests.b64 import b64
66

77
# Fixtures
88
# --------
@@ -48,3 +48,13 @@ def test_acceptance_aok(val, validator):
4848
coerce_val = validator.validate_coerce(val)
4949
assert isinstance(coerce_val, list)
5050
assert validator.present(coerce_val) == tuple(val)
51+
52+
53+
# Test that it doesn't use a base64 array
54+
# Numpy v2 has a StrDType but we don't want to convert it yet.
55+
# Change this test if you add support for it.
56+
def test_acceptance_b64_aok(validator):
57+
val = b64(np.array(["red", "rgb(255, 0, 0)"]))
58+
coerce_val = validator.validate_coerce(val)
59+
assert coerce_val[0] == "red"
60+
assert coerce_val[1] == "rgb(255, 0, 0)"

packages/python/plotly/_plotly_utils/tests/validators/test_compoundarray_validator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from _plotly_utils.basevalidators import CompoundArrayValidator
33
from plotly.graph_objs.layout import Image
44

5-
65
# Fixtures
76
# --------
87
@pytest.fixture()

packages/python/plotly/_plotly_utils/tests/validators/test_enumerated_validator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import numpy as np
33
import pandas as pd
44
from _plotly_utils.basevalidators import EnumeratedValidator
5+
from plotly.tests.b64 import b64
56

67

78
# Fixtures
@@ -94,6 +95,14 @@ def test_acceptance_aok(val, validator_aok):
9495
assert coerce_val == val
9596

9697

98+
# Test base64 array
99+
def test_aok_base64_array(validator_aok):
100+
val = b64(np.array([1, 2, 3], dtype="int64"))
101+
coerce_val = validator_aok.validate_coerce(val)
102+
assert coerce_val["bdata"] == "AQID"
103+
assert coerce_val["dtype"] == "i1"
104+
105+
97106
# ### Rejection by value ###
98107
@pytest.mark.parametrize("val", [True, 0, 1, 23, np.inf, set()])
99108
def test_rejection_by_value_aok(val, validator_aok):

packages/python/plotly/_plotly_utils/tests/validators/test_flaglist_validator.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33
from _plotly_utils.basevalidators import FlaglistValidator
44
import numpy as np
5-
5+
from plotly.tests.b64 import b64
66

77
EXTRAS = ["none", "all", True, False, 3]
88
FLAGS = ["lines", "markers", "text"]
@@ -130,6 +130,14 @@ def test_acceptance_aok_scalar_extra(extra, validator_extra_aok):
130130
assert validator_extra_aok.validate_coerce(extra) == extra
131131

132132

133+
# Test base64 array
134+
def test_acceptance_aok_scalar_base64(validator_extra_aok):
135+
val = b64(np.array([1, 2, 3], dtype="int64"))
136+
coerce_val = validator_extra_aok.validate_coerce(val)
137+
assert coerce_val["bdata"] == "AQID"
138+
assert coerce_val["dtype"] == "i1"
139+
140+
133141
# ### Acceptance (lists) ###
134142
def test_acceptance_aok_scalarlist_flaglist(flaglist, validator_extra_aok):
135143
assert np.array_equal(

packages/python/plotly/_plotly_utils/tests/validators/test_integer_validator.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,48 @@ def test_acceptance_aok_list(val, validator_aok):
113113

114114

115115
# Test base64 encoded arrays with array_ok=True
116-
@pytest.mark.parametrize("val", [b64(np.array([1, 0], dtype="int16")), b64([1, 0])])
117-
def test_acceptance_aok_base64(val, validator_aok):
118-
assert np.array_equal(validator_aok.validate_coerce(val), val)
116+
INT_BASE64_TEST_CASES = [
117+
# Note: we decided not to support int64 in plotly.js,
118+
# so the the max / min value are limited to int32 and the
119+
# dtype is cast to int32 in the output
120+
(
121+
b64(np.array([-900000000, 900000000, 3], dtype="int64")),
122+
{"bdata": "ABdbygDppDUDAAAA", "dtype": "i4"},
123+
),
124+
(
125+
b64(np.array([-900000000, 900000000, 3], dtype="int32")),
126+
{"bdata": "ABdbygDppDUDAAAA", "dtype": "i4"},
127+
),
128+
(
129+
b64(np.array([32767, -32767, 3], dtype="int16")),
130+
{"bdata": "/38BgAMA", "dtype": "i2"},
131+
),
132+
(
133+
b64(np.array([127, -127, 3], dtype="int8")),
134+
{"bdata": "f4ED", "dtype": "i1"},
135+
),
136+
(
137+
b64(np.array([900000000, 2, 3], dtype="uint64")),
138+
{"bdata": "AOmkNQIAAAADAAAA", "dtype": "u4"},
139+
),
140+
(
141+
b64(np.array([900000000, 2, 3], dtype="uint32")),
142+
{"bdata": "AOmkNQIAAAADAAAA", "dtype": "u4"},
143+
),
144+
(
145+
b64(np.array([32767, 0, 3], dtype="uint16")),
146+
{"bdata": "/38AAAMA", "dtype": "u2"},
147+
),
148+
(
149+
b64(np.array([127, 2, 3], dtype="uint8")),
150+
{"bdata": "fwID", "dtype": "u1"},
151+
),
152+
]
153+
154+
155+
@pytest.mark.parametrize("val, expected", INT_BASE64_TEST_CASES)
156+
def test_acceptance_aok_base64(val, expected, validator_aok):
157+
assert np.array_equal(validator_aok.validate_coerce(val), expected)
119158

120159

121160
# ### Coerce ###

packages/python/plotly/_plotly_utils/tests/validators/test_number_validator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from _plotly_utils.basevalidators import NumberValidator
55
import numpy as np
66
import pandas as pd
7+
from plotly.tests.b64 import b64
78

89
# Fixtures
910
# --------
@@ -108,6 +109,14 @@ def test_acceptance_aok_list(val, validator_aok):
108109
)
109110

110111

112+
# Test base64 array
113+
def test_acceptance_aok_base64(validator_aok):
114+
val = b64(np.array([1, 2, 3], dtype="int64"))
115+
coerce_val = validator_aok.validate_coerce(val)
116+
assert coerce_val["bdata"] == "AQID"
117+
assert coerce_val["dtype"] == "i1"
118+
119+
111120
# ### Coerce ###
112121
# Coerced to general consistent numeric type
113122
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)