|
| 1 | +import pytest |
| 2 | +from _plotly_utils.basevalidators import ColorValidator |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +# Fixtures |
| 7 | +# -------- |
| 8 | +@pytest.fixture() |
| 9 | +def validator(): |
| 10 | + return ColorValidator('prop', 'parent') |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture() |
| 14 | +def validator_colorscale(): |
| 15 | + return ColorValidator('prop', 'parent', colorscale_path='parent.colorscale') |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def validator_aok(): |
| 20 | + return ColorValidator('prop', 'parent', array_ok=True) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture() |
| 24 | +def validator_aok_colorscale(): |
| 25 | + return ColorValidator('prop', 'parent', array_ok=True, colorscale_path='parent.colorscale') |
| 26 | + |
| 27 | + |
| 28 | +# Array not ok, numbers not ok |
| 29 | +# ---------------------------- |
| 30 | +@pytest.mark.parametrize('val', |
| 31 | + ['red', 'BLUE', 'rgb(255, 0, 0)', 'hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)', |
| 32 | + 'hsv(0, 100%, 100%)', 'hsva(0, 100%, 100%, 50%)']) |
| 33 | +def test_acceptance(val, validator: ColorValidator): |
| 34 | + if isinstance(val, str): |
| 35 | + assert validator.validate_coerce(val) == str.replace(val.lower(), ' ', '') |
| 36 | + else: |
| 37 | + assert validator.validate_coerce(val) == val |
| 38 | + |
| 39 | + |
| 40 | +# ### Rejection by type ### |
| 41 | +@pytest.mark.parametrize('val', |
| 42 | + [set(), 23, 0.5, {}, ['red'], [12]]) |
| 43 | +def test_rejection(val, validator: ColorValidator): |
| 44 | + with pytest.raises(ValueError) as validation_failure: |
| 45 | + validator.validate_coerce(val) |
| 46 | + |
| 47 | + assert 'Invalid value' in str(validation_failure.value) |
| 48 | + |
| 49 | + |
| 50 | +# ### Rejection by value ### |
| 51 | +@pytest.mark.parametrize('val', |
| 52 | + ['redd', 'rgbbb(255, 0, 0)', 'hsl(0, 1%0000%, 50%)']) |
| 53 | +def test_rejection(val, validator: ColorValidator): |
| 54 | + with pytest.raises(ValueError) as validation_failure: |
| 55 | + validator.validate_coerce(val) |
| 56 | + |
| 57 | + assert 'Invalid value' in str(validation_failure.value) |
| 58 | + |
| 59 | + |
| 60 | +# Array not ok, numbers ok |
| 61 | +# ------------------------ |
| 62 | +# ### Acceptance ### |
| 63 | +@pytest.mark.parametrize('val', |
| 64 | + ['red', 'BLUE', 23, 15, 'rgb(255, 0, 0)', 'hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)', |
| 65 | + 'hsv(0, 100%, 100%)', 'hsva(0, 100%, 100%, 50%)']) |
| 66 | +def test_acceptance_colorscale(val, validator_colorscale: ColorValidator): |
| 67 | + if isinstance(val, str): |
| 68 | + assert validator_colorscale.validate_coerce(val) == str.replace(val.lower(), ' ', '') |
| 69 | + else: |
| 70 | + assert validator_colorscale.validate_coerce(val) == val |
| 71 | + |
| 72 | + |
| 73 | +# ### Rejection by type ### |
| 74 | +@pytest.mark.parametrize('val', |
| 75 | + [set(), {}, ['red'], [12]]) |
| 76 | +def test_rejection_colorscale(val, validator_colorscale: ColorValidator): |
| 77 | + with pytest.raises(ValueError) as validation_failure: |
| 78 | + validator_colorscale.validate_coerce(val) |
| 79 | + |
| 80 | + assert 'Invalid value' in str(validation_failure.value) |
| 81 | + |
| 82 | + |
| 83 | +# ### Rejection by value ### |
| 84 | +@pytest.mark.parametrize('val', |
| 85 | + ['redd', 'rgbbb(255, 0, 0)', 'hsl(0, 1%0000%, 50%)']) |
| 86 | +def test_rejection_colorscale(val, validator_colorscale: ColorValidator): |
| 87 | + with pytest.raises(ValueError) as validation_failure: |
| 88 | + validator_colorscale.validate_coerce(val) |
| 89 | + |
| 90 | + assert 'Invalid value' in str(validation_failure.value) |
| 91 | + |
| 92 | + |
| 93 | +# Array ok, numbers not ok |
| 94 | +# ------------------------ |
| 95 | +# ### Acceptance ### |
| 96 | +@pytest.mark.parametrize('val', |
| 97 | + ['blue', |
| 98 | + ['red', 'rgb(255, 0, 0)'], |
| 99 | + ['hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)', 'hsv(0, 100%, 100%)'], |
| 100 | + ['hsva(0, 100%, 100%, 50%)']]) |
| 101 | +def test_acceptance_aok(val, validator_aok: ColorValidator): |
| 102 | + coerce_val = validator_aok.validate_coerce(val) |
| 103 | + if isinstance(val, (list, np.ndarray)): |
| 104 | + expected = np.array( |
| 105 | + [str.replace(v.lower(), ' ', '') if isinstance(v, str) else v for v in val], |
| 106 | + dtype=coerce_val.dtype) |
| 107 | + assert np.array_equal(coerce_val, expected) |
| 108 | + else: |
| 109 | + expected = str.replace(val.lower(), ' ', '') if isinstance(val, str) else val |
| 110 | + assert coerce_val == expected |
| 111 | + |
| 112 | + |
| 113 | +# ### Rejection ### |
| 114 | +@pytest.mark.parametrize('val', |
| 115 | + [[23], [0, 1, 2], |
| 116 | + ['redd', 'rgb(255, 0, 0)'], |
| 117 | + ['hsl(0, 100%, 50_00%)', 'hsla(0, 100%, 50%, 100%)', 'hsv(0, 100%, 100%)'], |
| 118 | + ['hsva(0, 1%00%, 100%, 50%)']]) |
| 119 | +def test_rejection_aok(val, validator_aok: ColorValidator): |
| 120 | + with pytest.raises(ValueError) as validation_failure: |
| 121 | + validator_aok.validate_coerce(val) |
| 122 | + |
| 123 | + assert 'Invalid element(s)' in str(validation_failure.value) |
| 124 | + |
| 125 | + |
| 126 | +# Array ok, numbers ok |
| 127 | +# -------------------- |
| 128 | +# ### Acceptance ### |
| 129 | +@pytest.mark.parametrize('val', |
| 130 | + ['blue', 23, [0, 1, 2], |
| 131 | + ['red', 0.5, 'rgb(255, 0, 0)'], |
| 132 | + ['hsl(0, 100%, 50%)', 'hsla(0, 100%, 50%, 100%)', 'hsv(0, 100%, 100%)'], |
| 133 | + ['hsva(0, 100%, 100%, 50%)']]) |
| 134 | +def test_acceptance_aok_colorscale(val, validator_aok_colorscale: ColorValidator): |
| 135 | + coerce_val = validator_aok_colorscale.validate_coerce(val) |
| 136 | + if isinstance(val, (list, np.ndarray)): |
| 137 | + expected = np.array( |
| 138 | + [str.replace(v.lower(), ' ', '') if isinstance(v, str) else v for v in val], |
| 139 | + dtype=coerce_val.dtype) |
| 140 | + assert np.array_equal(coerce_val, expected) |
| 141 | + else: |
| 142 | + expected = str.replace(val.lower(), ' ', '') if isinstance(val, str) else val |
| 143 | + assert coerce_val == expected |
| 144 | + |
| 145 | + |
| 146 | +# ### Rejection ### |
| 147 | +@pytest.mark.parametrize('val', |
| 148 | + [['redd', 0.5, 'rgb(255, 0, 0)'], |
| 149 | + ['hsl(0, 100%, 50_00%)', 'hsla(0, 100%, 50%, 100%)', 'hsv(0, 100%, 100%)'], |
| 150 | + ['hsva(0, 1%00%, 100%, 50%)']]) |
| 151 | +def test_rejection_aok_colorscale(val, validator_aok_colorscale: ColorValidator): |
| 152 | + with pytest.raises(ValueError) as validation_failure: |
| 153 | + validator_aok_colorscale.validate_coerce(val) |
| 154 | + |
| 155 | + assert 'Invalid element(s)' in str(validation_failure.value) |
| 156 | + |
| 157 | + |
| 158 | +# Description |
| 159 | +# ----------- |
| 160 | +# Test dynamic description logic |
| 161 | +def test_description(validator: ColorValidator): |
| 162 | + desc = validator.description() |
| 163 | + assert 'A number that will be interpreted as a color' not in desc |
| 164 | + assert 'A list or array of any of the above' not in desc |
| 165 | + |
| 166 | + |
| 167 | +def test_description_aok(validator_aok: ColorValidator): |
| 168 | + desc = validator_aok.description() |
| 169 | + assert 'A number that will be interpreted as a color' not in desc |
| 170 | + assert 'A list or array of any of the above' in desc |
| 171 | + |
| 172 | + |
| 173 | +def test_description_aok(validator_colorscale: ColorValidator): |
| 174 | + desc = validator_colorscale.description() |
| 175 | + assert 'A number that will be interpreted as a color' in desc |
| 176 | + assert 'A list or array of any of the above' not in desc |
| 177 | + |
| 178 | + |
| 179 | +def test_description_aok_colorscale(validator_aok_colorscale: ColorValidator): |
| 180 | + desc = validator_aok_colorscale.description() |
| 181 | + assert 'A number that will be interpreted as a color' in desc |
| 182 | + assert 'A list or array of any of the above' in desc |
0 commit comments