TestCenter Reference
FieldValueTests.py
Go to the documentation of this file.
1 #
2 # Copyright 2009, MeVis Medical Solutions AG
3 #
4 # The user may use this file in accordance with the license agreement provided with
5 # the Software or, alternatively, in accordance with the terms contained in a
6 # written agreement between the user and MeVis Medical Solutions AG.
7 #
8 # For further information use the contact form at https://www.mevislab.de/contact
9 #
10 
11 from builtins import str
12 from builtins import object
13 
14 
16 
17 
58 
59 # -- system imports ----------------------------------------------------------------------------{{{-
60 import os
61 import xml.etree.cElementTree as etree
62 
63 import sys
64 
65 from random import shuffle
66 from datetime import datetime
67 # ----------------------------------------------------------------------------------------------}}}-
68 
69 # -- local imports -----------------------------------------------------------------------------{{{-
70 import mevis
71 
72 from TestSupport.ChangeSet import ChangeSet
73 # ----------------------------------------------------------------------------------------------}}}-
74 
75 # -- class FieldValueTestCaseIterator ----------------------------------------------------------{{{-
76 
78  # -- member variables ------------------------------------------------------------------------{{{-
79 
80  __filename = None
81 
82 
83  __fieldValueTestCaseSet = None
84 
85  __testCaseList = None
86 
87  __testCase = None
88 
89  __index = None
90 
91 
92  __changeSet = None
93 
94 
95  __running = None
96  # --------------------------------------------------------------------------------------------}}}-
97 
98  # -- def __init__ ----------------------------------------------------------------------------{{{-
99 
100  def __init__ (self, ctx, filename, testRunName=None, randomOrder=False):
101  self.__filename__filename = filename
102  self.__fieldValueTestCaseSet__fieldValueTestCaseSet = FieldValueTestCaseSet(ctx)
103 
104  self.__index__index = 0
105  self.__running__running = True
106  self.__statusString__statusString = ""
107 
108  # Set test run name to given value or a timestamp.
109  self.__testRunName__testRunName = testRunName if testRunName else str(datetime.now())
110 
111  self.__changeSet__changeSet = ChangeSet(ctx)
112 
113  if not self.__fieldValueTestCaseSet__fieldValueTestCaseSet.load(self.__filename__filename):
114  self.__running__running = False
115  self.__testCaseList__testCaseList = []
116  else:
117  # Build up the list of available test cases.
118  self.__testCaseList__testCaseList = sorted(self.__fieldValueTestCaseSet__fieldValueTestCaseSet.getList())
119  if randomOrder:
120  shuffle(self.__testCaseList__testCaseList)
121  self.__setTestCase__setTestCase()
122  # --------------------------------------------------------------------------------------------}}}-
123 
124  # -- def getCurrentIndex --------------------------------------------------------------------------{{{-
125 
127  def getCurrentIndex (self):
128  return self.__index__index
129  # --------------------------------------------------------------------------------------------}}}-
130 
131  # -- def getStatusString --------------------------------------------------------------------------{{{-
132 
134  def getStatusString (self):
135  return self.__statusString__statusString
136  # --------------------------------------------------------------------------------------------}}}-
137 
138  # -- def getCurrentCaseName --------------------------------------------------------------------------{{{-
139 
141  def getCurrentCaseName (self):
142  return self.__testCaseList__testCaseList[self.__index__index]
143  # --------------------------------------------------------------------------------------------}}}-
144 
145  # -- def getIndex --------------------------------------------------------------------------{{{-
146 
148  def getNumCases (self):
149  return len(self.__testCaseList__testCaseList)
150  # --------------------------------------------------------------------------------------------}}}-
151 
152  # -- def isFinished --------------------------------------------------------------------------{{{-
153 
155  def isFinished (self):
156  return not self.__running__running
157  # --------------------------------------------------------------------------------------------}}}-
158 
159  # -- def next --------------------------------------------------------------------------------{{{-
160 
166  def next (self, verbose=False, saveEachCase = True):
167  failedFieldList = []
168  if not self.__running__running or not self.__testCase__testCase.verifyExpectedResults(failedFieldList=failedFieldList, findAll=True, verbose=verbose):
169  return False, failedFieldList
170  self.__testCase__testCase.saveResults(self.__testRunName__testRunName, overwrite=True)
171  self.__index__index += 1
172  if self.__index__index < len(self.__testCaseList__testCaseList):
173  if saveEachCase:
174  # save result after each step. that way, in case the application crashes, results wont't be lost
175  self.__fieldValueTestCaseSet__fieldValueTestCaseSet.save(self.__filename__filename)
176  self.__setTestCase__setTestCase()
177  else:
178  self.__index__index = -1
179  self.__running__running = False
180  self.__fieldValueTestCaseSet__fieldValueTestCaseSet.save(self.__filename__filename)
181  self.__statusString__statusString = "finished"
182  return True, []
183  # --------------------------------------------------------------------------------------------}}}-
184 
185  # -- def finish --------------------------------------------------------------------------------{{{-
186 
187  def finish (self, verbose = False):
188  if not self.__running__running:
189  return False
190  if verbose:
191  print("finishing run")
192  self.__index__index = -1
193  self.__running__running = False
194  self.__fieldValueTestCaseSet__fieldValueTestCaseSet.save(self.__filename__filename)
195  self.__statusString__statusString = "finished"
196  return True
197  # --------------------------------------------------------------------------------------------}}}-
198 
199  # -- def prev --------------------------------------------------------------------------------{{{-
200 
201  def prev (self, verbose = False):
202  if not self.__running__running or self.__index__index == 0:
203  return False
204  if verbose:
205  print("going to prev test case")
206  self.__index__index -= 1
207  self.__setTestCase__setTestCase()
208  return True
209  # --------------------------------------------------------------------------------------------}}}-
210 
211  # -- def goToCaseWithId ----------------------------------------------------------------------{{{-
212 
213  def goToCaseWithId (self, id, verbose = False):
214  if not self.__running__running or id < 0 or id >= self.getNumCasesgetNumCases():
215  return False
216  if verbose:
217  print("going to case with id ", id)
218  self.__index__index = id
219  self.__setTestCase__setTestCase()
220  return True
221  # --------------------------------------------------------------------------------------------}}}-
222 
223  # -- def __setTestCase -----------------------------------------------------------------------{{{-
224 
225  def __setTestCase (self):
226  self.__changeSet__changeSet.revert()
227  self.__testCase__testCase = self.__fieldValueTestCaseSet__fieldValueTestCaseSet.get(self.__testCaseList__testCaseList[self.__index__index])
228  self.__testCase__testCase.applyParameterization(self.__changeSet__changeSet)
229  self.__statusString__statusString = "%d/%d (%s)" % (self.getCurrentIndexgetCurrentIndex() + 1, self.getNumCasesgetNumCases(), self.getCurrentCaseNamegetCurrentCaseName())
230 
231  # --------------------------------------------------------------------------------------------}}}-
232 # ----------------------------------------------------------------------------------------------}}}-
233 
234 # -- class FieldValueTestCaseSet ---------------------------------------------------------------{{{-
235 
248 class FieldValueTestCaseSet (object):
249  # -- member variables ------------------------------------------------------------------------{{{-
250 
251  __ctx = None
252 
253 
254  __xmlTree = None
255 
256  __xmlRoot = None
257 
258 
259  __fieldValueTestCaseDict = None
260  # --------------------------------------------------------------------------------------------}}}-
261 
262  # -- def __init__ ----------------------------------------------------------------------------{{{-
263 
265  def __init__ (self, context):
266  self.__fieldValueTestCaseDict__fieldValueTestCaseDict = {}
267 
268  self.__ctx__ctx = context
269 
270  self.__xmlRoot__xmlRoot = etree.Element('FieldValueTestCaseSet')
271  self.__xmlTree__xmlTree = etree.ElementTree(self.__xmlRoot__xmlRoot)
272  # --------------------------------------------------------------------------------------------}}}-
273 
274  # -- def load --------------------------------------------------------------------------------{{{-
275 
280  def load (self, filename):
281  retVal = True
282  if len(self.__fieldValueTestCaseDict__fieldValueTestCaseDict) != 0:
283  mevis.MLAB.logError("There are already field-value test cases in this set. Loading not supported in this case!")
284  retVal = False
285  # Verify file with given filename exists.
286  elif not os.path.isfile(filename):
287  mevis.MLAB.logError("File %s does not exist." % (filename))
288  retVal = False
289  else:
290  # Load file and try to verify the file's content.
291  xmlTree = etree.parse(filename)
292  xmlRoot = xmlTree.getroot()
293  if not self.__verify__verify(xmlRoot):
294  retVal = False
295  else:
296  self.__xmlTree__xmlTree = xmlTree
297  self.__xmlRoot__xmlRoot = xmlRoot
298 
299  for node in self.__xmlRoot__xmlRoot.findall('FieldValueTestCase'):
300  testCaseName = node.get('name')
301  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[testCaseName] = None
302  return retVal
303  # --------------------------------------------------------------------------------------------}}}-
304 
305  # -- def save --------------------------------------------------------------------------------{{{-
306 
307  def save (self, filename):
308  # Update the XML datastructure from the field-value test case objects.
309  for node in self.__xmlRoot__xmlRoot.findall('FieldValueTestCase'):
310  name = node.get('name')
311  if self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]:
312  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name].save(node)
313  self.__xmlTree__xmlTree.write(filename)
314  # --------------------------------------------------------------------------------------------}}}-
315 
316  # -- def getList -----------------------------------------------------------------------------{{{-
317 
319  def getList (self):
320  return list(self.__fieldValueTestCaseDict__fieldValueTestCaseDict.keys())
321  # --------------------------------------------------------------------------------------------}}}-
322 
323  # -- def add ---------------------------------------------------------------------------------{{{-
324 
329  def add (self, fieldValueTestCase):
330  retVal = True
331  name = fieldValueTestCase.getName()
332  if name in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
333  mevis.MLAB.logError("Test with name %s already known!" % (name))
334  retVal = False
335  else:
336  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name] = fieldValueTestCase
337  self.__xmlRoot__xmlRoot.append(fieldValueTestCase.save())
338  return retVal
339  # --------------------------------------------------------------------------------------------}}}-
340 
341  # -- def copy --------------------------------------------------------------------------------{{{-
342 
348  def copy (self, source, destination):
349  retVal = True
350  if not source in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
351  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (source))
352  retVal = False
353  else:
354  node = self.__getFieldValueTestCaseNode__getFieldValueTestCaseNode(source)
355  if node is None:
356  retVal = False
357  else:
358  # Save source to be sure to copy the current state. If the source has
359  # not be used after loading there is no FieldValueTestCase object in
360  # the dict and then nothing must be saved.
361  if not self.__fieldValueTestCaseDict__fieldValueTestCaseDict[source] is None:
362  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[source].save(node)
363  # Add new node to the internal data structures.
364  retVal = self.addadd(FieldValueTestCase(destination, self.__ctx__ctx, node))
365  return retVal
366  # --------------------------------------------------------------------------------------------}}}-
367 
368  # -- def remove ------------------------------------------------------------------------------{{{-
369 
373  def remove (self, name):
374  retVal = True
375  if not name in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
376  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
377  retVal = False
378  else:
379  node = self.__getFieldValueTestCaseNode__getFieldValueTestCaseNode(name)
380  if node is None:
381  retVal = False
382  else:
383  self.__xmlRoot__xmlRoot.remove(node)
384  del self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]
385  return retVal
386  # --------------------------------------------------------------------------------------------}}}-
387 
388  # -- def rename ------------------------------------------------------------------------------{{{-
389 
396  def rename (self, name, newName):
397  retVal = True
398  if not name in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
399  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
400  retVal = False
401  elif newName in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
402  mevis.MLAB.logError("FieldValueTestCase with name %s already exists!" % (newName))
403  retVal = False
404  else:
405  node = self.__getFieldValueTestCaseNode__getFieldValueTestCaseNode(name)
406  if node is None:
407  # Failed to find the given field-value test case in the XML data structure.
408  retVal = False
409  else:
410  # Set node name in the XML data structure.
411  node.set('name', newName)
412  # If field-value test case is loaded set name there and rename it in
413  # the dictionary with all field-value test cases.
414  fieldValueTestCase = self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]
415  if not fieldValueTestCase is None:
416  fieldValueTestCase.setName(newName)
417  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[newName] = fieldValueTestCase
418  del self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]
419  return retVal
420  # --------------------------------------------------------------------------------------------}}}-
421 
422  # -- def get ---------------------------------------------------------------------------------{{{-
423 
427  def get (self, name):
428  fieldValueTestCase = None
429  if name not in self.__fieldValueTestCaseDict__fieldValueTestCaseDict:
430  mevis.MLAB.logError("FieldValueTestCase with name %s not found!" % (name))
431  else:
432  # If the field-value test case has not yet been created: do that.
433  if not self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]:
434  node = self.__getFieldValueTestCaseNode__getFieldValueTestCaseNode(name)
435  if not node is None:
436  self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name] = FieldValueTestCase(name, self.__ctx__ctx, node)
437  fieldValueTestCase = self.__fieldValueTestCaseDict__fieldValueTestCaseDict[name]
438  return fieldValueTestCase
439  # --------------------------------------------------------------------------------------------}}}-
440 
441  # -- def __verify ----------------------------------------------------------------------------{{{-
442  def __verify (self, xmlRoot):
443  if not xmlRoot.tag == "FieldValueTestCaseSet":
444  return False
445 
446  for fieldValueTestCaseNode in xmlRoot:
447  if not fieldValueTestCaseNode.tag == "FieldValueTestCase":
448  mevis.MLAB.logError("Only FieldValueTestCase nodes allowed.")
449  return False
450  settingsNode = fieldValueTestCaseNode.find("Settings")
451  resultsNode = fieldValueTestCaseNode.find("Results")
452  if settingsNode is None or resultsNode is None:
453  mevis.MLAB.logError("Settings and Results node must be defined.")
454  return False
455  if len(fieldValueTestCaseNode) != 2:
456  mevis.MLAB.logError("Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode)))
457  return False
458  flList = [False, False, False]
459  for fieldListNode in settingsNode:
460  if fieldListNode.tag == "FieldValueList":
461  name = fieldListNode.get("name")
462  if name == "Parameterization" and not flList[0]:
463  flList[0] = True
464  elif name == "ExpectedResults" and not flList[1]:
465  flList[1] = True
466  else:
467  mevis.MLAB.logError("Only FieldValueLists with name Parameterization or ExpectedResults allowed here.")
468  return False
469  elif fieldListNode.tag == "FieldList":
470  name = fieldListNode.get("name")
471  if name == "ResultsToSave" and not flList[2]:
472  flList[2] = True
473  else:
474  mevis.MLAB.logError("Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
475  return False
476  else:
477  mevis.MLAB.logError("Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag))
478  return False
479  for child in fieldListNode:
480  if child.tag != "Field":
481  mevis.MLAB.logError("There must only be nodes of type Field.")
482  return False
483  if not (child.get("module") != None and child.get("field") != None and child.get("type") != None):
484  mevis.MLAB.logError("Each field must have information on the module, field and type.")
485  return False
486  if not flList[0] or not flList[1] or not flList[2]:
487  mevis.MLAB.logError("There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave).")
488  return False
489  for fieldListNode in resultsNode:
490  pass
491  return True
492  # --------------------------------------------------------------------------------------------}}}-
493 
494  # -- def __getFieldValueTestCaseNode ---------------------------------------------------------{{{-
495 
503  def __getFieldValueTestCaseNode (self, name):
504  for node in self.__xmlRoot__xmlRoot.findall('FieldValueTestCase'):
505  if node.get('name') == name:
506  return node
507  mevis.MLAB.logError("FieldValueTestCase %s not found in XML data-structure!" % (name))
508  return None
509  # --------------------------------------------------------------------------------------------}}}-
510 # ----------------------------------------------------------------------------------------------}}}-
511 
512 # -- class FieldValueTestCase -----------------------------------------------------------------{{{-
513 
519 class FieldValueTestCase (object):
520  # -- member variables ------------------------------------------------------------------------{{{-
521 
522  __name = None
523 
524  __ctx = None
525 
526 
527  __fieldValueListDict = None
528  # --------------------------------------------------------------------------------------------}}}-
529 
530  # -- class FieldListBase ---------------------------------------------------------------------{{{-
531 
533  class FieldListBase (list):
534  # -- member variables ----------------------------------------------------------------------{{{-
535 
537  _type = None
538  # ------------------------------------------------------------------------------------------}}}-
539 
540  # -- def __init__ --------------------------------------------------------------------------{{{-
541 
543  def __init__ (self, name):
544  self.__name__name = name
545  # ------------------------------------------------------------------------------------------}}}-
546 
547  # -- def add -------------------------------------------------------------------------------{{{-
548 
554  def add (self, fieldInfo, index=None):
555  retVal = index
556  if not index is None:
557  if index < 0:
558  index = 0
559  if index > len(self):
560  index = len(self)
561  else:
562  index = len(self)
563  return self._add(index, fieldInfo)
564  # ------------------------------------------------------------------------------------------}}}-
565 
566  # -- def remove ----------------------------------------------------------------------------{{{-
567 
570  def remove (self, index):
571  retVal = True
572  if index >= 0 and index < len(self):
573  del self[index]
574  else:
575  retVal = False
576  return retVal
577  # ------------------------------------------------------------------------------------------}}}-
578 
579  # -- def swap ------------------------------------------------------------------------------{{{-
580 
584  def swap (self, indexA, indexB):
585  retVal = True
586  if indexA>=0 and indexA<len(self) and indexB>=0 and indexB<len(self) and indexA!=indexB:
587  tmp = self[indexA]
588  self[indexA] = self[indexB]
589  self[indexB] = tmp
590  else:
591  retVal = False
592  return retVal
593  # ------------------------------------------------------------------------------------------}}}-
594 
595  # -- def load ------------------------------------------------------------------------------{{{-
596 
598  def load (self, xmlNode):
599  for fieldNode in xmlNode.findall('Field'):
600  self.append(self._loadField(fieldNode))
601  # ------------------------------------------------------------------------------------------}}}-
602 
603  # -- def save ------------------------------------------------------------------------------{{{-
604 
606  def save (self):
607  xmlNode = etree.Element(self._type_type, name=self.__name__name)
608  for fieldInfo in self:
609  self._saveField(fieldInfo, xmlNode)
610  return xmlNode
611  # ------------------------------------------------------------------------------------------}}}-
612  # --------------------------------------------------------------------------------------------}}}-
613 
614  # -- class FieldList -------------------------------------------------------------------------{{{-
615 
617  # -- def __init__ --------------------------------------------------------------------------{{{-
618 
620  def __init__ (self, name):
621  self._type_type_type = 'FieldList'
622  FieldValueTestCase.FieldListBase.__init__(self, name)
623  # ------------------------------------------------------------------------------------------}}}-
624 
625  # -- def update ----------------------------------------------------------------------------{{{-
626 
629  def update (self, index, fieldInfo):
630  return
631  # ------------------------------------------------------------------------------------------}}}-
632 
633  # -- def _add ------------------------------------------------------------------------------{{{-
634 
637  def _add (self, index, fieldInfo):
638  self.insert(index, fieldInfo[0:3])
639  # ------------------------------------------------------------------------------------------}}}-
640 
641  # -- def _loadField ------------------------------------------------------------------------{{{-
642 
645  def _loadField (self, xmlNode):
646  return (xmlNode.get("module"), xmlNode.get("field"), xmlNode.get("type"))
647  # ------------------------------------------------------------------------------------------}}}-
648 
649  # -- def _saveField ------------------------------------------------------------------------{{{-
650 
653  def _saveField (self, fieldInfo, xmlNode):
654  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
655  # ------------------------------------------------------------------------------------------}}}-
656  # --------------------------------------------------------------------------------------------}}}-
657 
658  # -- class FieldValueList --------------------------------------------------------------------{{{-
659 
661  # -- def __init__ --------------------------------------------------------------------------{{{-
662  def __init__ (self, name):
663  self._type_type_type = 'FieldValueList'
664  FieldValueTestCase.FieldListBase.__init__(self, name)
665  # ------------------------------------------------------------------------------------------}}}-
666 
667  # -- def update ----------------------------------------------------------------------------{{{-
668 
671  def update (self, index, fieldInfo):
672  fieldType = fieldInfo[2]
673  fieldValue = self.__convertValue__convertValue(fieldInfo[3], fieldType)
674  self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
675  # ------------------------------------------------------------------------------------------}}}-
676 
677  # -- def _add ------------------------------------------------------------------------------{{{-
678 
681  def _add (self, index, fieldInfo):
682  self.insert(index, fieldInfo)
683  # ------------------------------------------------------------------------------------------}}}-
684 
685  # -- def _loadField ------------------------------------------------------------------------{{{-
686 
689  def _loadField (self, xmlNode):
690  fieldType = xmlNode.get("type")
691  fieldValue = self.__convertValue__convertValue(xmlNode.text, fieldType)
692  return (xmlNode.get("module"), xmlNode.get("field"), fieldType, fieldValue)
693  # ------------------------------------------------------------------------------------------}}}-
694 
695  # -- def _saveField ------------------------------------------------------------------------{{{-
696 
699  def _saveField (self, fieldInfo, xmlNode):
700  if fieldInfo[2] not in ('Trigger'):
701  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = str(fieldInfo[3])
702  else:
703  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
704  # ------------------------------------------------------------------------------------------}}}-
705 
706  # -- def __convertValue ----------------------------------------------------------------------{{{-
707 
711  def __convertValue (self, value, type):
712  if type == "Integer":
713  value = int(value)
714  elif type in ('Float', 'Double'):
715  value = float(value)
716  elif 'Vector' in type:
717  value = tuple([float(x) for x in value[1:-1].split(',')])
718  elif type == 'Bool':
719  value = value=="True"
720  elif type == 'Trigger':
721  value = ""
722  else:
723  if type not in ('Enum', 'String'):
724  mevis.MLAB.log("Maybe type %s should be supported?!" % (type))
725  return value
726  # --------------------------------------------------------------------------------------------}}}-
727  # --------------------------------------------------------------------------------------------}}}-
728 
729  # -- def __init__ ----------------------------------------------------------------------------{{{-
730 
736  def __init__ (self, name, ctx, xmlNode=None):
737  self.__name__name = name
738  self.__ctx__ctx = ctx
739  self.__fieldValueListDict__fieldValueListDict = { 'Parameterization': FieldValueTestCase.FieldValueList('Parameterization'),
740  'ExpectedResults': FieldValueTestCase.FieldValueList('ExpectedResults'),
741  'ResultsToSave': FieldValueTestCase.FieldList('ResultsToSave'),
742  'SavedResults':{} }
743 
744  # If an XML node is given load it.
745  if xmlNode:
746  self.__load__load(xmlNode)
747  # --------------------------------------------------------------------------------------------}}}-
748 
749  # -- def getName -----------------------------------------------------------------------------{{{-
750 
752  def getName (self):
753  return self.__name__name
754  # --------------------------------------------------------------------------------------------}}}-
755 
756  # -- def setName -----------------------------------------------------------------------------{{{-
757 
759  def setName (self, name):
760  self.__name__name = name
761  # --------------------------------------------------------------------------------------------}}}-
762 
763  # -- def save --------------------------------------------------------------------------------{{{-
764 
768  def save (self, xmlNode=None):
769  # Create an empty XML node if none was specified. Otherwise clean the
770  # existing node and put the current settings in.
771  if not xmlNode:
772  xmlNode = etree.Element('FieldValueTestCase', name=self.__name__name)
773  settingsNode = etree.SubElement(xmlNode, 'Settings')
774  resultsNode = etree.SubElement(xmlNode, 'Results')
775  else:
776  settingsNode = xmlNode.find('Settings')
777  resultsNode = xmlNode.find('Results')
778  settingsNode.clear()
779  resultsNode.clear()
780 
781  # Save the settings.
782  settingsNode.append(self.__fieldValueListDict__fieldValueListDict['Parameterization'].save())
783  settingsNode.append(self.__fieldValueListDict__fieldValueListDict['ExpectedResults'].save())
784  settingsNode.append(self.__fieldValueListDict__fieldValueListDict['ResultsToSave'].save())
785  for result in self.__fieldValueListDict__fieldValueListDict['SavedResults']:
786  resultsNode.append(self.__fieldValueListDict__fieldValueListDict['SavedResults'][result].save())
787 
788  return xmlNode
789  # --------------------------------------------------------------------------------------------}}}-
790 
791  # -- def getParameterization -----------------------------------------------------------------{{{-
792 
795  return self.__fieldValueListDict__fieldValueListDict['Parameterization']
796  # --------------------------------------------------------------------------------------------}}}-
797 
798  # -- def applyParameterization ---------------------------------------------------------------{{{-
799 
804  def applyParameterization (self, changeSet, verbose=False):
805  for item in self.__fieldValueListDict__fieldValueListDict['Parameterization']:
806  fieldName = "%s.%s" % (item[0], item[1])
807  if item[2] in ("Trigger"):
808  self.__ctx__ctx.field(fieldName).touch()
809  else:
810  changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
811  # --------------------------------------------------------------------------------------------}}}-
812 
813  # -- def getExpectedResults ------------------------------------------------------------------{{{-
814 
816  def getExpectedResults (self):
817 
819  return self.__fieldValueListDict__fieldValueListDict['ExpectedResults']
820  # --------------------------------------------------------------------------------------------}}}-
821 
822  # -- def verifyExpectedResults ---------------------------------------------------------------{{{-
823 
828  def verifyExpectedResults (self, failedFieldList=[], findAll=False, verbose=False):
829  retValue = True
830  for item in self.__fieldValueListDict__fieldValueListDict['ExpectedResults']:
831  fieldName = "%s.%s" % (item[0], item[1])
832  field = self.__ctx__ctx.field(fieldName)
833  if not field:
834  if verbose:
835  mevis.MLAB.logError("Unknown field: %s." % (fieldName))
836  else:
837  match = False
838  if field.type in ("Float", "Double"):
839  # TODO Use configurable epsilon.
840  match = abs(field.value-item[3]) < 0.0001
841  else:
842  match = field.value == item[3]
843  if not match:
844  failedFieldList.append(fieldName)
845  if verbose:
846  mevis.MLAB.logError("Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3]))
847  if not findAll:
848  return False
849  retValue = False
850  return retValue
851  # --------------------------------------------------------------------------------------------}}}-
852 
853  # -- def getResultsToSave --------------------------------------------------------------------{{{-
854 
856  def getResultsToSave (self):
857  return self.__fieldValueListDict__fieldValueListDict['ResultsToSave']
858  # --------------------------------------------------------------------------------------------}}}-
859 
860  # -- def saveResults -------------------------------------------------------------------------{{{-
861 
866  def saveResults (self, name, overwrite=False):
867  retVal = True
868  if not overwrite and name in self.__fieldValueListDict__fieldValueListDict['SavedResults']:
869  mevis.MLAB.logError("Result with name %s available already." % (name))
870  retVal = False
871  else:
872  resList = self.__fieldValueListDict__fieldValueListDict['SavedResults'][name] = FieldValueTestCase.FieldValueList(name)
873  for item in self.__fieldValueListDict__fieldValueListDict['ResultsToSave']:
874  fldName = "%s.%s" % (item[0], item[1])
875  if item[2] == "Trigger":
876  self.__ctx__ctx.field(fldName).touch()
877  fldValue = ""
878  else:
879  fldValue = self.__ctx__ctx.field(fldName).value
880  resList.append(tuple((item[0], item[1], item[2], fldValue)))
881  return retVal
882  # --------------------------------------------------------------------------------------------}}}-
883 
884  # -- def getSavedResultList ------------------------------------------------------------------{{{-
885 
887  def getSavedResultList (self):
888  return sorted(self.__fieldValueListDict__fieldValueListDict['SavedResults'].keys())
889  # --------------------------------------------------------------------------------------------}}}-
890 
891  # -- def addSavedResultList ------------------------------------------------------------------{{{-
892 
894  def addSavedResultList (self, name, resultList):
895  retVal = True
896  if name in self.__fieldValueListDict__fieldValueListDict['SavedResults']:
897  mevis.MLAB.logError("Result with name %s available already." % (name))
898  retVal = False
899  else:
900  resList = FieldValueTestCase.FieldValueList(name)
901  resToSaveList = []
902  for item in self.__fieldValueListDict__fieldValueListDict['ResultsToSave']:
903  resToSaveList.append("%s.%s" % (item[0], item[1]))
904  for item in resultList:
905  field = "%s.%s" % (item[0], item[1])
906  if not field in resToSaveList:
907  mevis.MLAB.logError("Field %s not in list of results to save. Ignoring it." % (field))
908  else:
909  resList.add(item)
910  self.__fieldValueListDict__fieldValueListDict['SavedResults'][name] = resList
911  return retVal
912  # --------------------------------------------------------------------------------------------}}}-
913 
914  # -- def getSavedResult ----------------------------------------------------------------------{{{-
915 
917  def getSavedResult (self, name):
918  if name not in self.__fieldValueListDict__fieldValueListDict['SavedResults']:
919  mevis.MLAB.logError('No result with name %s available.' % (name))
920  return None
921  return self.__fieldValueListDict__fieldValueListDict['SavedResults'][name]
922  # --------------------------------------------------------------------------------------------}}}-
923 
924  # -- def __load ------------------------------------------------------------------------------{{{-
925 
927  def __load (self, xmlNode):
928  node = xmlNode.find('Settings')
929  for fieldValueListNode in node.findall('FieldValueList'):
930  self.__fieldValueListDict__fieldValueListDict[fieldValueListNode.get('name')].load(fieldValueListNode)
931  for fieldListNode in node.findall('FieldList'):
932  self.__fieldValueListDict__fieldValueListDict[fieldListNode.get('name')].load(fieldListNode)
933  for resultNode in xmlNode.findall('Results/FieldValueList'):
934  name = resultNode.get('name')
935  self.__fieldValueListDict__fieldValueListDict['SavedResults'].setdefault(name, FieldValueTestCase.FieldValueList(name))
936  self.__fieldValueListDict__fieldValueListDict['SavedResults'][name].load(resultNode)
937  # --------------------------------------------------------------------------------------------}}}-
938 # ----------------------------------------------------------------------------------------------}}}-
939 
940 #//# MeVis signature v1
941 #//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
942 #//# owner: MeVis
943 #//# date: 2010-11-26T09:00:38
944 #//# hash: rMwMlcYQJQHOxPbybrFYtdjiuX+LFIHs/PgH3XbmXzWLw97M+Pv+FGNwyD0VMD9vLOsMHIIyRwUumHpGFR6koQ==
945 #//# MeVis end
Class to handle field changes and make them revertable.
Definition: ChangeSet.py:35
A class to iterate over the list of field-value test cases of a given set.
def finish(self, verbose=False)
Finish the current test run.
def isFinished(self)
Are there any test cases left to iterate?
def getCurrentCaseName(self)
Return the index of the current case.
def next(self, verbose=False, saveEachCase=True)
Change to the next field-value test case.
def __init__(self, ctx, filename, testRunName=None, randomOrder=False)
The default constructor.
def getNumCases(self)
Return the number of cases of the loaded test case set.
def goToCaseWithId(self, id, verbose=False)
Go to test case with specified id.
def getCurrentIndex(self)
Return the index of the current case.
def getStatusString(self)
Return a string containing the current case id, current case name and number of cases.
def prev(self, verbose=False)
Go back to the previous test case.
A class collecting a set of field-value test cases.
def getList(self)
Return the list of names of the existing field-value test cases.
def remove(self, name)
Remove the field-value test case with the given name.
def save(self, filename)
Save the XML data structure.
def rename(self, name, newName)
Rename a given field-value test case.
def __init__(self, context)
The default constructor.
def load(self, filename)
Try to load the field-value test case set from the given XML file.
def add(self, fieldValueTestCase)
Add the given field-value test case to the FieldValueTestCaseSet.
def copy(self, source, destination)
Create a copy of the given field-value test case with the given new name.
def get(self, name)
Return the field-value test case with the given name.
Superclass for the field lists used in the field-value test case.
def remove(self, index)
Remove the element at the given index.
def add(self, fieldInfo, index=None)
Add the field with given info to the list.
def swap(self, indexA, indexB)
Swap the given two elements.
def save(self)
Save the field information to XML.
def load(self, xmlNode)
Load the given xmlNode.
def update(self, index, fieldInfo)
Update the values of the given fields.
List of field information including field values.
def update(self, index, fieldInfo)
Update the values of the given fields.
A class implementing the field-value test cases.
def addSavedResultList(self, name, resultList)
Add saved results.
def getName(self)
Return the name of the field-value test case.
def getSavedResultList(self)
Get a sorted list of names of the saved results.
def saveResults(self, name, overwrite=False)
Save the given field values to the xml data structure.
def getSavedResult(self, name)
Get the field-value list saved under the given name.
def getExpectedResults(self)
Getter for the list of expected results.
def __init__(self, name, ctx, xmlNode=None)
The default constructor.
def getParameterization(self)
Return the parameterization set.
def save(self, xmlNode=None)
Save the settings of this field-value test case to an XML node.
def applyParameterization(self, changeSet, verbose=False)
Apply the parameterization to the context given in the constructor.
def getResultsToSave(self)
Return the results to save.
def verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False)
Return the expected result set.
def setName(self, name)
Set the name of the field-value test case.
def touch(fieldName, verbose=True)
Touch the given field.
Definition: Fields.py:33
def getList(modInfo, itemListName)
Definition: Formal.py:34