forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exceptions.py
63 lines (52 loc) · 1.75 KB
/
test_exceptions.py
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
"""Test to verify that Home Assistant exceptions work."""
from __future__ import annotations
import pytest
from homeassistant.exceptions import (
ConditionErrorContainer,
ConditionErrorIndex,
ConditionErrorMessage,
TemplateError,
)
def test_conditionerror_format() -> None:
"""Test ConditionError stringifiers."""
error1 = ConditionErrorMessage("test", "A test error")
assert str(error1) == "In 'test' condition: A test error"
error2 = ConditionErrorMessage("test", "Another error")
assert str(error2) == "In 'test' condition: Another error"
error_pos1 = ConditionErrorIndex("box", index=0, total=2, error=error1)
assert (
str(error_pos1)
== """In 'box' (item 1 of 2):
In 'test' condition: A test error"""
)
error_pos2 = ConditionErrorIndex("box", index=1, total=2, error=error2)
assert (
str(error_pos2)
== """In 'box' (item 2 of 2):
In 'test' condition: Another error"""
)
error_container1 = ConditionErrorContainer("box", errors=[error_pos1, error_pos2])
assert (
str(error_container1)
== """In 'box' (item 1 of 2):
In 'test' condition: A test error
In 'box' (item 2 of 2):
In 'test' condition: Another error"""
)
error_pos3 = ConditionErrorIndex("box", index=0, total=1, error=error1)
assert (
str(error_pos3)
== """In 'box':
In 'test' condition: A test error"""
)
@pytest.mark.parametrize(
("arg", "expected"),
[
("message", "message"),
(Exception("message"), "Exception: message"),
],
)
def test_template_message(arg: str | Exception, expected: str) -> None:
"""Ensure we can create TemplateError."""
template_error = TemplateError(arg)
assert str(template_error) == expected