Coverage for tests/utils/assert_utils.py: 95%
42 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1import logging
3from abc import ABC
4from typing import Any
5from monero import SerializableStruct, MoneroRpcConnection
7logger: logging.Logger = logging.getLogger("AssertUtils")
10class AssertUtils(ABC):
11 """Assert utilities."""
13 @classmethod
14 def assert_equals(cls, expr1: Any, expr2: Any, message: str = "assertion failed") -> None:
15 """Check for objects equality.
17 :param Any expr1: first object.
18 :param Any expr2: second object.
19 :param str message: failure message.
20 """
21 if isinstance(expr1, MoneroRpcConnection) and isinstance(expr2, MoneroRpcConnection):
22 assert expr1.uri == expr2.uri
23 assert expr1.username == expr2.username
24 assert expr1.password == expr2.password
25 assert expr1.proxy_uri == expr2.proxy_uri
26 assert expr1.priority == expr2.priority
27 assert expr1.timeout_ms == expr2.timeout_ms
28 elif isinstance(expr1, SerializableStruct) and isinstance(expr2, SerializableStruct):
29 str1: str = expr1.serialize()
30 str2: str = expr2.serialize()
31 assert str1 == str2, f"{message}: {str1} == {str2}"
32 else:
33 assert expr1 == expr2, f"{message}: {expr1} == {expr2}"
35 @classmethod
36 def assert_list_equals[T](cls, list_1: list[T] | None, list_2: list[T] | None, message: str = "lists doesn't equal") -> None:
37 """Check for lists equality.
39 :param list[T] | None list_1: first list to assert equality with `list_2`.
40 :param list[T] | None list_2: second list to assert equality with `list_1`.
41 :param str message: failure message.
42 :raises AssertionError: raises if `list_1` is not equal to `list_2`.
43 """
44 if list_1 == list_2:
45 return
46 elif list_1 is None and list_2 is None:
47 # TODO raise AssertError?
48 return
50 assert list_1 is not None, "list_1 is None"
51 assert list_2 is not None, "list_2 is None"
52 size1: int = len(list_1)
53 size2: int = len(list_2)
54 assert size1 == size2, f"Lists size mismatch: (list_1) {size1} != (list_2) {size2}"
55 for i, elem1 in enumerate(list_1):
56 elem2: T = list_2[i]
57 cls.assert_equals(elem1, elem2, message)
59 @classmethod
60 def assert_serialization_integrity[T: SerializableStruct](cls, obj: T) -> T:
61 """Serialize obj, deserialize it back through the model's own from_property_tree
62 binding, and assert the result matches the original field for field.
64 :param T obj: object to verity serialization integrity.
65 :return T: new deserialized object.
66 """
67 cls = type(obj)
68 json_str: str = obj.serialize()
69 logger.debug(f"Serialized {cls.__name__}: {json_str}")
70 restored: Any = cls.deserialize(json_str) # type: ignore
71 AssertUtils.assert_equals(obj, restored)
72 return restored # type: ignore