File: test_dataarray_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 (50 lines) | stat: -rw-r--r-- 1,275 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
import pytest
from _plotly_utils.basevalidators import DataArrayValidator
import numpy as np
import pandas as pd

# Fixtures
# --------
@pytest.fixture()
def validator():
    return DataArrayValidator("prop", "parent")


# Tests
# -----
# ### Acceptance ###
@pytest.mark.parametrize(
    "val",
    [
        [],
        [1],
        [""],
        (),
        ("Hello, ", "world!"),
        ["A", 1, "B", 0, "C"],
        [np.array(1), np.array(2)],
    ],
)
def test_validator_acceptance_simple(val, validator):
    coerce_val = validator.validate_coerce(val)
    assert isinstance(coerce_val, list)
    assert validator.present(coerce_val) == tuple(val)


@pytest.mark.parametrize(
    "val",
    [np.array([2, 3, 4]), pd.Series(["a", "b", "c"]), np.array([[1, 2, 3], [4, 5, 6]])],
)
def test_validator_acceptance_homogeneous(val, validator):
    coerce_val = validator.validate_coerce(val)
    assert isinstance(coerce_val, np.ndarray)
    assert np.array_equal(validator.present(coerce_val), val)


# ### Rejection ###
@pytest.mark.parametrize("val", ["Hello", 23, set(), {}])
def test_rejection(val, validator):
    with pytest.raises(ValueError) as validation_failure:
        validator.validate_coerce(val)

    assert "Invalid value" in str(validation_failure.value)