changeset 1050:de415d7984f7

memory (params), test: fixes bug relative to parameter filtering
author souliane <souliane@mailoo.org>
date Tue, 03 Jun 2014 15:10:51 +0200
parents 9e24ca275ddd
children 854880a31717
files src/memory/params.py src/test/test_memory.py
diffstat 2 files changed, 63 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/params.py	Wed Jun 04 12:57:49 2014 +0200
+++ b/src/memory/params.py	Tue Jun 03 15:10:51 2014 +0200
@@ -546,13 +546,16 @@
                     if category not in cache:
                         # we make a copy for the new xml
                         cache[category] = dest_cat = cat_node.cloneNode(True)
+                        to_remove = []
                         for node in dest_cat.childNodes:
                             if node.nodeName != "param":
                                 continue
                             if not checkNode(node):
-                                dest_cat.removeChild(node)
+                                to_remove.append(node)
                                 continue
                             dest_params[node.getAttribute('name')] = node
+                        for node in to_remove:
+                            dest_cat.removeChild(node)
                         new_node = True
                     else:
                         # It's not a new node, we use the previously cloned one
--- a/src/test/test_memory.py	Wed Jun 04 12:57:49 2014 +0200
+++ b/src/test/test_memory.py	Tue Jun 03 15:10:51 2014 +0200
@@ -33,6 +33,12 @@
         self.host = helpers.FakeSAT()
 
     def _getParamXML(self, param="1", security_level=None):
+        """Generate XML for testing parameters
+
+        @param param (str): parameter "1", "2" or "both"
+        @param security_level: security level of the parameters
+        @return (str)
+        """
         def getParam(name):
             return """
             <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/>
@@ -61,7 +67,12 @@
         }
 
     def _paramExists(self, param="1", src=None):
-        """@return: True is the param (category, name) exists"""
+        """
+
+        @param param (str): parameter "1" or "2"
+        @param src (DOM element): the top-level element to look in
+        @return: True is the param exists
+        """
         if param == "1":
             name = Const.ENABLE_UNIBOX_PARAM
         else:
@@ -84,8 +95,14 @@
         return False
 
     def assertParam_generic(self, param="1", src=None, exists=True, deferred=False):
+        """
+        @param param (str): parameter "1" or "2"
+        @param src (DOM element): the top-level element to look in
+        @param exists (boolean): True to assert the param exists, False to assert it doesn't
+        @param deferred (boolean): True if this method is called from a Deferred callback
+        """
         msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n"
-        if deferred == False:
+        if deferred:
             # in this stack we can see the line where the error came from,
             # if limit=5, 6 is not enough you can increase the value
             msg += "\n".join(traceback.format_stack(limit=5 if exists else 6))
@@ -107,6 +124,12 @@
         self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), False, True)
 
     def _getParams(self, security_limit, app='', profile_key='@NONE@'):
+        """Get the parameters accessible with the given security limit and application name.
+
+        @param security_limit (int): the security limit
+        @param app (str): empty string or "libervia"
+        @param profile_key
+        """
         if profile_key == '@NONE@':
             profile_key = '@DEFAULT@'
         return self.host.memory.getParams(security_limit, app, profile_key)
@@ -165,15 +188,20 @@
         self.host.memory.updateParams(params)
         self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
         self._getParams(0).addCallback(self.assertParamNotExists_async)
-        self._getParams(1).addCallback(self.assertParamExists_async)
+        return self._getParams(1).addCallback(self.assertParamExists_async)
 
     def test_paramsRegisterApp(self):
 
-        def register(*args):
+        def register(xml, security_limit, app):
+            """
+            @param xml: XML definition of the parameters to be added
+            @param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure
+            @param app: name of the frontend registering the parameters
+            """
             logger = getLogger()
             level = logger.getEffectiveLevel()
             logger.setLevel(INFO)
-            self.host.memory.paramsRegisterApp(*args)
+            self.host.memory.paramsRegisterApp(xml, security_limit, app)
             logger.setLevel(level)
 
         # tests with no security level on the parameter (most secure)
@@ -212,6 +240,21 @@
         register(params, 1, Const.APP_NAME)
         self.assertParamExists()
 
+        # tests with security level 1 and several parameters being registered
+        params = self._getParamXML("both", security_level=1)
+        self.host.memory.init()
+        register(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
+        self.assertParamExists()
+        self.assertParamExists("2")
+        self.host.memory.init()
+        register(params, 0, Const.APP_NAME)
+        self.assertParamNotExists()
+        self.assertParamNotExists("2")
+        self.host.memory.init()
+        register(params, 1, Const.APP_NAME)
+        self.assertParamExists()
+        self.assertParamExists("2")
+
     def test_paramsRegisterApp_getParams(self):
         # test retrieving the parameter for a specific frontend
         self.host.memory.init()
@@ -220,3 +263,14 @@
         self._getParams(1, '').addCallback(self.assertParamExists_async)
         self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async)
         self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async)
+
+        # the same with several parameters registered at the same time
+        self.host.memory.init()
+        params = self._getParamXML('both', security_level=0)
+        self.host.memory.paramsRegisterApp(params, 5, Const.APP_NAME)
+        self._getParams(5, '').addCallback(self.assertParamExists_async)
+        self._getParams(5, '').addCallback(self.assertParamExists_async, "2")
+        self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async)
+        self._getParams(5, Const.APP_NAME).addCallback(self.assertParamExists_async, "2")
+        self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async)
+        return self._getParams(5, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async, "2")