File: test_readers.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (386 lines) | stat: -rw-r--r-- 11,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""Test AsusRouter readers tools."""

from typing import Any
from unittest.mock import patch

import pytest

from asusrouter.const import ContentType
from asusrouter.tools import readers
from asusrouter.tools.units import (
    DataRateUnitConverter,
    UnitConverterBase,
    UnitOfDataRate,
)


class MockConverter(UnitConverterBase):
    """Mock converter for testing."""


@pytest.mark.parametrize(
    ("value", "result"),
    [
        # Any float-compatible value should pass if not
        # smaller than zero
        (1.0, True),
        ("0", True),
        (123132, True),
        # Negatives should fail
        (-1.0, False),
        ("-1", False),
        # Non-comparable types should pass, since not negative
        (None, True),
        (object(), True),
        ({}, True),
    ],
)
def test_is_non_negative(value: Any, result: bool) -> None:
    """Test is_non_negative method."""

    assert readers.is_non_negative(value) is result


@pytest.mark.parametrize(
    ("dict1", "dict2", "expected"),
    [
        # Test non-nested dictionaries
        ({"a": 1, "b": 2}, {"b": 3, "c": 4}, {"a": 1, "b": 2, "c": 4}),
        # Test nested dictionaries
        (
            {"a": 1, "b": {"x": 2}},
            {"b": {"y": 3}, "c": 4},
            {"a": 1, "b": {"x": 2, "y": 3}, "c": 4},
        ),
        # Test with None values
        ({"a": None, "b": 2}, {"a": 1, "b": None}, {"a": 1, "b": 2}),
        ({"a": None}, {"a": {"b": 1}}, {"a": {"b": 1}}),
    ],
)
def test_merge_dicts(
    dict1: dict[str, Any], dict2: dict[str, Any], expected: dict[str, Any]
) -> None:
    """Test merge_dicts method."""

    assert readers.merge_dicts(dict1, dict2) == expected


@pytest.mark.parametrize(
    ("content", "expected"),
    [
        ("Test string", "test_string"),  # Upper case
        (
            "test with  special ^$@ characters",
            "test_with_special_characters",
        ),  # Special characters
        ("snake_case", "snake_case"),  # Already snake case
    ],
)
def test_read_as_snake_case(content: str, expected: str) -> None:
    """Test read_as_snake_case method."""

    assert readers.read_as_snake_case(content) == expected


@pytest.mark.parametrize(
    ("headers", "expected"),
    [
        ({"content-type": "application/json;charset=UTF-8"}, ContentType.JSON),
        ({"content-type": "application/xml"}, ContentType.XML),
        ({"content-type": "text/html"}, ContentType.HTML),
        ({"content-type": "text/plain"}, ContentType.TEXT),
        ({"content-type": "application/octet-stream"}, ContentType.BINARY),
        ({"content-type": "application/unknown"}, ContentType.UNKNOWN),
        ({}, ContentType.UNKNOWN),
    ],
)
def test_read_content_type(
    headers: dict[str, str], expected: ContentType
) -> None:
    """Test read_content_type method."""

    assert readers.read_content_type(headers) == expected


@pytest.mark.parametrize(
    ("content", "expected"),
    [
        # JS from the active temperature sensors
        (
            'curr_coreTmp_wl0_raw = "44°C";',
            {"curr_coreTmp_wl0_raw": "44°C"},
        ),
        # JS from the disabled temperature sensors
        (
            'curr_coreTmp_wl2_raw = "<i>disabled</i>";',
            {"curr_coreTmp_wl2_raw": "<i>disabled</i>"},
        ),
        # JS from the VPN
        ('vpn_client1_status = "None";', {"vpn_client1_status": "None"}),
        # JSON variables
        (
            "get_onboardinglist = [{}][0];",
            {"get_onboardinglist": [{}]},
        ),
        # Value with array index, fallback to string
        ("arr = value[0];", {"arr": "value"}),
        # Quoted string, not valid JSON or Python literal
        ("foo = 'bar';", {"foo": "bar"}),
        ('baz = "qux";', {"baz": "qux"}),
        # Unquoted string, not valid JSON or Python literal
        ("plain = not_json;", {"plain": "not_json"}),
        # Extra quotes
        ('extra = ""quoted"";', {"extra": '"quoted"'}),
        # Multi-line variable
        (
            (
                "multi_line = {\n"
                '    "key1": "value1",\n'
                "    \n"
                '    "key2": "value2"\n'
                "};"
            ),
            {
                "multi_line": {
                    "key1": "value1",
                    "key2": "value2",
                }
            },
        ),
        (
            ('if (true) {\n    conditional = "value";\n};'),
            {"conditional": "value"},
        ),
        # Weird formatting
        (("something var=42;"), {"var": 42}),
        (("something var='4;2';"), {"var": "4;2"}),
        (('var="val42";'), {"var": "val42"}),
        (('var="val;42";'), {"var": "val;42"}),
        (('if(True){var="val42";}'), {"var": "val42"}),
        ('if(True){ var="value;amp"; }', {"var": "value;amp"}),
        (
            ('if(True){var="val;42";}'),
            {"var": "val;42"},
        ),
        # Other cases
        ('var="val\\"42";', {"var": 'val"42'}),
        ("var=42; // comment", {"var": 42}),
        ('var="foo"; /* block comment */', {"var": "foo"}),
        ("a=1; b=2;", {"a": 1, "b": 2}),
        ('   var =   "foo"   ;   ', {"var": "foo"}),
        ("var=;", {"var": ""}),
        # Non-string values
        ("num=123;", {"num": 123}),
        ("flag=true;", {"flag": True}),
    ],
)
def test_read_js_variables(content: str, expected: dict[str, Any]) -> None:
    """Test read_js_variables method."""

    assert readers.read_js_variables(content) == expected


@pytest.mark.parametrize(
    ("content", "expected"),
    [
        # JSON from ethernet ports
        (
            (
                '{ "portSpeed": { "WAN 0": "G", "LAN 1": "G", "LAN 2": "G", '
                '"LAN 3": "G", "LAN 4": "X", "LAN 5": "X", "LAN 6": "X", '
                '"LAN 7": "X", "LAN 8": "G" }, "portCount": { "wanCount": 1, '
                '"lanCount": 8 } }'
            ),
            {
                "portSpeed": {
                    "WAN 0": "G",
                    "LAN 1": "G",
                    "LAN 2": "G",
                    "LAN 3": "G",
                    "LAN 4": "X",
                    "LAN 5": "X",
                    "LAN 6": "X",
                    "LAN 7": "X",
                    "LAN 8": "G",
                },
                "portCount": {"wanCount": 1, "lanCount": 8},
            },
        ),
        # JSON from sysinfo
        (
            (
                '{"wlc_0_arr":["11", "11", "11"],'
                '"wlc_1_arr":["2", "2", "2"],'
                '"wlc_2_arr":["0", "0", "0"],'
                '"wlc_3_arr":["0", "0", "0"],'
                '"conn_stats_arr":["394","56"],'
                '"mem_stats_arr":["882.34", "395.23",'
                '"0.00", "52.64", "0.00", "0.00", "85343", "7.61 / 63.00 MB"],'
                '"cpu_stats_arr":["2.18", "2.09", "2.03"]}'
            ),
            {
                "wlc_0_arr": ["11", "11", "11"],
                "wlc_1_arr": ["2", "2", "2"],
                "wlc_2_arr": ["0", "0", "0"],
                "wlc_3_arr": ["0", "0", "0"],
                "conn_stats_arr": ["394", "56"],
                "mem_stats_arr": [
                    "882.34",
                    "395.23",
                    "0.00",
                    "52.64",
                    "0.00",
                    "0.00",
                    "85343",
                    "7.61 / 63.00 MB",
                ],
                "cpu_stats_arr": ["2.18", "2.09", "2.03"],
            },
        ),
        # Test valid JSON content
        ('{"key": "value"}', {"key": "value"}),
        # Test empty content
        (None, {}),
        # Test invalid JSON content
        ("not a json", {}),
        # Test missing values
        (
            '{ , "key1": "value1", , "key2": "value2", }',
            {"key1": "value1", "key2": "value2"},
        ),
        # Test keys without values
        (
            '{"key1": "value1", "key2": , "key3": "value3", "key4": ,}',
            {"key1": "value1", "key2": None, "key3": "value3", "key4": None},
        ),
    ],
)
def test_read_json_content(
    content: str | None, expected: dict[str, Any]
) -> None:
    """Test read_json_content method."""

    assert readers.read_json_content(content) == expected


def test_read_json_content_fail() -> None:
    """Test read_json_content method with invalid JSON response."""

    with patch(
        "asusrouter.tools.readers.json.loads", return_value="some value"
    ):
        assert readers.read_json_content("invalid json") == {}


@pytest.mark.parametrize(
    ("content", "expected"),
    [
        # Test valid MAC addresses
        ("01:23:45:67:89:AB", True),
        ("01-23-45-67-89-AB", True),
        # Test invalid MAC addresses
        ("01:23:45:67:89-87-65", False),
        ("01-23-45-67-89", False),
        ("01:23:45:67:89:ZZ", False),
        ("   ", False),
        # Test non-string input
        (1234567890, False),
        (None, False),
    ],
)
def test_readable_mac(content: str | None, expected: bool) -> None:
    """Test readable_mac method."""

    assert readers.readable_mac(content) == expected


def test_read_units_as_base_wrong_converter() -> None:
    """Test read_units_as_base with a wrong converter."""

    with pytest.raises(TypeError, match="Converter must be"):
        readers.read_units_as_base("not a converter", "some_units")  # type: ignore[arg-type]


@pytest.mark.parametrize("mode", ["none", "single", "list"])
def test_read_units_as_base_checks_pass(mode: str) -> None:
    """Reader should call converter.convert_to_base when checks pass."""

    check_calls: Any

    with patch.object(
        MockConverter, "convert_to_base", return_value=1
    ) as mock_convert_to_base:
        if mode == "none":
            check_calls = None
        elif mode == "single":

            def is_positive(v: float) -> bool:
                return v > 0

            check_calls = is_positive
        else:

            def is_positive(v: float) -> bool:
                return v > 0

            def less_than_1(v: float) -> bool:
                return v < 1

            check_calls = [is_positive, less_than_1]

        reader = readers.read_units_as_base(
            MockConverter, "u", check_calls=check_calls, fallback_value=0.0
        )

        result = reader(0.5)
        mock_convert_to_base.assert_called_once_with(0.5, "u")
        assert result == 1


def test_read_units_as_base_checks_fail() -> None:
    """Reader should return fallback value when checks fail."""

    fallback_value = 0.0

    with patch.object(
        MockConverter, "convert_to_base", return_value=1
    ) as mock_convert_to_base:
        check_calls = [lambda v: v > 0, lambda v: v < 1]

        reader = readers.read_units_as_base(
            MockConverter,
            "u",
            check_calls=check_calls,
            fallback_value=fallback_value,
        )

        result = reader(1.5)
        mock_convert_to_base.assert_not_called()
        assert result == fallback_value


def test_read_units_data_rate() -> None:
    """Test read_units_data_rate method."""

    input_value = 42.0

    def return_function(x: Any) -> Any:
        """Return the input value."""

        return x

    with patch(
        "asusrouter.tools.readers.read_units_as_base",
        return_value=return_function,
    ) as mock_read_units_as_base:
        result = readers.read_units_data_rate(
            UnitOfDataRate.MEBIBIT_PER_SECOND
        )

        assert input_value == result(input_value)
        mock_read_units_as_base.assert_called_once_with(
            DataRateUnitConverter,
            UnitOfDataRate.MEBIBIT_PER_SECOND,
            readers.is_non_negative,
            0.0,
        )