Debian Codegraph @

tests/tools/test_writers.py

raw
1 """Test AsusRouter writers tools."""
2
3 from collections.abc import Mapping
4 from typing import Any
5 from unittest.mock import Mock, patch
6
7 import pytest
8
9 from asusrouter.config import (
10 CONFIG_DEFAULT_ALREADY_NOTIFIED,
11 ARConfigBase,
12 ARConfigKeyBase,
13 )
14 from asusrouter.const import RequestType
15 from asusrouter.tools import writers
16 from asusrouter.tools.identifiers import MacAddress
17
18
19 class MockKey(ARConfigKeyBase):
20 """Test keys."""
21
22 EXISTING = "existing"
23 ANY = "any"
24 SOME = "some"
25
26
27 @pytest.mark.parametrize(
28 ("content", "expected"),
29 [
30 ("test", "nvram_get(test);"),
31 (["test1", "test2"], "nvram_get(test1);nvram_get(test2);"),
32 (None, None),
33 ],
34 )
35 def test_nvram(content: str | list[str] | None, expected: str | None) -> None:
36 """Test nvram method."""
37
38 assert writers.nvram(content) == expected
39
40
41 @pytest.mark.parametrize(
42 ("data", "request_type", "expected"),
43 [
44 ({"key": "value"}, RequestType.POST, "'key':'value'"),
45 ({"key": "value"}, RequestType.GET, "key=value"),
46 (
47 {"key1": "value1", "key2": "value2"},
48 RequestType.POST,
49 "'key1':'value1';'key2':'value2'",
50 ),
51 (
52 {"key1": "value1", "key2": "value2"},
53 RequestType.GET,
54 "key1=value1&key2=value2",
55 ),
56 ({}, RequestType.POST, ""),
57 ({}, RequestType.GET, ""),
58 (
59 {"mac": MacAddress("aa:bb:cc:dd:ee:ff")},
60 RequestType.POST,
61 "'mac':'AA:BB:CC:DD:EE:FF'",
62 ),
63 (
64 {"key1": True, "key2": False},
65 RequestType.POST,
66 "'key1':'1';'key2':'0'",
67 ),
68 # edge cases
69 ({"a b": "c d"}, RequestType.GET, "a+b=c+d"),
70 ({"sym": "&=+?"}, RequestType.GET, "sym=%26%3D%2B%3F"),
71 ({"flag": True}, RequestType.GET, "flag=1"),
72 ({"k'": "v'"}, RequestType.POST, "'k\\'':'v\\''"),
73 ],
74 ids=[
75 "post-simple-single",
76 "get-simple-single",
77 "post-simple-multiple",
78 "get-simple-multiple",
79 "empty-post",
80 "empty-get",
81 "MacAddress",
82 "bool-post",
83 "get-quoting-spaces",
84 "get-quoting-special-chars",
85 "get-bool",
86 "post-escape-single-quotes",
87 ],
88 )
89 def test_dict_to_request(
90 data: Mapping[str, Any], request_type: RequestType, expected: str | None
91 ) -> None:
92 """Test dict_to_request method."""
93
94 assert writers.dict_to_request(data, request_type) == expected
95
96
97 @pytest.mark.parametrize(
98 ("key", "called"),
99 [
100 (MockKey.EXISTING, False),
101 (MockKey.ANY, True),
102 (MockKey.SOME, True),
103 ],
104 )
105 def test_ensure_notification_flag(key: ARConfigKeyBase, called: bool) -> None:
106 """Test ensure_notification_flag method."""
107
108 # Create a base config and register the key
109 config = ARConfigBase()
110 config.register(MockKey.EXISTING)
111
112 with (
113 patch.object(config, "set", return_value=None) as mock_set,
114 patch.object(config, "register", return_value=None) as mock_register,
115 ):
116 writers.ensure_notification_flag(config, key)
117
118 if called:
119 mock_register.assert_called_once_with(key)
120 mock_set.assert_called_once_with(
121 key, CONFIG_DEFAULT_ALREADY_NOTIFIED
122 )
123 else:
124 mock_register.assert_not_called()
125 mock_set.assert_not_called()
126
127
128 def test_ensure_notification_flag_order() -> None:
129 """Test ensure_notification_flag does the correct sequencing."""
130
131 calls: list[tuple[Any, ...]] = []
132
133 def rec_contains(k: Any) -> bool:
134 """Check if a key is registered."""
135
136 calls.append(("contains", k))
137 return False
138
139 def rec_register(k: Any) -> None:
140 """Register a key."""
141
142 calls.append(("register", k))
143
144 def rec_set(k: Any, v: Any) -> None:
145 """Set a key-value pair."""
146
147 calls.append(("set", k, v))
148
149 config = Mock(spec=ARConfigBase)
150 config.register = Mock(side_effect=rec_register)
151 config.set = Mock(side_effect=rec_set)
152 config.__contains__ = Mock(side_effect=rec_contains)
153
154 writers.ensure_notification_flag(config, MockKey.SOME)
155
156 # Verify the calls made in the correct order
157 assert calls == [
158 ("contains", MockKey.SOME),
159 ("register", MockKey.SOME),
160 ("set", MockKey.SOME, CONFIG_DEFAULT_ALREADY_NOTIFIED),
161 ]