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 152
|
import pytest
from pytest import approx
from _plotly_utils.basevalidators import NumberValidator
import numpy as np
import pandas as pd
# Fixtures
# --------
@pytest.fixture
def validator(request):
return NumberValidator("prop", "parent")
@pytest.fixture
def validator_min_max(request):
return NumberValidator("prop", "parent", min=-1.0, max=2.0)
@pytest.fixture
def validator_min(request):
return NumberValidator("prop", "parent", min=-1.0)
@pytest.fixture
def validator_max(request):
return NumberValidator("prop", "parent", max=2.0)
@pytest.fixture
def validator_aok():
return NumberValidator("prop", "parent", min=-1, max=1.5, array_ok=True)
# Array not ok
# ------------
# ### Acceptance ###
@pytest.mark.parametrize(
"val", [1.0, 0.0, 1, -1234.5678, 54321, np.pi, np.nan, np.inf, -np.inf]
)
def test_acceptance(val, validator):
assert validator.validate_coerce(val) == approx(val, nan_ok=True)
# ### Rejection by value ###
@pytest.mark.parametrize("val", ["hello", (), [], [1, 2, 3], set(), "34"])
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)
# ### With min/max ###
@pytest.mark.parametrize("val", [0, 0.0, -0.5, 1, 1.0, 2, 2.0, np.pi / 2.0])
def test_acceptance_min_max(val, validator_min_max):
assert validator_min_max.validate_coerce(val) == approx(val)
@pytest.mark.parametrize("val", [-1.01, -10, 2.1, 234, -np.inf, np.nan, np.inf])
def test_rejection_min_max(val, validator_min_max):
with pytest.raises(ValueError) as validation_failure:
validator_min_max.validate_coerce(val)
assert "in the interval [-1.0, 2.0]" in str(validation_failure.value)
# ### With min only ###
@pytest.mark.parametrize("val", [0, 0.0, -0.5, 99999, np.inf])
def test_acceptance_min(val, validator_min):
assert validator_min.validate_coerce(val) == approx(val)
@pytest.mark.parametrize("val", [-1.01, -np.inf, np.nan])
def test_rejection_min(val, validator_min):
with pytest.raises(ValueError) as validation_failure:
validator_min.validate_coerce(val)
assert "in the interval [-1.0, inf]" in str(validation_failure.value)
# ### With max only ###
@pytest.mark.parametrize("val", [0, 0.0, -np.inf, -123456, np.pi / 2])
def test_acceptance_max(val, validator_max):
assert validator_max.validate_coerce(val) == approx(val)
@pytest.mark.parametrize("val", [2.01, np.inf, np.nan])
def test_rejection_max(val, validator_max):
with pytest.raises(ValueError) as validation_failure:
validator_max.validate_coerce(val)
assert "in the interval [-inf, 2.0]" in str(validation_failure.value)
# Array ok
# --------
# ### Acceptance ###
@pytest.mark.parametrize("val", [1.0, 0.0, 1, 0.4])
def test_acceptance_aok_scalars(val, validator_aok):
assert validator_aok.validate_coerce(val) == val
@pytest.mark.parametrize("val", [[1.0, 0.0], [1], [-0.1234, 0.41, -1.0]])
def test_acceptance_aok_list(val, validator_aok):
assert np.array_equal(
validator_aok.validate_coerce(val), np.array(val, dtype="float")
)
# ### Coerce ###
# Coerced to general consistent numeric type
@pytest.mark.parametrize(
"val,expected",
[
([1.0, 0], (1.0, 0)),
(np.array([1, -1]), np.array([1, -1])),
(pd.Series([1, -1]), np.array([1, -1])),
(pd.Index([1, -1]), np.array([1, -1])),
((-0.1234, 0, -1), (-0.1234, 0.0, -1.0)),
],
)
def test_coercion_aok_list(val, expected, validator_aok):
v = validator_aok.validate_coerce(val)
if isinstance(val, (np.ndarray, pd.Series, pd.Index)):
assert np.array_equal(v, expected)
else:
assert isinstance(v, list)
assert validator_aok.present(v) == tuple(val)
# ### Rejection ###
#
@pytest.mark.parametrize("val", [["a", 4]])
def test_rejection_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)
# ### Rejection by element ###
@pytest.mark.parametrize(
"val",
[[-1.6, 0.0], [1, 1.5, 2], [-0.1234, 0.41, np.nan], [0, np.inf], [0, -np.inf]],
)
def test_rejection_aok_min_max(val, validator_aok):
with pytest.raises(ValueError) as validation_failure:
validator_aok.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
assert "in the interval [-1, 1.5]" in str(validation_failure.value)
|