Debian Codegraph @

tests/modules/endpoint/test_temperature.py

raw
1 """Tests for the Temperature endpoint module."""
2
3 from typing import Any
4 from unittest.mock import call, patch
5
6 import pytest
7
8 from asusrouter.config import ARConfig, ARConfigKey as ARConfKey
9 from asusrouter.modules.data import AsusData
10 from asusrouter.modules.endpoint import temperature as temp_mod
11 from asusrouter.modules.endpoint.temperature import _scale_temperature, process
12 from asusrouter.modules.wlan import Wlan
13
14
15 @pytest.fixture(autouse=True)
16 def reset_config() -> None:
17 """Reset the configuration before each test."""
18
19 ARConfig.set(ARConfKey.OPTIMISTIC_TEMPERATURE, False)
20
21
22 @pytest.mark.parametrize(
23 ("mock_vars", "expected", "optimistic", "scaled"),
24 [
25 # Normal values, no scaling
26 (
27 {
28 "curr_coreTmp_2_raw": "42",
29 "curr_coreTmp_5_raw": "43",
30 "curr_coreTmp_52_raw": "44",
31 "curr_coreTmp_cpu": "45",
32 },
33 {
34 Wlan.FREQ_2G: 42.0,
35 Wlan.FREQ_5G: 43.0,
36 Wlan.FREQ_5G2: 44.0,
37 "cpu": 45.0,
38 },
39 False,
40 False,
41 ),
42 # Values need scaling
43 (
44 {
45 "curr_coreTmp_0_raw": "0.04",
46 "curr_coreTmp_1_raw": "0.05",
47 "curr_coreTmp_2_raw": "0.06",
48 "curr_coreTmp_3_raw": "0.07",
49 "curr_coreTmp_cpu": "0.08",
50 },
51 {
52 Wlan.FREQ_2G: 40.0,
53 Wlan.FREQ_5G: 50.0,
54 Wlan.FREQ_5G2: 60.0,
55 Wlan.FREQ_6G: 70.0,
56 "cpu": 80.0,
57 },
58 True,
59 True,
60 ),
61 # Disabled value
62 (
63 {
64 "curr_coreTmp_wl0_raw": "disabled",
65 "curr_coreTmp_wl1_raw": "43",
66 "curr_coreTmp_wl2_raw": "44",
67 "curr_coreTmp_wl3_raw": "45",
68 "curr_coreTmp_cpu": "46",
69 },
70 {
71 Wlan.FREQ_5G: 43.0,
72 Wlan.FREQ_5G2: 44.0,
73 Wlan.FREQ_6G: 45.0,
74 "cpu": 46.0,
75 },
76 False,
77 False,
78 ),
79 # CPU temp only
80 (
81 {"curr_cpuTemp": "55"},
82 {"cpu": 55.0},
83 False,
84 False,
85 ),
86 ],
87 )
88 def test_read_mocked(
89 mock_vars: dict[str, str | None],
90 expected: dict[str, float],
91 optimistic: bool,
92 scaled: bool,
93 ) -> None:
94 """Test the read function."""
95
96 ARConfig.set(ARConfKey.OPTIMISTIC_TEMPERATURE, optimistic)
97
98 with (
99 patch.object(
100 temp_mod, "clean_content", side_effect=lambda x: x
101 ) as mock_clean,
102 patch.object(
103 temp_mod, "read_js_variables", side_effect=lambda x: mock_vars
104 ) as mock_js,
105 patch.object(
106 temp_mod,
107 "safe_float",
108 side_effect=lambda x: float(x) if x != "disabled" else None,
109 ) as mock_float,
110 patch.object(
111 temp_mod,
112 "_scale_temperature",
113 side_effect=lambda temp: (expected, scaled),
114 ) as mock_scale,
115 patch.object(
116 temp_mod,
117 "ensure_notification_flag",
118 return_value=None,
119 ) as mock_notify,
120 ):
121 result = temp_mod.read("dummy_content")
122 assert result == expected
123
124 # Check that clean_content and read_js_variables are called once
125 mock_clean.assert_called_once_with("dummy_content")
126 mock_js.assert_called_once_with("dummy_content")
127
128 # safe_float should be called for each value except "disabled"
129 float_calls = [
130 call(v)
131 for v in mock_vars.values()
132 if v is not None and v != "disabled"
133 ]
134 mock_float.assert_has_calls(float_calls, any_order=True)
135
136 # _scale_temperature should be called only if optimistic is True
137 if optimistic:
138 mock_notify.assert_called_once()
139 mock_scale.assert_called_once()
140 else:
141 mock_notify.assert_not_called()
142 mock_scale.assert_not_called()
143
144
145 @pytest.mark.parametrize(
146 "input",
147 [
148 {"val1": 30.0},
149 {"val1": 30.0, "val2": 40.0},
150 {"val1": "anything", "val2": None},
151 ],
152 )
153 def test_process(input: dict[str, Any]) -> None:
154 """Test the process function."""
155
156 result = process(input)
157 assert result.get(AsusData.TEMPERATURE) == input
158
159
160 @pytest.mark.parametrize(
161 ("temperature", "result_temperature", "result_scaled"),
162 [
163 # No scaling needed
164 ({"val1": 30.0, "val2": 40.0}, {"val1": 30.0, "val2": 40.0}, False),
165 # Scaling up needed (3 orders of magnitude off down)
166 ({"val1": 0.03, "val2": 0.04}, {"val1": 30.0, "val2": 40.0}, True),
167 # Scaling down needed (3 orders of magnitude off up)
168 (
169 {"val1": 30000.0, "val2": 40000.0},
170 {"val1": 30.0, "val2": 40.0},
171 True,
172 ),
173 # None in the input should be ignored
174 ({"val1": None}, {}, False),
175 # Scaling needed for 6 orders of magnitude -> this should not happen
176 # fully - we allow only 5 steps of scaling
177 ({"val1": 0.00003, "val2": 0.00004}, {"val1": 3.0, "val2": 4.0}, True),
178 (
179 {"val1": 30000000.0, "val2": 40000000.0},
180 {"val1": 300.0, "val2": 400.0},
181 True,
182 ),
183 ],
184 )
185 def test_scale_temperature(
186 temperature: dict[str, float | None],
187 result_temperature: dict[str, float],
188 result_scaled: bool,
189 ) -> None:
190 """Test the _scale_temperature function."""
191
192 scaled_temperature, scaled = _scale_temperature(temperature)
193 assert scaled_temperature == result_temperature
194 assert scaled == result_scaled
195 scaled_temperature, scaled = _scale_temperature(temperature)
196 assert scaled_temperature == result_temperature
197 assert scaled == result_scaled