File: test_compound_validator.py

package info (click to toggle)
plotly 5.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 90,200 kB
  • sloc: python: 368,793; javascript: 213,159; sh: 49; makefile: 4
file content (93 lines) | stat: -rw-r--r-- 2,459 bytes parent folder | download
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
import pytest
from _plotly_utils.basevalidators import CompoundValidator
from plotly.graph_objs.scatter import Marker


# Fixtures
# --------
@pytest.fixture()
def validator():
    return CompoundValidator("prop", "scatter", data_class_str="Marker", data_docs="")


# Tests
# -----
def test_acceptance(validator):
    val = Marker(color="green", size=10)
    res = validator.validate_coerce(val)

    assert isinstance(res, Marker)
    assert res.color == "green"
    assert res.size == 10


def test_acceptance_none(validator):
    val = None
    res = validator.validate_coerce(val)

    assert isinstance(res, Marker)
    assert res.color is None
    assert res.size is None


def test_acceptance_dict(validator):
    val = dict(color="green", size=10)
    res = validator.validate_coerce(val)

    assert isinstance(res, Marker)
    assert res.color == "green"
    assert res.size == 10


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_value(validator):
    val = dict(color="green", size=10, bogus=99)

    with pytest.raises(ValueError) as validation_failure:
        validator.validate_coerce(val)

    assert (
        "Invalid property specified for object of type "
        "plotly.graph_objs.scatter.Marker: 'bogus'" in str(validation_failure.value)
    )


def test_skip_invalid(validator):
    val = dict(
        color="green",
        size=10,
        bogus=99,  # Bad property name
        colorbar={"bgcolor": "blue", "bogus_inner": 23},  # Bad nested property name
        opacity="bogus value",  # Bad value for valid property
    )

    expected = dict(color="green", size=10, colorbar={"bgcolor": "blue"})

    res = validator.validate_coerce(val, skip_invalid=True)
    assert res.to_plotly_json() == expected


def test_skip_invalid_empty_object(validator):
    val = dict(
        color="green",
        size=10,
        colorbar={
            "bgcolor": "bad_color",  # Bad value for valid property
            "bogus_inner": 23,  # Bad nested property name
        },
        opacity=0.5,  # Bad value for valid property
    )

    # The colorbar property should be absent, not None or {}
    expected = dict(color="green", size=10, opacity=0.5)

    res = validator.validate_coerce(val, skip_invalid=True)
    assert res.to_plotly_json() == expected