-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully test the config schema changes
- Loading branch information
Showing
2 changed files
with
88 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,75 @@ | ||
import enum | ||
|
||
from zigpy_znp.config import EnumValue | ||
import pytest | ||
from voluptuous import Invalid | ||
|
||
import zigpy_znp.config as conf | ||
|
||
|
||
def test_EnumValue(): | ||
class TestEnum(enum.Enum): | ||
foo = 123 | ||
BAR = 456 | ||
|
||
assert EnumValue(TestEnum)("foo") == TestEnum.foo | ||
assert EnumValue(TestEnum)("BAR") == TestEnum.BAR | ||
assert conf.EnumValue(TestEnum)("foo") == TestEnum.foo | ||
assert conf.EnumValue(TestEnum)("BAR") == TestEnum.BAR | ||
|
||
assert conf.EnumValue(TestEnum, transformer=str.lower)("FOO") == TestEnum.foo | ||
assert conf.EnumValue(TestEnum, transformer=str.upper)("bar") == TestEnum.BAR | ||
|
||
assert conf.EnumValue(TestEnum)(TestEnum.foo) == TestEnum.foo | ||
assert conf.EnumValue(TestEnum)(TestEnum.BAR) == TestEnum.BAR | ||
|
||
|
||
assert ( | ||
EnumValue(TestEnum, transformer=lambda s: str(s).lower())("FOO") == TestEnum.foo | ||
def test_pin_states_same_lengths(): | ||
# Bare schema works | ||
conf.CONFIG_SCHEMA( | ||
{ | ||
conf.CONF_DEVICE: {conf.CONF_DEVICE_PATH: "/dev/null"}, | ||
conf.CONF_ZNP_CONFIG: {}, | ||
} | ||
) | ||
assert ( | ||
EnumValue(TestEnum, transformer=lambda s: str(s).upper())("bar") == TestEnum.BAR | ||
|
||
# So does one with explicitly specified pin states | ||
config = conf.CONFIG_SCHEMA( | ||
{ | ||
conf.CONF_DEVICE: {conf.CONF_DEVICE_PATH: "/dev/null"}, | ||
conf.CONF_ZNP_CONFIG: { | ||
conf.CONF_CONNECT_RTS_STATES: ["on", True, 0, 0, 0, 1, 1], | ||
conf.CONF_CONNECT_DTR_STATES: ["off", False, 1, 0, 0, 1, 1], | ||
}, | ||
} | ||
) | ||
|
||
assert EnumValue(TestEnum)(TestEnum.foo) == TestEnum.foo | ||
assert EnumValue(TestEnum)(TestEnum.BAR) == TestEnum.BAR | ||
assert config[conf.CONF_ZNP_CONFIG][conf.CONF_CONNECT_RTS_STATES] == [ | ||
True, | ||
True, | ||
False, | ||
False, | ||
False, | ||
True, | ||
True, | ||
] | ||
assert config[conf.CONF_ZNP_CONFIG][conf.CONF_CONNECT_DTR_STATES] == [ | ||
False, | ||
False, | ||
True, | ||
False, | ||
False, | ||
True, | ||
True, | ||
] | ||
|
||
|
||
def test_pin_states_different_lengths(): | ||
# They must be the same length | ||
with pytest.raises(Invalid): | ||
conf.CONFIG_SCHEMA( | ||
{ | ||
conf.CONF_DEVICE: {conf.CONF_DEVICE_PATH: "/dev/null"}, | ||
conf.CONF_ZNP_CONFIG: { | ||
conf.CONF_CONNECT_RTS_STATES: [1, 1, 0], | ||
conf.CONF_CONNECT_DTR_STATES: [1, 1], | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters