Coverage for tests/common/test_gen_utils.py: 100%
94 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 pytest
2import logging
3import re
4import sys
5import time
7from monero import GenUtils
8from utils import BaseTestClass
10logger: logging.Logger = logging.getLogger("TestGenUtils")
12_UUID_RE = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
15@pytest.mark.unit
16class TestGenUtils(BaseTestClass):
17 """Unit tests for GenUtils."""
19 #region uuid / wait_for / bool_equals
21 def test_get_uuid_format_and_uniqueness(self) -> None:
22 uuid1: str = GenUtils.get_uuid()
23 uuid2: str = GenUtils.get_uuid()
24 logger.debug(f"get_uuid(): {uuid1}, {uuid2}")
25 assert _UUID_RE.match(uuid1), f"not a UUID: {uuid1}"
26 assert _UUID_RE.match(uuid2), f"not a UUID: {uuid2}"
27 assert uuid1 != uuid2
29 def test_wait_for_blocks_for_at_least_duration(self) -> None:
30 # wait_for is std::this_thread::sleep_for in C++. On Windows it follows the timer of the system, which ticks every 15.6 ms, and it
31 # can return up to a tick before the time that was asked, however it is measured (49.8 ms for wait_for(50) was seen in CI,
32 # with perf_counter too). Everywhere else it is not shorter.
33 tolerance_ms: float = 16 if sys.platform == "win32" else 0
34 start: float = time.perf_counter()
35 GenUtils.wait_for(50)
36 elapsed_ms: float = (time.perf_counter() - start) * 1000
37 logger.debug(f"wait_for(50) actually took {elapsed_ms:.1f} ms")
38 assert elapsed_ms >= 50 - tolerance_ms
40 def test_wait_for_zero_does_not_block(self) -> None:
41 start: float = time.perf_counter()
42 GenUtils.wait_for(0)
43 elapsed_ms: float = (time.perf_counter() - start) * 1000
44 assert elapsed_ms < 50
46 def test_wait_for_negative_raises(self) -> None:
47 with pytest.raises(TypeError):
48 GenUtils.wait_for(-1)
50 def test_bool_equals(self) -> None:
51 assert GenUtils.bool_equals(True, True) is True
52 assert GenUtils.bool_equals(False, False) is True
53 assert GenUtils.bool_equals(True, False) is False
54 assert GenUtils.bool_equals(False, True) is False
55 # opt_val=None is documented to compare as False, even against val=False
56 assert GenUtils.bool_equals(True, None) is False
57 assert GenUtils.bool_equals(False, None) is False
59 #endregion
61 #region reconcile: equality and gap filling
63 def test_reconcile_uint64_equal_returns_value(self) -> None:
64 assert GenUtils.reconcile_uint64(5, 5) == 5
65 assert GenUtils.reconcile_uint64(None, None) is None
67 def test_reconcile_uint64_gap_fill(self) -> None:
68 assert GenUtils.reconcile_uint64(None, 5) == 5
69 assert GenUtils.reconcile_uint64(5, None) == 5
71 def test_reconcile_uint64_resolve_defined_false_returns_none_on_gap(self) -> None:
72 # resolve_defined=False overrides the default "fill the gap" behavior
73 assert GenUtils.reconcile_uint64(None, 5, resolve_defined=False) is None
74 assert GenUtils.reconcile_uint64(5, None, resolve_defined=False) is None
76 def test_reconcile_bool_equal_and_gap_fill(self) -> None:
77 assert GenUtils.reconcile_bool(True, True) is True
78 assert GenUtils.reconcile_bool(None, True) is True
79 assert GenUtils.reconcile_bool(False, None) is False
81 def test_reconcile_string_equal_and_gap_fill(self) -> None:
82 assert GenUtils.reconcile_string("x", "x") == "x"
83 assert GenUtils.reconcile_string(None, "x") == "x"
84 assert GenUtils.reconcile_string("x", None) == "x"
86 def test_reconcile_string_list_equal_and_gap_fill(self) -> None:
87 assert GenUtils.reconcile_string_list(["a", "b"], ["a", "b"]) == ["a", "b"]
88 assert GenUtils.reconcile_string_list([], ["a", "b"]) == ["a", "b"]
89 assert GenUtils.reconcile_string_list(["a", "b"], []) == ["a", "b"]
91 #endregion
93 #region reconcile: conflicts raise by design
95 def test_reconcile_uint64_conflict_raises(self) -> None:
96 with pytest.raises(RuntimeError, match="Cannot reconcile integrals"):
97 GenUtils.reconcile_uint64(3, 7)
99 def test_reconcile_bool_conflict_without_resolver_raises(self) -> None:
100 with pytest.raises(RuntimeError, match="Cannot reconcile integrals"):
101 GenUtils.reconcile_bool(True, False)
103 def test_reconcile_string_conflict_raises(self) -> None:
104 with pytest.raises(RuntimeError, match="Cannot reconcile strings"):
105 GenUtils.reconcile_string("a", "b")
107 def test_reconcile_string_list_conflict_raises(self) -> None:
108 with pytest.raises(RuntimeError, match="Cannot reconcile vectors"):
109 GenUtils.reconcile_string_list(["a"], ["b"])
111 def test_reconcile_string_resolve_true_is_ignored(self) -> None:
112 # unlike the bool/int overloads, the string overload accepts
113 # resolve_true/resolve_max for signature symmetry but never reads them
114 with pytest.raises(RuntimeError, match="Cannot reconcile strings"):
115 GenUtils.reconcile_string("a", "b", resolve_true=True)
117 #endregion
119 #region reconcile: resolve_max picks the numeric extreme
121 def test_reconcile_uint64_resolve_max_true_picks_greater(self) -> None:
122 assert GenUtils.reconcile_uint64(3, 7, resolve_max=True) == 7
123 assert GenUtils.reconcile_uint64(7, 3, resolve_max=True) == 7
125 def test_reconcile_uint64_resolve_max_false_picks_lesser(self) -> None:
126 assert GenUtils.reconcile_uint64(3, 7, resolve_max=False) == 3
127 assert GenUtils.reconcile_uint64(7, 3, resolve_max=False) == 3
129 #endregion
131 #region reconcile values
133 def test_reconcile_bool_resolve_true_prefers_the_true_operand(self) -> None:
134 # val1=False, val2=True, resolve_true=True -> should prefer the
135 # operand that IS true, i.e. val2
136 result: bool | None = GenUtils.reconcile_bool(False, True, resolve_true=True)
137 logger.debug(f"reconcile_bool(False, True, resolve_true=True) = {result}")
138 assert result is True
140 def test_reconcile_bool_resolve_true_false_prefers_the_false_operand(self) -> None:
141 # val1=False, val2=True, resolve_true=False -> should prefer the
142 # operand that IS false, i.e. val1
143 result: bool | None = GenUtils.reconcile_bool(False, True, resolve_true=False)
144 logger.debug(f"reconcile_bool(False, True, resolve_true=False) = {result}")
145 assert result is False
147 def test_reconcile_uint64_resolve_true_prefers_the_true_operand(self) -> None:
148 # val1=0 (falsy), val2=1 (truthy), resolve_true=True -> should prefer
149 # val2 since it's the operand whose bool cast is True
150 result: int | None = GenUtils.reconcile_uint64(0, 1, resolve_true=True)
151 logger.debug(f"reconcile_uint64(0, 1, resolve_true=True) = {result}")
152 assert result == 1
154 #endregion