Skip to content

Commit

Permalink
ci: Parameterize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgoette committed Jul 10, 2022
1 parent a814634 commit d9bef40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
5 changes: 4 additions & 1 deletion homeassistant/helpers/config_validation.py
Expand Up @@ -449,7 +449,10 @@ def time_period_str(value: str) -> timedelta:
day = 0
date_parsed = value.partition(" ")
if date_parsed[1]:
day = int(date_parsed[0])
try:
day = int(date_parsed[0])
except ValueError as err:
raise vol.Invalid(TIME_PERIOD_ERROR.format(value)) from err
value = date_parsed[2]

parsed = value.split(":")
Expand Down
39 changes: 26 additions & 13 deletions tests/helpers/test_config_validation.py
Expand Up @@ -1334,24 +1334,37 @@ def test_currency():
assert schema(value)


def test_time_period_str():
@pytest.mark.parametrize(
"time_period_str_pass",
(
("23:42", timedelta(hours=23, minutes=42)),
("-23:42:25", -timedelta(hours=23, minutes=42, seconds=25)),
("23:42:25.987654", timedelta(hours=23, minutes=42, seconds=25.987654)),
("1 23:42", timedelta(days=1, hours=23, minutes=42)),
("-1 23:42:25", -timedelta(days=1, hours=23, minutes=42, seconds=25)),
(
"1 23:42:25.987654",
timedelta(days=1, hours=23, minutes=42, seconds=25.987654),
),
),
)
def test_time_period_str_pass(time_period_str_pass):
"""Test time_period_str validator and transformer."""
schema = vol.Schema(cv.time_period_str)
assert cv.time_period_str(time_period_str_pass[0]) == time_period_str_pass[1]


for value in [
@pytest.mark.parametrize(
"time_period_str_fail",
(
None,
datetime.now().time(),
"2016-11-23",
"2016-11-23T18:59:08",
" 1 23:42:25.987654",
"1 1 23:42:25.987654",
]:
with pytest.raises(vol.MultipleInvalid):
schema(value)

schema("23:42")
schema("-23:42:25")
schema("23:42:25.987654")
schema("1 23:42")
schema("-1 23:42:25")
schema("1 23:42:25.987654")
),
)
def test_time_period_str_fail(time_period_str_fail):
"""Test time_period_str validator and transformer."""
with pytest.raises(vol.Invalid):
vol.Schema(cv.time_period_str(time_period_str_fail))

0 comments on commit d9bef40

Please sign in to comment.