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

Source Code for Module qm.test.run_database

  1  ######################################################################## 
  2  # 
  3  # File:   run_database.py 
  4  # Author: Mark Mitchell 
  5  # Date:   2005-08-08 
  6  # 
  7  # Contents: 
  8  #   QMTest RunDatabase class. 
  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.extension import Extension 
 21  from qm.test.result import Result 
 22  from qm.common import parse_time_iso 
 23   
 24  ######################################################################## 
 25  # Classes 
 26  ######################################################################## 
 27   
28 -class RunDatabase(Extension):
29 """A 'RunDatabase' stores 'TestRun's. 30 31 A 'RunDatabase' provides a mechanism for selecting 'TestRun's that 32 meet particular criteria.""" 33
34 - def GetAllRuns(self):
35 """Return all the 'TestRun's in the database. 36 37 returns -- A sequence consisting of all of the 'TestRun's in 38 the database.""" 39 40 raise NotImplementedError
41 42
43 - def GetRuns(self, predicate):
44 """Return the set of 'TestRun's satisfying 'predicate' 45 46 'predicate' -- A callable that can be passed one 'TestRun' 47 argument. 48 49 returns -- A sequence of 'TestRun's consisting only of those 50 'TestRun's in the database for which 'predicate' returns a 51 true value.""" 52 53 return filter(self.GetAllRuns(), predicate)
54 55
56 - def GetAnnotations(self, key):
57 """Return the set of annotations for 'key' from all test runs. 58 59 'key' -- A string used to look up the annotations. 60 61 returns -- A set of (distinct) annotations for 'key' from all 62 test runs.""" 63 64 # We can't use sets since we want to remain python 2.2 compatible. 65 annotations = [] 66 for r in self.GetAllRuns(): 67 value = r.GetAnnotation(key) 68 if value not in annotations: 69 annotations.append(value) 70 return annotations
71 72
73 - def GetTimeframe(self, time_key, is_iso_time = True):
74 """Return a pair of min / max values found for the given time_key 75 across all test runs. 76 77 'time_key' -- Annotation key referring to a string convertible to 78 either iso-formatted time or floating point number. 79 80 returns -- minimum, maximum.""" 81 82 minimum = None 83 maximum = None 84 for r in self.GetAllRuns(): 85 time_string = r.GetAnnotation(time_key) 86 if not time_string: 87 continue 88 if is_iso_time: 89 time = parse_time_iso(time_string) 90 else: 91 time = float(time_string) 92 if not minimum or minimum > time: 93 minimum = time 94 if not maximum or maximum < time: 95 # Make sure the largest value is still inside the interval. 96 maximum = time + 0.1 97 return minimum, maximum
98 99
100 - def GetRunInTimeframe(self, key, value, time_key, minimum, maximum, is_iso_time = True):
101 """Return a test run id matching the key and timeframe.""" 102 103 for i in range(len(self.GetAllRuns())): 104 r = self.GetAllRuns()[i] 105 if r.GetAnnotation(key) != value: 106 continue 107 time_string = r.GetAnnotation(time_key) 108 if not time_string: 109 continue 110 if is_iso_time: 111 time = parse_time_iso(time_string) 112 else: 113 time = float(time_string) 114 if time >= minimum and time < maximum: 115 return i 116 # No match. 117 return None
118 119
120 - def GetRunsByAnnotations(self, annotation_filter):
121 """Return the 'TestRun's matching 'annotation_filter'. 122 123 'annotation_filter' -- A dictionary mapping annotation keys 124 (strings) to values (either strings or callables). 125 126 returns -- A sequence of 'TestRun's consisting only of those 127 'TestRun's in the database that match the 128 'annotation_filter'. A 'TestRun' matches the 129 'annotation_filter' if it matches each of the key-value pairs 130 in the filter. If the value in such a pair is a string, then 131 the annotation in the 'TestRun' must exactly match the value. 132 If the value is a callable, rather than a string, then when 133 passed the value from the 'TestRun', the predicate must return 134 a true value.""" 135 136 def predicate(run): 137 for key, pattern in annotation_filter.iteritems(): 138 value = run.GetAnnotation(key) 139 if callable(pattern): 140 if not pattern(value): 141 return False 142 elif value != pattern: 143 return False 144 return True
145 146 return self.GetRuns(predicate)
147 148
149 - def GetOutcomes(self, id, kind = Result.TEST):
150 """Return an outcome dictionary for the indicated test. 151 152 'id' -- The name of a test, suite, or resource item. 153 154 'kind' -- The kind of the item to retrieve the outcome for. 155 156 returns -- A dictionary indicating the number of outcomes per category.""" 157 158 outcomes = {Result.PASS: 0, 159 Result.FAIL: 0, 160 Result.ERROR: 0, 161 Result.UNTESTED: 0} 162 for r in self.GetAllRuns(): 163 result = r.GetResult(id, kind) 164 if result: 165 outcomes[result.GetOutcome()] += 1 166 else: 167 outcomes[Result.UNTESTED] += 1 168 return outcomes
169