comparison sat/test/test_memory.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
24 from constants import Const 24 from constants import Const
25 from xml.dom import minidom 25 from xml.dom import minidom
26 26
27 27
28 class MemoryTest(unittest.TestCase): 28 class MemoryTest(unittest.TestCase):
29
30 def setUp(self): 29 def setUp(self):
31 self.host = helpers.FakeSAT() 30 self.host = helpers.FakeSAT()
32 31
33 def _getParamXML(self, param="1", security_level=None): 32 def _getParamXML(self, param="1", security_level=None):
34 """Generate XML for testing parameters 33 """Generate XML for testing parameters
35 34
36 @param param (str): a subset of "123" 35 @param param (str): a subset of "123"
37 @param security_level: security level of the parameters 36 @param security_level: security level of the parameters
38 @return (str) 37 @return (str)
39 """ 38 """
39
40 def getParam(name): 40 def getParam(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 """ % {'param_name': name, 43 """ % {
44 'param_label': _(name), 44 "param_name": name,
45 'security': '' if security_level is None else ('security="%d"' % security_level) 45 "param_label": _(name),
46 } 46 "security": ""
47 params = '' 47 if security_level is None
48 else ('security="%d"' % security_level),
49 }
50
51 params = ""
48 if "1" in param: 52 if "1" in param:
49 params += getParam(Const.ENABLE_UNIBOX_PARAM) 53 params += getParam(Const.ENABLE_UNIBOX_PARAM)
50 if "2" in param: 54 if "2" in param:
51 params += getParam(Const.PARAM_IN_QUOTES) 55 params += getParam(Const.PARAM_IN_QUOTES)
52 if "3" in param: 56 if "3" in param:
58 %(params)s 62 %(params)s
59 </category> 63 </category>
60 </individual> 64 </individual>
61 </params> 65 </params>
62 """ % { 66 """ % {
63 'category_name': Const.COMPOSITION_KEY, 67 "category_name": Const.COMPOSITION_KEY,
64 'category_label': _(Const.COMPOSITION_KEY), 68 "category_label": _(Const.COMPOSITION_KEY),
65 'params': params 69 "params": params,
66 } 70 }
67 71
68 def _paramExists(self, param="1", src=None): 72 def _paramExists(self, param="1", src=None):
69 """ 73 """
70 74
84 # some "individual" or "general" elements, when it comes 88 # some "individual" or "general" elements, when it comes
85 # from Memory.getParams we have here a "params" elements 89 # from Memory.getParams we have here a "params" elements
86 if type_node.nodeName not in ("individual", "general", "params"): 90 if type_node.nodeName not in ("individual", "general", "params"):
87 continue 91 continue
88 for cat_node in type_node.childNodes: 92 for cat_node in type_node.childNodes:
89 if cat_node.nodeName != "category" or cat_node.getAttribute("name") != category: 93 if (
94 cat_node.nodeName != "category"
95 or cat_node.getAttribute("name") != category
96 ):
90 continue 97 continue
91 for param in cat_node.childNodes: 98 for param in cat_node.childNodes:
92 if param.nodeName == "param" and param.getAttribute("name") == name: 99 if param.nodeName == "param" and param.getAttribute("name") == name:
93 return True 100 return True
94 return False 101 return False
98 @param param (str): a character in "12" 105 @param param (str): a character in "12"
99 @param src (DOM element): the top-level element to look in 106 @param src (DOM element): the top-level element to look in
100 @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
101 @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
102 """ 109 """
103 msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n" 110 msg = (
111 "Expected parameter not found!\n"
112 if exists
113 else "Unexpected parameter found!\n"
114 )
104 if deferred: 115 if deferred:
105 # 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,
106 # 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
107 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))
108 assertion = self._paramExists(param, src) 119 assertion = self._paramExists(param, src)
114 def assertParamNotExists(self, param="1", src=None): 125 def assertParamNotExists(self, param="1", src=None):
115 self.assertParam_generic(param, src, False) 126 self.assertParam_generic(param, src, False)
116 127
117 def assertParamExists_async(self, src, param="1"): 128 def assertParamExists_async(self, src, param="1"):
118 """@param src: a deferred result from Memory.getParams""" 129 """@param src: a deferred result from Memory.getParams"""
119 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), True, True) 130 self.assertParam_generic(
131 param, minidom.parseString(src.encode("utf-8")), True, True
132 )
120 133
121 def assertParamNotExists_async(self, src, param="1"): 134 def assertParamNotExists_async(self, src, param="1"):
122 """@param src: a deferred result from Memory.getParams""" 135 """@param src: a deferred result from Memory.getParams"""
123 self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), False, True) 136 self.assertParam_generic(
124 137 param, minidom.parseString(src.encode("utf-8")), False, True
125 def _getParams(self, security_limit, app='', profile_key='@NONE@'): 138 )
139
140 def _getParams(self, security_limit, app="", profile_key="@NONE@"):
126 """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.
127 142
128 @param security_limit (int): the security limit 143 @param security_limit (int): the security limit
129 @param app (str): empty string or "libervia" 144 @param app (str): empty string or "libervia"
130 @param profile_key 145 @param profile_key
131 """ 146 """
132 if profile_key == '@NONE@': 147 if profile_key == "@NONE@":
133 profile_key = '@DEFAULT@' 148 profile_key = "@DEFAULT@"
134 return self.host.memory.params.getParams(security_limit, app, profile_key) 149 return self.host.memory.params.getParams(security_limit, app, profile_key)
135 150
136 def test_updateParams(self): 151 def test_updateParams(self):
137 self.host.memory.reinit() 152 self.host.memory.reinit()
138 # check if the update works 153 # check if the update works
139 self.host.memory.updateParams(self._getParamXML()) 154 self.host.memory.updateParams(self._getParamXML())
140 self.assertParamExists() 155 self.assertParamExists()
141 previous = self.host.memory.params.dom.cloneNode(True) 156 previous = self.host.memory.params.dom.cloneNode(True)
142 # now check if it is really updated and not duplicated 157 # now check if it is really updated and not duplicated
143 self.host.memory.updateParams(self._getParamXML()) 158 self.host.memory.updateParams(self._getParamXML())
144 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) 159 self.assertEqual(
160 previous.toxml().encode("utf-8"),
161 self.host.memory.params.dom.toxml().encode("utf-8"),
162 )
145 163
146 self.host.memory.reinit() 164 self.host.memory.reinit()
147 # check successive updates (without intersection) 165 # check successive updates (without intersection)
148 self.host.memory.updateParams(self._getParamXML('1')) 166 self.host.memory.updateParams(self._getParamXML("1"))
149 self.assertParamExists("1") 167 self.assertParamExists("1")
150 self.assertParamNotExists("2") 168 self.assertParamNotExists("2")
151 self.host.memory.updateParams(self._getParamXML('2')) 169 self.host.memory.updateParams(self._getParamXML("2"))
152 self.assertParamExists("1") 170 self.assertParamExists("1")
153 self.assertParamExists("2") 171 self.assertParamExists("2")
154 172
155 previous = self.host.memory.params.dom.cloneNode(True) # save for later 173 previous = self.host.memory.params.dom.cloneNode(True) # save for later
156 174
157 self.host.memory.reinit() 175 self.host.memory.reinit()
158 # check successive updates (with intersection) 176 # check successive updates (with intersection)
159 self.host.memory.updateParams(self._getParamXML('1')) 177 self.host.memory.updateParams(self._getParamXML("1"))
160 self.assertParamExists("1") 178 self.assertParamExists("1")
161 self.assertParamNotExists("2") 179 self.assertParamNotExists("2")
162 self.host.memory.updateParams(self._getParamXML('12')) 180 self.host.memory.updateParams(self._getParamXML("12"))
163 self.assertParamExists("1") 181 self.assertParamExists("1")
164 self.assertParamExists("2") 182 self.assertParamExists("2")
165 183
166 # successive updates with or without intersection should have the same result 184 # successive updates with or without intersection should have the same result
167 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) 185 self.assertEqual(
186 previous.toxml().encode("utf-8"),
187 self.host.memory.params.dom.toxml().encode("utf-8"),
188 )
168 189
169 self.host.memory.reinit() 190 self.host.memory.reinit()
170 # one update with two params in a new category 191 # one update with two params in a new category
171 self.host.memory.updateParams(self._getParamXML('12')) 192 self.host.memory.updateParams(self._getParamXML("12"))
172 self.assertParamExists("1") 193 self.assertParamExists("1")
173 self.assertParamExists("2") 194 self.assertParamExists("2")
174 195
175 def test_getParams(self): 196 def test_getParams(self):
176 # tests with no security level on the parameter (most secure) 197 # tests with no security level on the parameter (most secure)
194 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async) 215 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
195 self._getParams(0).addCallback(self.assertParamNotExists_async) 216 self._getParams(0).addCallback(self.assertParamNotExists_async)
196 return self._getParams(1).addCallback(self.assertParamExists_async) 217 return self._getParams(1).addCallback(self.assertParamExists_async)
197 218
198 def test_paramsRegisterApp(self): 219 def test_paramsRegisterApp(self):
199
200 def register(xml, security_limit, app): 220 def register(xml, security_limit, app):
201 """ 221 """
202 @param xml: XML definition of the parameters to be added 222 @param xml: XML definition of the parameters to be added
203 @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
204 @param app: name of the frontend registering the parameters 224 @param app: name of the frontend registering the parameters
269 def test_paramsRegisterApp_getParams(self): 289 def test_paramsRegisterApp_getParams(self):
270 # test retrieving the parameter for a specific frontend 290 # test retrieving the parameter for a specific frontend
271 self.host.memory.reinit() 291 self.host.memory.reinit()
272 params = self._getParamXML(security_level=1) 292 params = self._getParamXML(security_level=1)
273 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) 293 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
274 self._getParams(1, '').addCallback(self.assertParamExists_async) 294 self._getParams(1, "").addCallback(self.assertParamExists_async)
275 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async) 295 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async)
276 self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) 296 self._getParams(1, "another_dummy_frontend").addCallback(
297 self.assertParamNotExists_async
298 )
277 299
278 # the same with several parameters registered at the same time 300 # the same with several parameters registered at the same time
279 self.host.memory.reinit() 301 self.host.memory.reinit()
280 params = self._getParamXML('12', security_level=0) 302 params = self._getParamXML("12", security_level=0)
281 self.host.memory.paramsRegisterApp(params, 5, Const.APP_NAME) 303 self.host.memory.paramsRegisterApp(params, 5, Const.APP_NAME)
282 self._getParams(5, '').addCallback(self.assertParamExists_async) 304 self._getParams(5, "").addCallback(self.assertParamExists_async)
283 self._getParams(5, '').addCallback(self.assertParamExists_async, "2") 305 self._getParams(5, "").addCallback(self.assertParamExists_async, "2")
284 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async) 306 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async)
285 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async, "2") 307 self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async, "2")
286 self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async) 308 self._getParams(5, "another_dummy_frontend").addCallback(
287 return self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async, "2") 309 self.assertParamNotExists_async
310 )
311 return self._getParams(5, "another_dummy_frontend").addCallback(
312 self.assertParamNotExists_async, "2"
313 )