Debian Codegraph @

tests/tools/test_units.py

raw
1 """Tests for the unit tools."""
2
3 from enum import StrEnum
4 from typing import Any
5 from unittest.mock import patch
6
7 import pytest
8
9 from asusrouter.error import AsusRouterError
10 from asusrouter.tools.units import UnitConverterBase
11
12
13 class MockUnits(StrEnum):
14 """Mock units for testing."""
15
16 BASE = "base"
17 MEGABASE = "megabase"
18 TERABASE = "terabase"
19
20
21 MOCK_UNIT_CLASS = "mock_units"
22
23 MOCK_UNIT_RATIOS: dict[StrEnum, float] = {
24 MockUnits.BASE: 1,
25 MockUnits.MEGABASE: 10**6,
26 MockUnits.TERABASE: 10**12,
27 }
28
29
30 class TestUnitConverter(UnitConverterBase):
31 """Test unit converter base."""
32
33 UNIT_CLASS = MOCK_UNIT_CLASS
34 UNIT_BASE = MockUnits.BASE
35 VALUE_UNITS = set(MockUnits)
36 _UNIT_RATIO = MOCK_UNIT_RATIOS
37
38 @pytest.mark.parametrize(
39 "unit",
40 [
41 MockUnits.BASE,
42 MockUnits.MEGABASE,
43 MockUnits.TERABASE,
44 ],
45 )
46 def test_validate_unit(self, unit: MockUnits) -> None:
47 """Test valid units pass validation."""
48
49 # Should just pass
50 self._validate_unit(unit)
51
52 @pytest.mark.parametrize(
53 "unit",
54 [
55 "not_a_unit",
56 None,
57 1.123,
58 ],
59 )
60 def test_invalid_unit(self, unit: Any) -> None:
61 """Test invalid units raise validation errors."""
62
63 with pytest.raises(AsusRouterError, match="Unknown unit"):
64 self._validate_unit(unit)
65
66 def test_convert(self) -> None:
67 """Test the convert method."""
68
69 unit1 = MockUnits.BASE
70 unit2 = MockUnits.MEGABASE
71
72 with (
73 patch(
74 "asusrouter.tools.units.UnitConverterBase._validate_unit",
75 ) as mock_validate_unit,
76 patch(
77 "asusrouter.tools.units.UnitConverterBase.converter_factory",
78 ) as mock_converter_factory,
79 ):
80 self.convert(1.0, unit1, unit2)
81
82 assert mock_validate_unit.call_count == 2 # noqa: PLR2004
83 assert mock_validate_unit.call_args_list[0][0] == (unit1,)
84 assert mock_validate_unit.call_args_list[1][0] == (unit2,)
85
86 mock_converter_factory.assert_called_once_with(unit1, unit2)
87
88 def test_convert_to_base(self) -> None:
89 """Test the convert_to_base method."""
90
91 # Clear the cache
92 self.converter_factory.cache_clear()
93
94 value = 1.0
95 unit = MockUnits.MEGABASE
96
97 with (
98 patch(
99 "asusrouter.tools.units.UnitConverterBase._validate_unit",
100 ) as mock_validate_unit,
101 patch(
102 "asusrouter.tools.units.UnitConverterBase.converter_factory",
103 ) as mock_converter_factory,
104 ):
105 result = self.convert_to_base(value, unit)
106
107 mock_validate_unit.assert_called_once_with(unit)
108 mock_converter_factory.assert_called_once_with(
109 unit, self.UNIT_BASE
110 )
111
112 assert result == mock_converter_factory.return_value(value)
113
114 def test_convert_to_base_already_base(self) -> None:
115 """Test the convert_to_base method when the unit is the base unit."""
116
117 value = 1.0
118 unit = MockUnits.BASE
119
120 with (
121 patch(
122 "asusrouter.tools.units.UnitConverterBase._validate_unit",
123 ) as mock_validate_unit,
124 patch(
125 "asusrouter.tools.units.UnitConverterBase.converter_factory",
126 ) as mock_converter_factory,
127 ):
128 result = self.convert_to_base(value, unit)
129
130 mock_validate_unit.assert_called_once_with(unit)
131 mock_converter_factory.assert_not_called()
132
133 assert result == value
134
135 def test_converter_factory(self) -> None:
136 """Test the converter factory method."""
137
138 # Clear the cache
139 self.converter_factory.cache_clear()
140
141 unit1 = MockUnits.BASE
142 unit2 = MockUnits.MEGABASE
143
144 with patch(
145 "asusrouter.tools.units.UnitConverterBase._get_ratios",
146 return_value=(1.0, 2.0),
147 ) as mock_get_ratios:
148 converter = self.converter_factory(unit1, unit2)
149
150 mock_get_ratios.assert_called_once_with(unit1, unit2)
151 assert converter is not None
152
153 def test_converter_factory_identity_returns_identity(self) -> None:
154 """When same units."""
155
156 # Clear the cache
157 self.converter_factory.cache_clear()
158
159 fn = self.converter_factory(MockUnits.BASE, MockUnits.BASE)
160 assert callable(fn)
161 assert fn(1.234) == pytest.approx(1.234)
162
163 def test_converter_factory_uses_get_ratios_and_is_cached(self) -> None:
164 """converter_factory should call _get_ratios once and be cached."""
165
166 # Clear the cache
167 self.converter_factory.cache_clear()
168
169 # arrange: make _get_ratios return predictable ratios
170 with patch(
171 "asusrouter.tools.units.UnitConverterBase._get_ratios",
172 return_value=(2.0, 4.0),
173 ) as mock_get:
174 fn = self.converter_factory(MockUnits.BASE, MockUnits.MEGABASE)
175
176 # _get_ratios must have been called once with the same enum args
177 mock_get.assert_called_once_with(
178 MockUnits.BASE, MockUnits.MEGABASE
179 )
180
181 # function must compute (value * from_ratio) / to_ratio
182 assert fn(3.0) == pytest.approx((3.0 * 2.0) / 4.0)
183
184 # reset mock and call converter_factory again with the same args:
185 # due to lru_cache it should be cached ->
186 mock_get.reset_mock()
187 fn2 = self.converter_factory(MockUnits.BASE, MockUnits.MEGABASE)
188 mock_get.assert_not_called()
189 assert fn2 is fn
190
191 def test_get_ratios(self) -> None:
192 """Test the _get_ratios method."""
193
194 unit1 = MockUnits.BASE
195 unit2 = MockUnits.MEGABASE
196
197 ratios = self._get_ratios(unit1, unit2)
198 assert ratios == (MOCK_UNIT_RATIOS[unit1], MOCK_UNIT_RATIOS[unit2])
199
200 def test_get_ratios_not_defined_ratio(self) -> None:
201 """Test the _get_ratios method when the ratio is not defined."""
202
203 unit1 = "Not a unit"
204 unit2 = MockUnits.MEGABASE
205
206 with pytest.raises(AsusRouterError, match="Unknown unit"):
207 self._get_ratios(unit1, unit2) # type: ignore[arg-type]
208
209 def test_get_unit_ratio(self) -> None:
210 """Test the get_unit_ratio method."""
211
212 unit1 = MockUnits.BASE
213 unit2 = MockUnits.MEGABASE
214
215 with patch(
216 "asusrouter.tools.units.UnitConverterBase._get_ratios",
217 return_value=(2.0, 4.0),
218 ) as mock_get_ratios:
219 ratio = self.get_unit_ratio(unit1, unit2)
220
221 mock_get_ratios.assert_called_once_with(unit1, unit2)
222 assert ratio == 2.0 / 4.0