Package qm :: Package test :: Module parameter_database
[hide private]
[frames] | no frames]

Source Code for Module qm.test.parameter_database

  1  ######################################################################## 
  2  # 
  3  # File:   parameter_database.py 
  4  # Author: Stefan Seefeld 
  5  # Date:   2005-01-17 
  6  # 
  7  # Contents: 
  8  #   Test database that parametrizes another test database.. 
  9  # 
 10  # Copyright (c) 2005 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # imports 
 18  ######################################################################## 
 19   
 20  from qm.test.database import * 
 21  from qm.test.suite import * 
 22   
 23  ######################################################################## 
 24  # classes 
 25  ######################################################################## 
 26   
27 -class ParameterDatabase(Database):
28 """A database that parametrizes another database. 'Parameter' in this 29 context refers to any name that is used as a label, and which stands 30 for a specific set of arguments passed to tests. 31 32 The 'ParameterDatabase' class is abstract. Subclasses need to implement 33 the '_GetParametersForTest' as well as the '_GetArgumentsForParameter' 34 method. 35 """ 36
37 - class ImplicitSuite(Suite):
38 """ImplicitSuite represents a suite obtained from a test and a set 39 of parameters applied to it.""" 40
41 - def __init__(self, db, suite_id):
42 43 Suite.__init__(self, {}, 44 qmtest_id = suite_id, 45 qmtest_database = db)
46 47
48 - def GetTestIds(self):
49 50 database = self.GetDatabase() 51 id = self.GetId() 52 return map(lambda p, db = database, id = id: db.JoinLabels(id, p), 53 database._GetParametersForTest(id))
54 55
56 - def IsImplicit(self):
57 58 return True
59 60
61 - class WrapperSuite(Suite):
62 """As tests from the wrapped Database are mapped to suites, suites 63 from the wrapped Database have to be recreated with all tests 64 replaced by ImplicitSuite instances. Additionally, new (explicit) 65 suites may be added.""" 66
67 - def __init__(self, db, suite, suite_ids = None):
68 """Take over suite_ids from the wrapped database but replace 69 test_ids by suite_ids if there are parameters available for 70 them.""" 71 72 Suite.__init__(self, {}, 73 qmtest_id = suite.GetId(), 74 qmtest_database = db) 75 self.__suite = suite 76 self.__suite_ids = suite_ids or []
77 78
79 - def GetSuiteIds(self):
80 81 suite_ids = self.__suite.GetSuiteIds() 82 for t in self.__suite.GetTestIds(): 83 if self.GetDatabase()._GetParametersForTest(t): 84 suite_ids.append(t) 85 return suite_ids + self.__suite_ids
86 87
88 - def GetTestIds(self):
89 90 test_ids = [] 91 for t in self.__suite.GetTestIds(): 92 if not self.GetDatabase()._GetParametersForTest(t): 93 test_ids.append(t) 94 return test_ids
95 96
97 - def IsImplicit(self):
98 99 return self.__suite.IsImplicit()
100 101
102 - def GetAllTestAndSuiteIds(self):
103 104 db = self.GetDatabase() 105 orig_test_ids, suite_ids = self.__suite.GetAllTestAndSuiteIds() 106 test_ids = [] 107 for test in orig_test_ids: 108 parameters = db._GetParametersForTest(test) 109 if parameters: 110 suite_ids.append(test) 111 test_ids.extend(map(lambda p, test=test, db=db: 112 db.JoinLabels(test, p), 113 parameters)) 114 else: 115 test_ids.append(test) 116 return test_ids, suite_ids + self.__suite_ids
117 118
119 - class ParameterSuite(Suite):
120 """ParameterSuite represents a suite obtained from applying a 121 given parameter to a suite from the wrapped DB.""" 122
123 - def __init__(self, database, suite, parameter):
124 """Construct a ParameterSuite. 125 126 database -- The database this suite refers to. 127 128 suite -- The original suite this suite parametrizes. 129 130 parameter -- The value for the parameter to apply to the suite.""" 131 132 Suite.__init__(self, {}, 133 qmtest_id = database.JoinLabels(suite.GetId(), 134 parameter), 135 qmtest_database = database) 136 self.__suite = suite 137 self.__parameter = parameter
138 139
140 - def GetSuiteIds(self):
141 """ParameterSuites contain ParameterSuites which wrap suites 142 contained in the wrapped suite.""" 143 144 database = self.GetDatabase() 145 return map(lambda id, db = database, p = self.__parameter: 146 db.JoinLabels(id, p), self.__suite.GetSuiteIds())
147 148
149 - def GetTestIds(self):
150 """ParameterSuites contain Tests obtained by applying the given 151 parameter set to the tests contained in the wrapped suite.""" 152 153 database = self.GetDatabase() 154 return map(lambda id, db = database, p = self.__parameter: 155 db.JoinLabels(id, p), self.__suite.GetTestIds())
156 157
158 - def IsImplicit(self):
159 160 return False
161 162
163 - def __init__(self, database, path, arguments):
164 165 self.label_class = database.label_class 166 Database.__init__(self, path, arguments) 167 self.__db = database
168 169
170 - def GetWrappedDatabase(self):
171 172 return self.__db
173 174
175 - def _GetParametersForTest(self, test_id):
176 """Return a list of parameters that can be applied to the test 177 'test_id'.""" 178 179 return []
180 181
182 - def _GetArgumentsForParameter(self, test_id, parameter):
183 """Return the set of arguments for this parameter. 184 185 'test_id' -- The test id to which the parameter belongs. 186 187 'parameter' -- The parameter for which the arguments are queried. 188 189 returns -- A dictionary containing the argument as name/value pairs.""" 190 191 return {}
192 193
194 - def GetTest(self, test_id):
195 196 directory, basename = self.SplitLabel(test_id) 197 198 if not self.HasTest(test_id): 199 raise NoSuchTestError, test_id 200 201 test = self.__db.GetTest(directory) 202 # now generate a new test 203 arguments = test.GetArguments() 204 205 #override parameters 206 arguments.update(self._GetArgumentsForParameter(directory, basename)) 207 return TestDescriptor(self, test_id, test.GetClassName(), 208 arguments)
209
210 - def HasTest(self, test_id):
211 212 # If test_id is a parametrized test, its basename has to refer to 213 # a parameter set and the directory to a test in the wrapped DB. 214 # 215 # Else basename must not be a parameter set, and test_id a test 216 # in the wrapped DB. 217 218 directory, basename = self.SplitLabel(test_id) 219 220 return (basename in self._GetParametersForTest(directory) 221 and self.__db.HasTest(directory) 222 or self.__db.HasTest(test_id))
223 224
225 - def GetTestIds(self, directory="", scan_subdirs=1):
226 227 # directory may be a test in the wrapped DB... 228 if self.__db.HasTest(directory): 229 ids = [directory] 230 # ...or a directory, in which case we only take it into account 231 # if scan_subdirs == 1. 232 elif scan_subdirs: 233 ids = self.__db.GetTestIds(directory, scan_subdirs) 234 else: 235 return [] 236 237 tests = [] 238 for p in self._GetParametersForTest(directory): 239 tests += map(lambda x, p = p, db = self: db.JoinLabels(x, p), 240 ids) 241 return tests
242 243
244 - def GetSuite(self, suite_id):
245 246 # If suite_id refers to a suite in the WD, wrap it. 247 if self.__db.HasSuite(suite_id): 248 return ParameterDatabase.WrapperSuite(self, 249 self.__db.GetSuite(suite_id)) 250 251 # If suite_id refers to a test in the WD, parametrize it. 252 elif self.__db.HasTest(suite_id): 253 return ParameterDatabase.ImplicitSuite(self, suite_id) 254 255 raise NoSuchSuiteError, suite_id
256 257
258 - def HasSuite(self, suite_id):
259 260 directory, basename = self.SplitLabel(suite_id) 261 return (basename in self._GetParametersForTest(directory) 262 and self.__db.HasSuite(directory) 263 or self.__db.HasSuite(suite_id) 264 or self.__db.HasTest(suite_id))
265 266
267 - def GetSuiteIds(self, directory="", scan_subdirs=1):
268 269 if self.__db.HasTest(directory): 270 return [] 271 272 suite_ids = self.__db.GetSuiteIds(directory, scan_subdirs) 273 test_ids = self.__db.GetTestIds(directory, scan_subdirs) 274 param_ids = map(lambda p, d = directory, db = self: 275 db.JoinLabels(d, p), 276 self._GetParametersForTest(directory)) 277 278 # The set of all (non-recursive) suite ids is composed of the 279 # original suite ids plus original test ids (now being suite ids) 280 # as well as explicit suites obtained by combining the given 281 # directory with all parameters. 282 ids = suite_ids + test_ids + param_ids 283 if not scan_subdirs: 284 return ids 285 else: 286 # The set of all suite ids is composed of the set above plus 287 # everything above combined with all parameters. 288 expl_ids = [] 289 for p in self._GetParametersForTest(directory): 290 expl_ids += map(lambda x, p = p, db = self: 291 db.JoinLabels(x, p), suite_ids + test_ids) 292 return ids + expl_ids
293 294
295 - def GetResource(self, resource_id):
296 297 return self.__db.GetResource(resource_id)
298 299
300 - def HasResource(self, resource_id):
301 302 return self.__db.HasResource(resource_id)
303 304
305 - def GetResourceIds(self, directory="", scan_subdirs=1):
306 307 if self.__db.HasTest(directory): 308 return [] 309 310 return self.__db.GetResourceIds(directory, scan_subdirs)
311 312
313 - def GetIds(self, kind, directory="", scan_subdirs = 1):
314 315 if kind == Database.TEST: 316 return self.GetTestIds(directory, scan_subdirs) 317 elif kind == Database.RESOURCE: 318 return self.GetResourceIds(directory, scan_subdirs) 319 else: 320 return self.__db.GetIds(kind, directory, scan_subdirs)
321 322
323 - def GetSubdirectories(self, directory):
324 325 # GetSubdirectories returns the subdirectories of the given directory. 326 # As this Database turns all tests from the wrapped Database into 327 # ImplicitSuites, we have to account for them here. 328 # 329 # Further, while 'directory' has to be a directory when looked at it 330 # from this Database, it may well be a test in the context of the wrapped 331 # Database, in which case it can't have subdirectories. 332 if self.__db.HasTest(directory): 333 return [] 334 335 subdirs = self.__db.GetSubdirectories(directory) 336 subdirs += [d[directory and len(directory) + 1 or 0:] # Remove common prefix. 337 for d in self.__db.GetTestIds(directory, False)] 338 return subdirs
339 340
341 - def GetAttachmentStore(self):
342 343 return self.__db.GetAttachmentStore()
344 345
346 - def GetClassPath(self):
347 348 return self.__db.GetClassPath()
349 350
351 - def GetTestClassNames(self):
352 353 return self.__db.GetTestClassNames()
354 355
356 - def GetResourceClassNames(self):
357 358 return self.__db.GetResourceClassNames()
359