Skip to content

Commit c858cfe

Browse files
committed
Refactor tests for more coverage
1 parent 1ded4d4 commit c858cfe

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

tests/util/test_enum.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import TypeVar
4+
35
import pytest
46

57
from deebot_client.util.enum import IntEnumWithXml, StrEnumWithXml
@@ -11,49 +13,50 @@ class _TestStrEnumWithXml(StrEnumWithXml):
1113
ENUM1 = "value1", "xmlvalue1"
1214
ENUM2 = "value2", "xmlvalue2"
1315

16+
def assert_self_conversion(self) -> None:
17+
assert self == _TestStrEnumWithXml(self.value)
18+
assert self == _TestStrEnumWithXml.from_xml(self.xml_value)
19+
1420

1521
class _TestIntEnumWithXml(IntEnumWithXml):
1622
ENUM1 = 1, "xmlvalue1"
1723
ENUM2 = 2, "xmlvalue2"
1824

25+
def assert_self_conversion(self) -> None:
26+
assert self == _TestIntEnumWithXml(self.value)
27+
assert self == _TestIntEnumWithXml.from_xml(self.xml_value)
28+
29+
30+
T = TypeVar("T", _TestStrEnumWithXml, _TestIntEnumWithXml)
31+
V = TypeVar("V", str, int)
32+
1933

2034
@pytest.mark.parametrize(
21-
("test_enum", "test_value", "test_xml_value"),
35+
"test_enum",
2236
[
23-
(_TestStrEnumWithXml.ENUM1, "value1", "xmlvalue1"),
24-
(_TestStrEnumWithXml.ENUM2, "value2", "xmlvalue2"),
37+
_TestStrEnumWithXml.ENUM1,
38+
_TestStrEnumWithXml.ENUM2,
39+
_TestIntEnumWithXml.ENUM1,
40+
_TestIntEnumWithXml.ENUM2,
2541
],
2642
)
27-
def test_StrEnumWithXml_values(
28-
test_enum: _TestStrEnumWithXml, test_value: str, test_xml_value: str
43+
def test_EnumWithXml_conversion(
44+
test_enum: T,
2945
) -> None:
30-
assert test_enum.value == test_value
31-
assert test_enum.xml_value == test_xml_value
32-
assert test_value == _TestStrEnumWithXml.from_xml(test_xml_value).value
33-
assert test_xml_value == _TestStrEnumWithXml(test_value).xml_value
34-
35-
36-
def test_StrEnumWithXml_invalid_value() -> None:
37-
with pytest.raises(ValueError, match="this_is_invalid"):
38-
_TestStrEnumWithXml.from_xml("this_is_invalid")
46+
test_enum.assert_self_conversion()
3947

4048

4149
@pytest.mark.parametrize(
42-
("test_enum", "test_value", "test_xml_value"),
50+
("test_enum_cls", "invalid_value"),
4351
[
44-
(_TestIntEnumWithXml.ENUM1, 1, "xmlvalue1"),
45-
(_TestIntEnumWithXml.ENUM2, 2, "xmlvalue2"),
52+
(_TestStrEnumWithXml, "this_is_invalid"),
53+
(_TestStrEnumWithXml, None),
54+
(_TestIntEnumWithXml, "this_is_invalid"),
55+
(_TestIntEnumWithXml, None),
4656
],
4757
)
48-
def test_IntEnumWithXml_values(
49-
test_enum: _TestIntEnumWithXml, test_value: int, test_xml_value: str
58+
def test_EnumWithXml_invalid_value(
59+
test_enum_cls: type[T], invalid_value: str | None
5060
) -> None:
51-
assert test_enum.value == test_value
52-
assert test_enum.xml_value == test_xml_value
53-
assert test_value == _TestIntEnumWithXml.from_xml(test_xml_value).value
54-
assert test_xml_value == _TestIntEnumWithXml(test_value).xml_value
55-
56-
57-
def test_IntEnumWithXml_invalid_value() -> None:
58-
with pytest.raises(ValueError, match="this_is_invalid"):
59-
_TestIntEnumWithXml.from_xml("this_is_invalid")
61+
with pytest.raises(ValueError, match=str(invalid_value)):
62+
test_enum_cls.from_xml(invalid_value)

0 commit comments

Comments
 (0)