Mercurial > libervia-backend
comparison sat/test/test_memory.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | be6d91572633 |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
27 | 27 |
28 class MemoryTest(unittest.TestCase): | 28 class MemoryTest(unittest.TestCase): |
29 def setUp(self): | 29 def setUp(self): |
30 self.host = helpers.FakeSAT() | 30 self.host = helpers.FakeSAT() |
31 | 31 |
32 def _getParamXML(self, param="1", security_level=None): | 32 def _get_param_xml(self, param="1", security_level=None): |
33 """Generate XML for testing parameters | 33 """Generate XML for testing parameters |
34 | 34 |
35 @param param (str): a subset of "123" | 35 @param param (str): a subset of "123" |
36 @param security_level: security level of the parameters | 36 @param security_level: security level of the parameters |
37 @return (str) | 37 @return (str) |
38 """ | 38 """ |
39 | 39 |
40 def getParam(name): | 40 def get_param(name): |
41 return """ | 41 return """ |
42 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/> | 42 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/> |
43 """ % { | 43 """ % { |
44 "param_name": name, | 44 "param_name": name, |
45 "param_label": _(name), | 45 "param_label": _(name), |
48 else ('security="%d"' % security_level), | 48 else ('security="%d"' % security_level), |
49 } | 49 } |
50 | 50 |
51 params = "" | 51 params = "" |
52 if "1" in param: | 52 if "1" in param: |
53 params += getParam(Const.ENABLE_UNIBOX_PARAM) | 53 params += get_param(Const.ENABLE_UNIBOX_PARAM) |
54 if "2" in param: | 54 if "2" in param: |
55 params += getParam(Const.PARAM_IN_QUOTES) | 55 params += get_param(Const.PARAM_IN_QUOTES) |
56 if "3" in param: | 56 if "3" in param: |
57 params += getParam("Dummy param") | 57 params += get_param("Dummy param") |
58 return """ | 58 return """ |
59 <params> | 59 <params> |
60 <individual> | 60 <individual> |
61 <category name="%(category_name)s" label="%(category_label)s"> | 61 <category name="%(category_name)s" label="%(category_label)s"> |
62 %(params)s | 62 %(params)s |
67 "category_name": Const.COMPOSITION_KEY, | 67 "category_name": Const.COMPOSITION_KEY, |
68 "category_label": _(Const.COMPOSITION_KEY), | 68 "category_label": _(Const.COMPOSITION_KEY), |
69 "params": params, | 69 "params": params, |
70 } | 70 } |
71 | 71 |
72 def _paramExists(self, param="1", src=None): | 72 def _param_exists(self, param="1", src=None): |
73 """ | 73 """ |
74 | 74 |
75 @param param (str): a character in "12" | 75 @param param (str): a character in "12" |
76 @param src (DOM element): the top-level element to look in | 76 @param src (DOM element): the top-level element to look in |
77 @return: True is the param exists | 77 @return: True is the param exists |
84 if src is None: | 84 if src is None: |
85 src = self.host.memory.params.dom.documentElement | 85 src = self.host.memory.params.dom.documentElement |
86 for type_node in src.childNodes: | 86 for type_node in src.childNodes: |
87 # when src comes self.host.memory.params.dom, we have here | 87 # when src comes self.host.memory.params.dom, we have here |
88 # some "individual" or "general" elements, when it comes | 88 # some "individual" or "general" elements, when it comes |
89 # from Memory.getParams we have here a "params" elements | 89 # from Memory.get_params we have here a "params" elements |
90 if type_node.nodeName not in ("individual", "general", "params"): | 90 if type_node.nodeName not in ("individual", "general", "params"): |
91 continue | 91 continue |
92 for cat_node in type_node.childNodes: | 92 for cat_node in type_node.childNodes: |
93 if ( | 93 if ( |
94 cat_node.nodeName != "category" | 94 cat_node.nodeName != "category" |
98 for param in cat_node.childNodes: | 98 for param in cat_node.childNodes: |
99 if param.nodeName == "param" and param.getAttribute("name") == name: | 99 if param.nodeName == "param" and param.getAttribute("name") == name: |
100 return True | 100 return True |
101 return False | 101 return False |
102 | 102 |
103 def assertParam_generic(self, param="1", src=None, exists=True, deferred=False): | 103 def assert_param_generic(self, param="1", src=None, exists=True, deferred=False): |
104 """ | 104 """ |
105 @param param (str): a character in "12" | 105 @param param (str): a character in "12" |
106 @param src (DOM element): the top-level element to look in | 106 @param src (DOM element): the top-level element to look in |
107 @param exists (boolean): True to assert the param exists, False to assert it doesn't | 107 @param exists (boolean): True to assert the param exists, False to assert it doesn't |
108 @param deferred (boolean): True if this method is called from a Deferred callback | 108 @param deferred (boolean): True if this method is called from a Deferred callback |
114 ) | 114 ) |
115 if deferred: | 115 if deferred: |
116 # in this stack we can see the line where the error came from, | 116 # in this stack we can see the line where the error came from, |
117 # if limit=5, 6 is not enough you can increase the value | 117 # if limit=5, 6 is not enough you can increase the value |
118 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) | 118 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6)) |
119 assertion = self._paramExists(param, src) | 119 assertion = self._param_exists(param, src) |
120 getattr(self, "assert%s" % exists)(assertion, msg) | 120 getattr(self, "assert%s" % exists)(assertion, msg) |
121 | 121 |
122 def assertParamExists(self, param="1", src=None): | 122 def assert_param_exists(self, param="1", src=None): |
123 self.assertParam_generic(param, src, True) | 123 self.assert_param_generic(param, src, True) |
124 | 124 |
125 def assertParamNotExists(self, param="1", src=None): | 125 def assert_param_not_exists(self, param="1", src=None): |
126 self.assertParam_generic(param, src, False) | 126 self.assert_param_generic(param, src, False) |
127 | 127 |
128 def assertParamExists_async(self, src, param="1"): | 128 def assert_param_exists_async(self, src, param="1"): |
129 """@param src: a deferred result from Memory.getParams""" | 129 """@param src: a deferred result from Memory.get_params""" |
130 self.assertParam_generic( | 130 self.assert_param_generic( |
131 param, minidom.parseString(src.encode("utf-8")), True, True | 131 param, minidom.parseString(src.encode("utf-8")), True, True |
132 ) | 132 ) |
133 | 133 |
134 def assertParamNotExists_async(self, src, param="1"): | 134 def assert_param_not_exists_async(self, src, param="1"): |
135 """@param src: a deferred result from Memory.getParams""" | 135 """@param src: a deferred result from Memory.get_params""" |
136 self.assertParam_generic( | 136 self.assert_param_generic( |
137 param, minidom.parseString(src.encode("utf-8")), False, True | 137 param, minidom.parseString(src.encode("utf-8")), False, True |
138 ) | 138 ) |
139 | 139 |
140 def _getParams(self, security_limit, app="", profile_key="@NONE@"): | 140 def _get_params(self, security_limit, app="", profile_key="@NONE@"): |
141 """Get the parameters accessible with the given security limit and application name. | 141 """Get the parameters accessible with the given security limit and application name. |
142 | 142 |
143 @param security_limit (int): the security limit | 143 @param security_limit (int): the security limit |
144 @param app (str): empty string or "libervia" | 144 @param app (str): empty string or "libervia" |
145 @param profile_key | 145 @param profile_key |
146 """ | 146 """ |
147 if profile_key == "@NONE@": | 147 if profile_key == "@NONE@": |
148 profile_key = "@DEFAULT@" | 148 profile_key = "@DEFAULT@" |
149 return self.host.memory.params.getParams(security_limit, app, profile_key) | 149 return self.host.memory.params.get_params(security_limit, app, profile_key) |
150 | 150 |
151 def test_updateParams(self): | 151 def test_update_params(self): |
152 self.host.memory.reinit() | 152 self.host.memory.reinit() |
153 # check if the update works | 153 # check if the update works |
154 self.host.memory.updateParams(self._getParamXML()) | 154 self.host.memory.update_params(self._get_param_xml()) |
155 self.assertParamExists() | 155 self.assert_param_exists() |
156 previous = self.host.memory.params.dom.cloneNode(True) | 156 previous = self.host.memory.params.dom.cloneNode(True) |
157 # now check if it is really updated and not duplicated | 157 # now check if it is really updated and not duplicated |
158 self.host.memory.updateParams(self._getParamXML()) | 158 self.host.memory.update_params(self._get_param_xml()) |
159 self.assertEqual( | 159 self.assertEqual( |
160 previous.toxml().encode("utf-8"), | 160 previous.toxml().encode("utf-8"), |
161 self.host.memory.params.dom.toxml().encode("utf-8"), | 161 self.host.memory.params.dom.toxml().encode("utf-8"), |
162 ) | 162 ) |
163 | 163 |
164 self.host.memory.reinit() | 164 self.host.memory.reinit() |
165 # check successive updates (without intersection) | 165 # check successive updates (without intersection) |
166 self.host.memory.updateParams(self._getParamXML("1")) | 166 self.host.memory.update_params(self._get_param_xml("1")) |
167 self.assertParamExists("1") | 167 self.assert_param_exists("1") |
168 self.assertParamNotExists("2") | 168 self.assert_param_not_exists("2") |
169 self.host.memory.updateParams(self._getParamXML("2")) | 169 self.host.memory.update_params(self._get_param_xml("2")) |
170 self.assertParamExists("1") | 170 self.assert_param_exists("1") |
171 self.assertParamExists("2") | 171 self.assert_param_exists("2") |
172 | 172 |
173 previous = self.host.memory.params.dom.cloneNode(True) # save for later | 173 previous = self.host.memory.params.dom.cloneNode(True) # save for later |
174 | 174 |
175 self.host.memory.reinit() | 175 self.host.memory.reinit() |
176 # check successive updates (with intersection) | 176 # check successive updates (with intersection) |
177 self.host.memory.updateParams(self._getParamXML("1")) | 177 self.host.memory.update_params(self._get_param_xml("1")) |
178 self.assertParamExists("1") | 178 self.assert_param_exists("1") |
179 self.assertParamNotExists("2") | 179 self.assert_param_not_exists("2") |
180 self.host.memory.updateParams(self._getParamXML("12")) | 180 self.host.memory.update_params(self._get_param_xml("12")) |
181 self.assertParamExists("1") | 181 self.assert_param_exists("1") |
182 self.assertParamExists("2") | 182 self.assert_param_exists("2") |
183 | 183 |
184 # successive updates with or without intersection should have the same result | 184 # successive updates with or without intersection should have the same result |
185 self.assertEqual( | 185 self.assertEqual( |
186 previous.toxml().encode("utf-8"), | 186 previous.toxml().encode("utf-8"), |
187 self.host.memory.params.dom.toxml().encode("utf-8"), | 187 self.host.memory.params.dom.toxml().encode("utf-8"), |
188 ) | 188 ) |
189 | 189 |
190 self.host.memory.reinit() | 190 self.host.memory.reinit() |
191 # one update with two params in a new category | 191 # one update with two params in a new category |
192 self.host.memory.updateParams(self._getParamXML("12")) | 192 self.host.memory.update_params(self._get_param_xml("12")) |
193 self.assertParamExists("1") | 193 self.assert_param_exists("1") |
194 self.assertParamExists("2") | 194 self.assert_param_exists("2") |
195 | 195 |
196 def test_getParams(self): | 196 def test_get_params(self): |
197 # tests with no security level on the parameter (most secure) | 197 # tests with no security level on the parameter (most secure) |
198 params = self._getParamXML() | 198 params = self._get_param_xml() |
199 self.host.memory.reinit() | 199 self.host.memory.reinit() |
200 self.host.memory.updateParams(params) | 200 self.host.memory.update_params(params) |
201 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | 201 self._get_params(Const.NO_SECURITY_LIMIT).addCallback(self.assert_param_exists_async) |
202 self._getParams(0).addCallback(self.assertParamNotExists_async) | 202 self._get_params(0).addCallback(self.assert_param_not_exists_async) |
203 self._getParams(1).addCallback(self.assertParamNotExists_async) | 203 self._get_params(1).addCallback(self.assert_param_not_exists_async) |
204 # tests with security level 0 on the parameter (not secure) | 204 # tests with security level 0 on the parameter (not secure) |
205 params = self._getParamXML(security_level=0) | 205 params = self._get_param_xml(security_level=0) |
206 self.host.memory.reinit() | 206 self.host.memory.reinit() |
207 self.host.memory.updateParams(params) | 207 self.host.memory.update_params(params) |
208 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | 208 self._get_params(Const.NO_SECURITY_LIMIT).addCallback(self.assert_param_exists_async) |
209 self._getParams(0).addCallback(self.assertParamExists_async) | 209 self._get_params(0).addCallback(self.assert_param_exists_async) |
210 self._getParams(1).addCallback(self.assertParamExists_async) | 210 self._get_params(1).addCallback(self.assert_param_exists_async) |
211 # tests with security level 1 on the parameter (more secure) | 211 # tests with security level 1 on the parameter (more secure) |
212 params = self._getParamXML(security_level=1) | 212 params = self._get_param_xml(security_level=1) |
213 self.host.memory.reinit() | 213 self.host.memory.reinit() |
214 self.host.memory.updateParams(params) | 214 self.host.memory.update_params(params) |
215 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) | 215 self._get_params(Const.NO_SECURITY_LIMIT).addCallback(self.assert_param_exists_async) |
216 self._getParams(0).addCallback(self.assertParamNotExists_async) | 216 self._get_params(0).addCallback(self.assert_param_not_exists_async) |
217 return self._getParams(1).addCallback(self.assertParamExists_async) | 217 return self._get_params(1).addCallback(self.assert_param_exists_async) |
218 | 218 |
219 def test_paramsRegisterApp(self): | 219 def test_params_register_app(self): |
220 def register(xml, security_limit, app): | 220 def register(xml, security_limit, app): |
221 """ | 221 """ |
222 @param xml: XML definition of the parameters to be added | 222 @param xml: XML definition of the parameters to be added |
223 @param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure | 223 @param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure |
224 @param app: name of the frontend registering the parameters | 224 @param app: name of the frontend registering the parameters |
225 """ | 225 """ |
226 helpers.muteLogging() | 226 helpers.mute_logging() |
227 self.host.memory.paramsRegisterApp(xml, security_limit, app) | 227 self.host.memory.params_register_app(xml, security_limit, app) |
228 helpers.unmuteLogging() | 228 helpers.unmute_logging() |
229 | 229 |
230 # tests with no security level on the parameter (most secure) | 230 # tests with no security level on the parameter (most secure) |
231 params = self._getParamXML() | 231 params = self._get_param_xml() |
232 self.host.memory.reinit() | 232 self.host.memory.reinit() |
233 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | 233 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) |
234 self.assertParamExists() | 234 self.assert_param_exists() |
235 self.host.memory.reinit() | 235 self.host.memory.reinit() |
236 register(params, 0, Const.APP_NAME) | 236 register(params, 0, Const.APP_NAME) |
237 self.assertParamNotExists() | 237 self.assert_param_not_exists() |
238 self.host.memory.reinit() | 238 self.host.memory.reinit() |
239 register(params, 1, Const.APP_NAME) | 239 register(params, 1, Const.APP_NAME) |
240 self.assertParamNotExists() | 240 self.assert_param_not_exists() |
241 | 241 |
242 # tests with security level 0 on the parameter (not secure) | 242 # tests with security level 0 on the parameter (not secure) |
243 params = self._getParamXML(security_level=0) | 243 params = self._get_param_xml(security_level=0) |
244 self.host.memory.reinit() | 244 self.host.memory.reinit() |
245 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | 245 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) |
246 self.assertParamExists() | 246 self.assert_param_exists() |
247 self.host.memory.reinit() | 247 self.host.memory.reinit() |
248 register(params, 0, Const.APP_NAME) | 248 register(params, 0, Const.APP_NAME) |
249 self.assertParamExists() | 249 self.assert_param_exists() |
250 self.host.memory.reinit() | 250 self.host.memory.reinit() |
251 register(params, 1, Const.APP_NAME) | 251 register(params, 1, Const.APP_NAME) |
252 self.assertParamExists() | 252 self.assert_param_exists() |
253 | 253 |
254 # tests with security level 1 on the parameter (more secure) | 254 # tests with security level 1 on the parameter (more secure) |
255 params = self._getParamXML(security_level=1) | 255 params = self._get_param_xml(security_level=1) |
256 self.host.memory.reinit() | 256 self.host.memory.reinit() |
257 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | 257 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) |
258 self.assertParamExists() | 258 self.assert_param_exists() |
259 self.host.memory.reinit() | 259 self.host.memory.reinit() |
260 register(params, 0, Const.APP_NAME) | 260 register(params, 0, Const.APP_NAME) |
261 self.assertParamNotExists() | 261 self.assert_param_not_exists() |
262 self.host.memory.reinit() | 262 self.host.memory.reinit() |
263 register(params, 1, Const.APP_NAME) | 263 register(params, 1, Const.APP_NAME) |
264 self.assertParamExists() | 264 self.assert_param_exists() |
265 | 265 |
266 # tests with security level 1 and several parameters being registered | 266 # tests with security level 1 and several parameters being registered |
267 params = self._getParamXML("12", security_level=1) | 267 params = self._get_param_xml("12", security_level=1) |
268 self.host.memory.reinit() | 268 self.host.memory.reinit() |
269 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) | 269 register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) |
270 self.assertParamExists() | 270 self.assert_param_exists() |
271 self.assertParamExists("2") | 271 self.assert_param_exists("2") |
272 self.host.memory.reinit() | 272 self.host.memory.reinit() |
273 register(params, 0, Const.APP_NAME) | 273 register(params, 0, Const.APP_NAME) |
274 self.assertParamNotExists() | 274 self.assert_param_not_exists() |
275 self.assertParamNotExists("2") | 275 self.assert_param_not_exists("2") |
276 self.host.memory.reinit() | 276 self.host.memory.reinit() |
277 register(params, 1, Const.APP_NAME) | 277 register(params, 1, Const.APP_NAME) |
278 self.assertParamExists() | 278 self.assert_param_exists() |
279 self.assertParamExists("2") | 279 self.assert_param_exists("2") |
280 | 280 |
281 # tests with several parameters being registered in an existing category | 281 # tests with several parameters being registered in an existing category |
282 self.host.memory.reinit() | 282 self.host.memory.reinit() |
283 self.host.memory.updateParams(self._getParamXML("3")) | 283 self.host.memory.update_params(self._get_param_xml("3")) |
284 register(self._getParamXML("12"), Const.NO_SECURITY_LIMIT, Const.APP_NAME) | 284 register(self._get_param_xml("12"), Const.NO_SECURITY_LIMIT, Const.APP_NAME) |
285 self.assertParamExists() | 285 self.assert_param_exists() |
286 self.assertParamExists("2") | 286 self.assert_param_exists("2") |
287 self.host.memory.reinit() | 287 self.host.memory.reinit() |
288 | 288 |
289 def test_paramsRegisterApp_getParams(self): | 289 def test_params_register_app_get_params(self): |
290 # test retrieving the parameter for a specific frontend | 290 # test retrieving the parameter for a specific frontend |
291 self.host.memory.reinit() | 291 self.host.memory.reinit() |
292 params = self._getParamXML(security_level=1) | 292 params = self._get_param_xml(security_level=1) |
293 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) | 293 self.host.memory.params_register_app(params, 1, Const.APP_NAME) |
294 self._getParams(1, "").addCallback(self.assertParamExists_async) | 294 self._get_params(1, "").addCallback(self.assert_param_exists_async) |
295 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) | 295 self._get_params(1, Const.APP_NAME).addCallback(self.assert_param_exists_async) |
296 self._getParams(1, "another_dummy_frontend").addCallback( | 296 self._get_params(1, "another_dummy_frontend").addCallback( |
297 self.assertParamNotExists_async | 297 self.assert_param_not_exists_async |
298 ) | 298 ) |
299 | 299 |
300 # the same with several parameters registered at the same time | 300 # the same with several parameters registered at the same time |
301 self.host.memory.reinit() | 301 self.host.memory.reinit() |
302 params = self._getParamXML("12", security_level=0) | 302 params = self._get_param_xml("12", security_level=0) |
303 self.host.memory.paramsRegisterApp(params, 5, Const.APP_NAME) | 303 self.host.memory.params_register_app(params, 5, Const.APP_NAME) |
304 self._getParams(5, "").addCallback(self.assertParamExists_async) | 304 self._get_params(5, "").addCallback(self.assert_param_exists_async) |
305 self._getParams(5, "").addCallback(self.assertParamExists_async, "2") | 305 self._get_params(5, "").addCallback(self.assert_param_exists_async, "2") |
306 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async) | 306 self._get_params(5, Const.APP_NAME).addCallback(self.assert_param_exists_async) |
307 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async, "2") | 307 self._get_params(5, Const.APP_NAME).addCallback(self.assert_param_exists_async, "2") |
308 self._getParams(5, "another_dummy_frontend").addCallback( | 308 self._get_params(5, "another_dummy_frontend").addCallback( |
309 self.assertParamNotExists_async | 309 self.assert_param_not_exists_async |
310 ) | 310 ) |
311 return self._getParams(5, "another_dummy_frontend").addCallback( | 311 return self._get_params(5, "another_dummy_frontend").addCallback( |
312 self.assertParamNotExists_async, "2" | 312 self.assert_param_not_exists_async, "2" |
313 ) | 313 ) |