|
| 1 | +import pytest |
| 2 | +import string |
| 3 | + |
| 4 | +from hypothesis import given, example |
| 5 | +from hypothesis.strategies import one_of, text, integers, booleans, builds |
| 6 | + |
| 7 | +from esphome import config_validation |
| 8 | +from esphome.config_validation import Invalid |
| 9 | +from esphome.core import Lambda, HexInt |
| 10 | + |
| 11 | + |
| 12 | +def test_check_not_tamplatable__invalid(): |
| 13 | + with pytest.raises(Invalid, match="This option is not templatable!"): |
| 14 | + config_validation.check_not_templatable(Lambda("")) |
| 15 | + |
| 16 | + |
| 17 | +@given(one_of( |
| 18 | + booleans(), |
| 19 | + integers(), |
| 20 | + text(alphabet=string.ascii_letters + string.digits)), |
| 21 | +) |
| 22 | +def test_alphanumeric__valid(value): |
| 23 | + actual = config_validation.alphanumeric(value) |
| 24 | + |
| 25 | + assert actual == str(value) |
| 26 | + |
| 27 | + |
| 28 | +@given(value=text(alphabet=string.ascii_lowercase + string.digits + "_")) |
| 29 | +def test_valid_name__valid(value): |
| 30 | + actual = config_validation.valid_name(value) |
| 31 | + |
| 32 | + assert actual == value |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("value", ( |
| 36 | + "foo bar", "FooBar", "foo::bar" |
| 37 | +)) |
| 38 | +def test_valid_name__invalid(value): |
| 39 | + with pytest.raises(Invalid): |
| 40 | + config_validation.valid_name(value) |
| 41 | + |
| 42 | + |
| 43 | +@given(one_of(integers(), text())) |
| 44 | +def test_string__valid(value): |
| 45 | + actual = config_validation.string(value) |
| 46 | + |
| 47 | + assert actual == str(value) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("value", ( |
| 51 | + {}, [], True, False, None |
| 52 | +)) |
| 53 | +def test_string__invalid(value): |
| 54 | + with pytest.raises(Invalid): |
| 55 | + config_validation.string(value) |
| 56 | + |
| 57 | + |
| 58 | +@given(text()) |
| 59 | +def test_strict_string__valid(value): |
| 60 | + actual = config_validation.string_strict(value) |
| 61 | + |
| 62 | + assert actual == value |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("value", (None, 123)) |
| 66 | +def test_string_string__invalid(value): |
| 67 | + with pytest.raises(Invalid, match="Must be string, got"): |
| 68 | + config_validation.string_strict(value) |
| 69 | + |
| 70 | + |
| 71 | +@given(builds(lambda v: "mdi:" + v, text())) |
| 72 | +@example("") |
| 73 | +def test_icon__valid(value): |
| 74 | + actual = config_validation.icon(value) |
| 75 | + |
| 76 | + assert actual == value |
| 77 | + |
| 78 | + |
| 79 | +def test_icon__invalid(): |
| 80 | + with pytest.raises(Invalid, match="Icons should start with prefix"): |
| 81 | + config_validation.icon("foo") |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("value", ( |
| 85 | + "True", "YES", "on", "enAblE", True |
| 86 | +)) |
| 87 | +def test_boolean__valid_true(value): |
| 88 | + assert config_validation.boolean(value) is True |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("value", ( |
| 92 | + "False", "NO", "off", "disAblE", False |
| 93 | +)) |
| 94 | +def test_boolean__valid_false(value): |
| 95 | + assert config_validation.boolean(value) is False |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("value", ( |
| 99 | + None, 1, 0, "foo" |
| 100 | +)) |
| 101 | +def test_boolean__invalid(value): |
| 102 | + with pytest.raises(Invalid, match="Expected boolean value"): |
| 103 | + config_validation.boolean(value) |
| 104 | + |
| 105 | + |
| 106 | +# TODO: ensure_list |
| 107 | +@given(integers()) |
| 108 | +def hex_int__valid(value): |
| 109 | + actual = config_validation.hex_int(value) |
| 110 | + |
| 111 | + assert isinstance(actual, HexInt) |
| 112 | + assert actual == value |
| 113 | + |
0 commit comments