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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
import pytest
import numpy as np
import pandas as pd
from _plotly_utils.basevalidators import EnumeratedValidator
# Fixtures
# --------
@pytest.fixture()
def validator():
values = ["first", "second", "third", 4]
return EnumeratedValidator("prop", "parent", values, array_ok=False)
@pytest.fixture()
def validator_re():
values = ["foo", "/bar(\d)+/", "baz"]
return EnumeratedValidator("prop", "parent", values, array_ok=False)
@pytest.fixture()
def validator_aok():
values = ["first", "second", "third", 4]
return EnumeratedValidator("prop", "parent", values, array_ok=True)
@pytest.fixture()
def validator_aok_re():
values = ["foo", "/bar(\d)+/", "baz"]
return EnumeratedValidator("prop", "parent", values, array_ok=True)
# Array not ok
# ------------
# ### Acceptance ###
@pytest.mark.parametrize("val", ["first", "second", "third", 4])
def test_acceptance(val, validator):
# Values should be accepted and returned unchanged
assert validator.validate_coerce(val) == val
# ### Value Rejection ###
@pytest.mark.parametrize(
"val",
[True, 0, 1, 23, np.inf, set(), ["first", "second"], [True], ["third", 4], [4]],
)
def test_rejection_by_value(val, validator):
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
# Array not ok, regular expression
# --------------------------------
@pytest.mark.parametrize("val", ["foo", "bar0", "bar1", "bar234"])
def test_acceptance(val, validator_re):
# Values should be accepted and returned unchanged
assert validator_re.validate_coerce(val) == val
# ### Value Rejection ###
@pytest.mark.parametrize("val", [12, set(), "bar", "BAR0", "FOO"])
def test_rejection_by_value(val, validator_re):
with pytest.raises(ValueError) as validation_failure:
validator_re.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
# Array ok
# --------
# ### Acceptance ###
@pytest.mark.parametrize(
"val",
[
"first",
"second",
"third",
4,
[],
["first", 4],
[4],
["third", "first"],
["first", "second", "third", 4],
],
)
def test_acceptance_aok(val, validator_aok):
# Values should be accepted and returned unchanged
coerce_val = validator_aok.validate_coerce(val)
if isinstance(val, (list, np.ndarray)):
assert np.array_equal(coerce_val, np.array(val, dtype=coerce_val.dtype))
else:
assert coerce_val == val
# ### Rejection by value ###
@pytest.mark.parametrize("val", [True, 0, 1, 23, np.inf, set()])
def test_rejection_by_value_aok(val, validator_aok):
with pytest.raises(ValueError) as validation_failure:
validator_aok.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
# ### Reject by elements ###
@pytest.mark.parametrize(
"val", [[True], [0], [1, 23], [np.inf, set()], ["ffirstt", "second", "third"]]
)
def test_rejection_by_element_aok(val, validator_aok):
with pytest.raises(ValueError) as validation_failure:
validator_aok.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
# Array ok, regular expression
# ----------------------------
# ### Acceptance ###
@pytest.mark.parametrize(
"val",
[
"foo",
"bar12",
"bar21",
[],
["bar12"],
("foo", "bar012", "baz"),
np.array([], dtype="object"),
np.array(["bar12"]),
np.array(["foo", "bar012", "baz"]),
],
)
def test_acceptance_aok(val, validator_aok_re):
# Values should be accepted and returned unchanged
coerce_val = validator_aok_re.validate_coerce(val)
if isinstance(val, (np.ndarray, pd.Series)):
assert np.array_equal(coerce_val, np.array(val, dtype=coerce_val.dtype))
elif isinstance(val, (list, tuple)):
assert validator_aok_re.present(coerce_val) == tuple(val)
else:
assert validator_aok_re.present(coerce_val) == val
# ### Reject by elements ###
@pytest.mark.parametrize("val", [["bar", "bar0"], ["foo", 123]])
def test_rejection_by_element_aok(val, validator_aok_re):
with pytest.raises(ValueError) as validation_failure:
validator_aok_re.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
|