comparison src/test/test_memory.py @ 789:0cb423500fbb

test: use the SatTestCase methods instead of builtin "assert" in tests for memory, plugin xep-0033
author souliane <souliane@mailoo.org>
date Tue, 07 Jan 2014 09:27:53 +0100
parents c3acc1298a2f
children cb2db0d85029
comparison
equal deleted inserted replaced
788:d6652683c572 789:0cb423500fbb
71 for param in cat_node.childNodes: 71 for param in cat_node.childNodes:
72 if param.nodeName == "param" and param.getAttribute("name") == name: 72 if param.nodeName == "param" and param.getAttribute("name") == name:
73 return True 73 return True
74 return False 74 return False
75 75
76 def _assert(self, src, true=True): 76 def assertParam_generic(self, src=None, exists=True, deferred=False):
77 msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n"
78 if deferred == False:
79 # in this stack we can see the line where the error came from,
80 # if limit=5, 6 is not enough you can increase the value
81 msg += "\n".join(traceback.format_stack(limit=5 if exists else 6))
82 assertion = self._paramExists(src=src)
83 getattr(self, "assert%s" % exists)(assertion, msg)
84
85 def assertParamExists(self, src=None):
86 self.assertParam_generic(src, True)
87
88 def assertParamNotExists(self, src=None):
89 self.assertParam_generic(src, False)
90
91 def assertParamExists_async(self, src):
77 """@param src: a deferred result from Memory.getParams""" 92 """@param src: a deferred result from Memory.getParams"""
78 assertion = self._paramExists(src=minidom.parseString(src.encode("utf-8"))) 93 self.assertParam_generic(minidom.parseString(src.encode("utf-8")), True, True)
79 if not true:
80 assertion = not assertion
81 try:
82 assert(assertion)
83 except AssertionError as e:
84 # in this stack we can see the line where the error came from,
85 # if limit=5 is not enough you can increase the value
86 print "---------------------- stack start ----------------------"
87 traceback.print_stack(limit=5 if true else 6)
88 print "----------------------- stack end -----------------------"
89 raise e
90 94
91 def _assert_not(self, src): 95 def assertParamNotExists_async(self, src):
92 """@param src: a deferred result from Memory.getParams""" 96 """@param src: a deferred result from Memory.getParams"""
93 self._assert(src, False) 97 self.assertParam_generic(minidom.parseString(src.encode("utf-8")), False, True)
94 98
95 def _getParams(self, security_limit, app='', profile_key='@NONE@'): 99 def _getParams(self, security_limit, app='', profile_key='@NONE@'):
96 if profile_key == '@NONE@': 100 if profile_key == '@NONE@':
97 profile_key = '@DEFAULT@' 101 profile_key = '@DEFAULT@'
98 return self.host.memory.getParams(security_limit, app, profile_key) 102 return self.host.memory.getParams(security_limit, app, profile_key)
99 103
100 def test_updateParams(self): 104 def test_updateParams(self):
101 self.host.memory.init() 105 self.host.memory.init()
102 # check if the update works 106 # check if the update works
103 self.host.memory.updateParams(self._getParamXML()) 107 self.host.memory.updateParams(self._getParamXML())
104 assert(self._paramExists()) 108 self.assertParamExists()
105 previous = self.host.memory.params.dom.cloneNode(True) 109 previous = self.host.memory.params.dom.cloneNode(True)
106 # now check if it is really updated and not duplicated 110 # now check if it is really updated and not duplicated
107 self.host.memory.updateParams(self._getParamXML()) 111 self.host.memory.updateParams(self._getParamXML())
108 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8")) 112 self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
109 113
110 def test_getParams(self): 114 def test_getParams(self):
111 # tests with no security level on the parameter (most secure) 115 # tests with no security level on the parameter (most secure)
112 params = self._getParamXML() 116 params = self._getParamXML()
113 self.host.memory.init() 117 self.host.memory.init()
114 self.host.memory.updateParams(params) 118 self.host.memory.updateParams(params)
115 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) 119 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
116 self._getParams(0).addCallback(self._assert_not) 120 self._getParams(0).addCallback(self.assertParamNotExists_async)
117 self._getParams(1).addCallback(self._assert_not) 121 self._getParams(1).addCallback(self.assertParamNotExists_async)
118 # tests with security level 0 on the parameter (not secure) 122 # tests with security level 0 on the parameter (not secure)
119 params = self._getParamXML(security_level=0) 123 params = self._getParamXML(security_level=0)
120 self.host.memory.init() 124 self.host.memory.init()
121 self.host.memory.updateParams(params) 125 self.host.memory.updateParams(params)
122 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) 126 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
123 self._getParams(0).addCallback(self._assert) 127 self._getParams(0).addCallback(self.assertParamExists_async)
124 self._getParams(1).addCallback(self._assert) 128 self._getParams(1).addCallback(self.assertParamExists_async)
125 # tests with security level 1 on the parameter (more secure) 129 # tests with security level 1 on the parameter (more secure)
126 params = self._getParamXML(security_level=1) 130 params = self._getParamXML(security_level=1)
127 self.host.memory.init() 131 self.host.memory.init()
128 self.host.memory.updateParams(params) 132 self.host.memory.updateParams(params)
129 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self._assert) 133 self._getParams(Const.NO_SECURITY_LIMIT).addCallback(self.assertParamExists_async)
130 self._getParams(0).addCallback(self._assert_not) 134 self._getParams(0).addCallback(self.assertParamNotExists_async)
131 self._getParams(1).addCallback(self._assert) 135 self._getParams(1).addCallback(self.assertParamExists_async)
132 136
133 def test_paramsRegisterApp(self): 137 def test_paramsRegisterApp(self):
134 # tests with no security level on the parameter (most secure) 138 # tests with no security level on the parameter (most secure)
135 params = self._getParamXML() 139 params = self._getParamXML()
136 self.host.memory.init() 140 self.host.memory.init()
137 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) 141 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
138 assert(self._paramExists()) 142 self.assertParamExists()
139 self.host.memory.init() 143 self.host.memory.init()
140 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) 144 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
141 assert(not self._paramExists()) 145 self.assertParamNotExists()
142 self.host.memory.init() 146 self.host.memory.init()
143 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) 147 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
144 assert(not self._paramExists()) 148 self.assertParamNotExists()
145 # tests with security level 0 on the parameter (not secure) 149 # tests with security level 0 on the parameter (not secure)
146 params = self._getParamXML(security_level=0) 150 params = self._getParamXML(security_level=0)
147 self.host.memory.init() 151 self.host.memory.init()
148 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) 152 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
149 assert(self._paramExists()) 153 self.assertParamExists()
150 self.host.memory.init() 154 self.host.memory.init()
151 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) 155 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
152 assert(self._paramExists()) 156 self.assertParamExists()
153 self.host.memory.init() 157 self.host.memory.init()
154 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) 158 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
155 assert(self._paramExists()) 159 self.assertParamExists()
156 # tests with security level 1 on the parameter (more secure) 160 # tests with security level 1 on the parameter (more secure)
157 params = self._getParamXML(security_level=1) 161 params = self._getParamXML(security_level=1)
158 self.host.memory.init() 162 self.host.memory.init()
159 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME) 163 self.host.memory.paramsRegisterApp(params, Const.NO_SECURITY_LIMIT, Const.APP_NAME)
160 assert(self._paramExists()) 164 self.assertParamExists()
161 self.host.memory.init() 165 self.host.memory.init()
162 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME) 166 self.host.memory.paramsRegisterApp(params, 0, Const.APP_NAME)
163 assert(not self._paramExists()) 167 self.assertParamNotExists()
164 self.host.memory.init() 168 self.host.memory.init()
165 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) 169 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
166 assert(self._paramExists()) 170 self.assertParamExists()
167 171
168 def test_paramsRegisterApp_getParams(self): 172 def test_paramsRegisterApp_getParams(self):
169 # test retrieving the parameter for a specific frontend 173 # test retrieving the parameter for a specific frontend
170 self.host.memory.init() 174 self.host.memory.init()
171 params = self._getParamXML(security_level=1) 175 params = self._getParamXML(security_level=1)
172 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME) 176 self.host.memory.paramsRegisterApp(params, 1, Const.APP_NAME)
173 self._getParams(1, '').addCallback(self._assert) 177 self._getParams(1, '').addCallback(self.assertParamExists_async)
174 self._getParams(1, Const.APP_NAME).addCallback(self._assert) 178 self._getParams(1, Const.APP_NAME).addCallback(self.assertParamExists_async)
175 self._getParams(1, 'another_dummy_frontend').addCallback(self._assert_not) 179 self._getParams(1, 'another_dummy_frontend').addCallback(self.assertParamNotExists_async)