-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_compoundarray_validator.py
104 lines (76 loc) · 2.86 KB
/
test_compoundarray_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pytest
from _plotly_utils.basevalidators import CompoundArrayValidator
from plotly.graph_objs.layout import Image
# Fixtures
# --------
@pytest.fixture()
def validator():
return CompoundArrayValidator(
"prop", "layout", data_class_str="Image", data_docs=""
)
# Tests
# -----
def test_acceptance(validator):
val = [Image(opacity=0.5, sizex=120), Image(x=99)]
res = validator.validate_coerce(val)
res_present = validator.present(res)
assert isinstance(res, list)
assert isinstance(res_present, tuple)
assert isinstance(res_present[0], Image)
assert res_present[0].opacity == 0.5
assert res_present[0].sizex == 120
assert res_present[0].x is None
assert isinstance(res_present[1], Image)
assert res_present[1].opacity is None
assert res_present[1].sizex is None
assert res_present[1].x == 99
def test_acceptance_empty(validator):
val = [{}]
res = validator.validate_coerce(val)
res_present = validator.present(res)
assert isinstance(res, list)
assert isinstance(res_present, tuple)
assert isinstance(res_present[0], Image)
assert res_present[0].opacity is None
assert res_present[0].sizex is None
assert res_present[0].x is None
def test_acceptance_dict(validator):
val = [dict(opacity=0.5, sizex=120), dict(x=99)]
res = validator.validate_coerce(val)
res_present = validator.present(res)
assert isinstance(res, list)
assert isinstance(res_present, tuple)
assert isinstance(res_present[0], Image)
assert res_present[0].opacity == 0.5
assert res_present[0].sizex == 120
assert res_present[0].x is None
assert isinstance(res[1], Image)
assert res_present[1].opacity is None
assert res_present[1].sizex is None
assert res_present[1].x == 99
def test_rejection_type(validator):
val = 37
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
def test_rejection_element(validator):
val = ["a", 37]
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
def test_rejection_value(validator):
val = [dict(opacity=0.5, sizex=120, bogus=100)]
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)
assert (
"Invalid property specified for object of type "
"plotly.graph_objs.layout.Image" in str(validation_failure.value)
)
def test_skip_invalid(validator):
val = [
dict(opacity="bad_opacity", x=23, sizex=120),
dict(x=99, bogus={"a": 23}, sizey=300),
]
expected = [dict(x=23, sizex=120), dict(x=99, sizey=300)]
res = validator.validate_coerce(val, skip_invalid=True)
assert [el.to_plotly_json() for el in res] == expected