TestCenter Reference
FieldValueTests.py
Go to the documentation of this file.
1 
2 # Already ported to Python 3
3 from __future__ import print_function
4 from builtins import str
5 from builtins import object
6 
7 # **InsertLicense** code author="Gereon A. Frey"
8 
10 
11 
52 
53 # -- system imports ----------------------------------------------------------------------------{{{-
54 import os
55 import xml.etree.cElementTree as etree
56 
57 import sys
58 
59 from random import shuffle
60 from datetime import datetime
61 # ----------------------------------------------------------------------------------------------}}}-
62 
63 # -- local imports -----------------------------------------------------------------------------{{{-
64 import mevis
65 
66 from TestSupport.ChangeSet import ChangeSet
67 # ----------------------------------------------------------------------------------------------}}}-
68 
69 # -- class FieldValueTestCaseIterator ----------------------------------------------------------{{{-
70 
72  # -- member variables ------------------------------------------------------------------------{{{-
73 
74  __filename = None
75 
76 
77  __fieldValueTestCaseSet = None
78 
79  __testCaseList = None
80 
81  __testCase = None
82 
83  __index = None
84 
85 
86  __changeSet = None
87 
88 
89  __running = None
90  # --------------------------------------------------------------------------------------------}}}-
91 
92  # -- def __init__ ----------------------------------------------------------------------------{{{-
93 
94  def __init__ (self, ctx, filename, testRunName=None, randomOrder=False):
95  self.__filename = filename
97 
98  self.__index = 0
99  self.__running = True
100  self.__statusString = ""
101 
102  # Set test run name to given value or a timestamp.
103  self.__testRunName = testRunName if testRunName else str(datetime.now())
104 
105  self.__changeSet = ChangeSet(ctx)
106 
107  if not self.__fieldValueTestCaseSet.load(self.__filename):
108  self.__running = False
109  self.__testCaseList = []
110  else:
111  # Build up the list of available test cases.
112  self.__testCaseList = sorted(self.__fieldValueTestCaseSet.getList())
113  if randomOrder:
114  shuffle(self.__testCaseList)
115  self.__setTestCase()
116  # --------------------------------------------------------------------------------------------}}}-
117 
118  # -- def getCurrentIndex --------------------------------------------------------------------------{{{-
119 
121  def getCurrentIndex (self):
122  return self.__index
123  # --------------------------------------------------------------------------------------------}}}-
124 
125  # -- def getStatusString --------------------------------------------------------------------------{{{-
126 
128  def getStatusString (self):
129  return self.__statusString
130  # --------------------------------------------------------------------------------------------}}}-
131 
132  # -- def getCurrentCaseName --------------------------------------------------------------------------{{{-
133 
135  def getCurrentCaseName (self):
136  return self.__testCaseList[self.__index]
137  # --------------------------------------------------------------------------------------------}}}-
138 
139  # -- def getIndex --------------------------------------------------------------------------{{{-
140 
142  def getNumCases (self):
143  return len(self.__testCaseList)
144  # --------------------------------------------------------------------------------------------}}}-
145 
146  # -- def isFinished --------------------------------------------------------------------------{{{-
147 
149  def isFinished (self):
150  return not self.__running
151  # --------------------------------------------------------------------------------------------}}}-
152 
153  # -- def next --------------------------------------------------------------------------------{{{-
154 
160  def next (self, verbose=False, saveEachCase = True):
161  failedFieldList = []
162  if not self.__running or not self.__testCase.verifyExpectedResults(failedFieldList=failedFieldList, findAll=True, verbose=verbose):
163  return False, failedFieldList
164  self.__testCase.saveResults(self.__testRunName, overwrite=True)
165  self.__index += 1
166  if self.__index < len(self.__testCaseList):
167  if saveEachCase:
168  # save result after each step. that way, in case the application crashes, results wont't be lost
169  self.__fieldValueTestCaseSet.save(self.__filename)
170  self.__setTestCase()
171  else:
172  self.__index = -1
173  self.__running = False
174  self.__fieldValueTestCaseSet.save(self.__filename)
175  self.__statusString = "finished"
176  return True, []
177  # --------------------------------------------------------------------------------------------}}}-
178 
179  # -- def finish --------------------------------------------------------------------------------{{{-
180 
181  def finish (self, verbose = False):
182  if not self.__running:
183  return False
184  if verbose:
185  print("finishing run")
186  self.__index = -1
187  self.__running = False
188  self.__fieldValueTestCaseSet.save(self.__filename)
189  self.__statusString = "finished"
190  return True
191  # --------------------------------------------------------------------------------------------}}}-
192 
193  # -- def prev --------------------------------------------------------------------------------{{{-
194 
195  def prev (self, verbose = False):
196  if not self.__running or self.__index == 0:
197  return False
198  if verbose:
199  print("going to prev test case")
200  self.__index -= 1
201  self.__setTestCase()
202  return True
203  # --------------------------------------------------------------------------------------------}}}-
204 
205  # -- def goToCaseWithId ----------------------------------------------------------------------{{{-
206 
207  def goToCaseWithId (self, id, verbose = False):
208  if not self.__running or id < 0 or id >= self.getNumCases():
209  return False
210  if verbose:
211  print("going to case with id ", id)
212  self.__index = id
213  self.__setTestCase()
214  return True
215  # --------------------------------------------------------------------------------------------}}}-
216 
217  # -- def __setTestCase -----------------------------------------------------------------------{{{-
218 
219  def __setTestCase (self):
220  self.__changeSet.revert()
221  self.__testCase = self.__fieldValueTestCaseSet.get(self.__testCaseList[self.__index])
222  self.__testCase.applyParameterization(self.__changeSet)
223  self.__statusString = "%d/%d (%s)" % (self.getCurrentIndex() + 1, self.getNumCases(), self.getCurrentCaseName())
224 
225  # --------------------------------------------------------------------------------------------}}}-
226 # ----------------------------------------------------------------------------------------------}}}-
227 
228 # -- class FieldValueTestCaseSet ---------------------------------------------------------------{{{-
229 
242 class FieldValueTestCaseSet (object):
243  # -- member variables ------------------------------------------------------------------------{{{-
244 
245  __ctx = None
246 
247 
248  __xmlTree = None
249 
250  __xmlRoot = None
251 
252 
253  __fieldValueTestCaseDict = None
254  # --------------------------------------------------------------------------------------------}}}-
255 
256  # -- def __init__ ----------------------------------------------------------------------------{{{-
257 
259  def __init__ (self, context):
260  self.__fieldValueTestCaseDict = {}
261 
262  self.__ctx = context
263 
264  self.__xmlRoot = etree.Element('FieldValueTestCaseSet')
265  self.__xmlTree = etree.ElementTree(self.__xmlRoot)
266  # --------------------------------------------------------------------------------------------}}}-
267 
268  # -- def load --------------------------------------------------------------------------------{{{-
269 
274  def load (self, filename):
275  retVal = True
276  if len(self.__fieldValueTestCaseDict) != 0:
277  mevis.MLAB.logError("There are already field-value test cases in this set. Loading not supported in this case!")
278  retVal = False
279  # Verify file with given filename exists.
280  elif not os.path.isfile(filename):
281  mevis.MLAB.logError("File %s does not exist." % (filename))
282  retVal = False
283  else:
284  # Load file and try to verify the file's content.
285  xmlTree = etree.parse(filename)
286  xmlRoot = xmlTree.getroot()
287  if not self.__verify(xmlRoot):
288  retVal = False
289  else:
290  self.__xmlTree = xmlTree
291  self.__xmlRoot = xmlRoot
292 
293  for node in self.__xmlRoot.findall('FieldValueTestCase'):
294  testCaseName = node.get('name')
295  self.__fieldValueTestCaseDict[testCaseName] = None
296  return retVal
297  # --------------------------------------------------------------------------------------------}}}-
298 
299  # -- def save --------------------------------------------------------------------------------{{{-
300 
301  def save (self, filename):
302  # Update the XML datastructure from the field-value test case objects.
303  for node in self.__xmlRoot.findall('FieldValueTestCase'):
304  name = node.get('name')
305  if self.__fieldValueTestCaseDict[name]:
306  self.__fieldValueTestCaseDict[name].save(node)
307  self.__xmlTree.write(filename)
308  # --------------------------------------------------------------------------------------------}}}-
309 
310  # -- def getList -----------------------------------------------------------------------------{{{-
311 
313  def getList (self):
314  return list(self.__fieldValueTestCaseDict.keys())
315  # --------------------------------------------------------------------------------------------}}}-
316 
317  # -- def add ---------------------------------------------------------------------------------{{{-
318 
323  def add (self, fieldValueTestCase):
324  retVal = True
325  name = fieldValueTestCase.getName()
326  if name in self.__fieldValueTestCaseDict:
327  mevis.MLAB.logError("Test with name %s already known!" % (name))
328  retVal = False
329  else:
330  self.__fieldValueTestCaseDict[name] = fieldValueTestCase
331  self.__xmlRoot.append(fieldValueTestCase.save())
332  return retVal
333  # --------------------------------------------------------------------------------------------}}}-
334 
335  # -- def copy --------------------------------------------------------------------------------{{{-
336 
342  def copy (self, source, destination):
343  retVal = True
344  if not source in self.__fieldValueTestCaseDict:
345  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (source))
346  retVal = False
347  else:
348  node = self.__getFieldValueTestCaseNode(source)
349  if node is None:
350  retVal = False
351  else:
352  # Save source to be sure to copy the current state. If the source has
353  # not be used after loading there is no FieldValueTestCase object in
354  # the dict and then nothing must be saved.
355  if not self.__fieldValueTestCaseDict[source] is None:
356  self.__fieldValueTestCaseDict[source].save(node)
357  # Add new node to the internal data structures.
358  retVal = self.add(FieldValueTestCase(destination, self.__ctx, node))
359  return retVal
360  # --------------------------------------------------------------------------------------------}}}-
361 
362  # -- def remove ------------------------------------------------------------------------------{{{-
363 
367  def remove (self, name):
368  retVal = True
369  if not name in self.__fieldValueTestCaseDict:
370  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
371  retVal = False
372  else:
373  node = self.__getFieldValueTestCaseNode(name)
374  if node is None:
375  retVal = False
376  else:
377  self.__xmlRoot.remove(node)
378  del self.__fieldValueTestCaseDict[name]
379  return retVal
380  # --------------------------------------------------------------------------------------------}}}-
381 
382  # -- def rename ------------------------------------------------------------------------------{{{-
383 
390  def rename (self, name, newName):
391  retVal = True
392  if not name in self.__fieldValueTestCaseDict:
393  mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
394  retVal = False
395  elif newName in self.__fieldValueTestCaseDict:
396  mevis.MLAB.logError("FieldValueTestCase with name %s already exists!" % (newName))
397  retVal = False
398  else:
399  node = self.__getFieldValueTestCaseNode(name)
400  if node is None:
401  # Failed to find the given field-value test case in the XML data structure.
402  retVal = False
403  else:
404  # Set node name in the XML data structure.
405  node.set('name', newName)
406  # If field-value test case is loaded set name there and rename it in
407  # the dictionary with all field-value test cases.
408  fieldValueTestCase = self.__fieldValueTestCaseDict[name]
409  if not fieldValueTestCase is None:
410  fieldValueTestCase.setName(newName)
411  self.__fieldValueTestCaseDict[newName] = fieldValueTestCase
412  del self.__fieldValueTestCaseDict[name]
413  return retVal
414  # --------------------------------------------------------------------------------------------}}}-
415 
416  # -- def get ---------------------------------------------------------------------------------{{{-
417 
421  def get (self, name):
422  fieldValueTestCase = None
423  if name not in self.__fieldValueTestCaseDict:
424  mevis.MLAB.logError("FieldValueTestCase with name %s not found!" % (name))
425  else:
426  # If the field-value test case has not yet been created: do that.
427  if not self.__fieldValueTestCaseDict[name]:
428  node = self.__getFieldValueTestCaseNode(name)
429  if not node is None:
430  self.__fieldValueTestCaseDict[name] = FieldValueTestCase(name, self.__ctx, node)
431  fieldValueTestCase = self.__fieldValueTestCaseDict[name]
432  return fieldValueTestCase
433  # --------------------------------------------------------------------------------------------}}}-
434 
435  # -- def __verify ----------------------------------------------------------------------------{{{-
436  def __verify (self, xmlRoot):
437  if not xmlRoot.tag == "FieldValueTestCaseSet":
438  return False
439 
440  for fieldValueTestCaseNode in xmlRoot.getchildren():
441  if not fieldValueTestCaseNode.tag == "FieldValueTestCase":
442  mevis.MLAB.logError("Only FieldValueTestCase nodes allowed.")
443  return False
444  settingsNode = fieldValueTestCaseNode.find("Settings")
445  resultsNode = fieldValueTestCaseNode.find("Results")
446  if settingsNode is None or resultsNode is None:
447  mevis.MLAB.logError("Settings and Results node must be defined.")
448  return False
449  if len(fieldValueTestCaseNode.getchildren()) != 2:
450  mevis.MLAB.logError("Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode.getchildren())))
451  return False
452  flList = [False, False, False]
453  for fieldListNode in settingsNode.getchildren():
454  if fieldListNode.tag == "FieldValueList":
455  name = fieldListNode.get("name")
456  if name == "Parameterization" and not flList[0]:
457  flList[0] = True
458  elif name == "ExpectedResults" and not flList[1]:
459  flList[1] = True
460  else:
461  mevis.MLAB.logError("Only FieldValueLists with name Parameterization or ExpectedResults allowed here.")
462  return False
463  elif fieldListNode.tag == "FieldList":
464  name = fieldListNode.get("name")
465  if name == "ResultsToSave" and not flList[2]:
466  flList[2] = True
467  else:
468  mevis.MLAB.logError("Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
469  return False
470  else:
471  mevis.MLAB.logError("Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag))
472  return False
473  for child in fieldListNode:
474  if child.tag != "Field":
475  mevis.MLAB.logError("There must only be nodes of type Field.")
476  return False
477  if not (child.get("module") != None and child.get("field") != None and child.get("type") != None):
478  mevis.MLAB.logError("Each field must have information on the module, field and type.")
479  return False
480  if not flList[0] or not flList[1] or not flList[2]:
481  mevis.MLAB.logError("There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave).")
482  return False
483  for fieldListNode in resultsNode.getchildren():
484  pass
485  return True
486  # --------------------------------------------------------------------------------------------}}}-
487 
488  # -- def __getFieldValueTestCaseNode ---------------------------------------------------------{{{-
489 
497  def __getFieldValueTestCaseNode (self, name):
498  for node in self.__xmlRoot.findall('FieldValueTestCase'):
499  if node.get('name') == name:
500  return node
501  mevis.MLAB.logError("FieldValueTestCase %s not found in XML data-structure!" % (name))
502  return None
503  # --------------------------------------------------------------------------------------------}}}-
504 # ----------------------------------------------------------------------------------------------}}}-
505 
506 # -- class FieldValueTestCase -----------------------------------------------------------------{{{-
507 
513 class FieldValueTestCase (object):
514  # -- member variables ------------------------------------------------------------------------{{{-
515 
516  __name = None
517 
518  __ctx = None
519 
520 
521  __fieldValueListDict = None
522  # --------------------------------------------------------------------------------------------}}}-
523 
524  # -- class FieldListBase ---------------------------------------------------------------------{{{-
525 
527  class FieldListBase (list):
528  # -- member variables ----------------------------------------------------------------------{{{-
529 
531  _type = None
532  # ------------------------------------------------------------------------------------------}}}-
533 
534  # -- def __init__ --------------------------------------------------------------------------{{{-
535 
537  def __init__ (self, name):
538  self.__name = name
539  # ------------------------------------------------------------------------------------------}}}-
540 
541  # -- def add -------------------------------------------------------------------------------{{{-
542 
548  def add (self, fieldInfo, index=None):
549  retVal = index
550  if not index is None:
551  if index < 0:
552  index = 0
553  if index > len(self):
554  index = len(self)
555  else:
556  index = len(self)
557  return self._add(index, fieldInfo)
558  # ------------------------------------------------------------------------------------------}}}-
559 
560  # -- def remove ----------------------------------------------------------------------------{{{-
561 
564  def remove (self, index):
565  retVal = True
566  if index >= 0 and index < len(self):
567  del self[index]
568  else:
569  retVal = False
570  return retVal
571  # ------------------------------------------------------------------------------------------}}}-
572 
573  # -- def swap ------------------------------------------------------------------------------{{{-
574 
578  def swap (self, indexA, indexB):
579  retVal = True
580  if indexA>=0 and indexA<len(self) and indexB>=0 and indexB<len(self) and indexA!=indexB:
581  tmp = self[indexA]
582  self[indexA] = self[indexB]
583  self[indexB] = tmp
584  else:
585  retVal = False
586  return retVal
587  # ------------------------------------------------------------------------------------------}}}-
588 
589  # -- def load ------------------------------------------------------------------------------{{{-
590 
592  def load (self, xmlNode):
593  for fieldNode in xmlNode.findall('Field'):
594  self.append(self._loadField(fieldNode))
595  # ------------------------------------------------------------------------------------------}}}-
596 
597  # -- def save ------------------------------------------------------------------------------{{{-
598 
600  def save (self):
601  xmlNode = etree.Element(self._type, name=self.__name)
602  for fieldInfo in self:
603  self._saveField(fieldInfo, xmlNode)
604  return xmlNode
605  # ------------------------------------------------------------------------------------------}}}-
606  # --------------------------------------------------------------------------------------------}}}-
607 
608  # -- class FieldList -------------------------------------------------------------------------{{{-
609 
611  # -- def __init__ --------------------------------------------------------------------------{{{-
612 
614  def __init__ (self, name):
615  self._type = 'FieldList'
616  FieldValueTestCase.FieldListBase.__init__(self, name)
617  # ------------------------------------------------------------------------------------------}}}-
618 
619  # -- def update ----------------------------------------------------------------------------{{{-
620 
623  def update (self, index, fieldInfo):
624  return
625  # ------------------------------------------------------------------------------------------}}}-
626 
627  # -- def _add ------------------------------------------------------------------------------{{{-
628 
631  def _add (self, index, fieldInfo):
632  self.insert(index, fieldInfo[0:3])
633  # ------------------------------------------------------------------------------------------}}}-
634 
635  # -- def _loadField ------------------------------------------------------------------------{{{-
636 
639  def _loadField (self, xmlNode):
640  return (xmlNode.get("module"), xmlNode.get("field"), xmlNode.get("type"))
641  # ------------------------------------------------------------------------------------------}}}-
642 
643  # -- def _saveField ------------------------------------------------------------------------{{{-
644 
647  def _saveField (self, fieldInfo, xmlNode):
648  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
649  # ------------------------------------------------------------------------------------------}}}-
650  # --------------------------------------------------------------------------------------------}}}-
651 
652  # -- class FieldValueList --------------------------------------------------------------------{{{-
653 
655  # -- def __init__ --------------------------------------------------------------------------{{{-
656  def __init__ (self, name):
657  self._type = 'FieldValueList'
658  FieldValueTestCase.FieldListBase.__init__(self, name)
659  # ------------------------------------------------------------------------------------------}}}-
660 
661  # -- def update ----------------------------------------------------------------------------{{{-
662 
665  def update (self, index, fieldInfo):
666  fieldType = fieldInfo[2]
667  fieldValue = self.__convertValue(fieldInfo[3], fieldType)
668  self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
669  # ------------------------------------------------------------------------------------------}}}-
670 
671  # -- def _add ------------------------------------------------------------------------------{{{-
672 
675  def _add (self, index, fieldInfo):
676  self.insert(index, fieldInfo)
677  # ------------------------------------------------------------------------------------------}}}-
678 
679  # -- def _loadField ------------------------------------------------------------------------{{{-
680 
683  def _loadField (self, xmlNode):
684  fieldType = xmlNode.get("type")
685  fieldValue = self.__convertValue(xmlNode.text, fieldType)
686  return (xmlNode.get("module"), xmlNode.get("field"), fieldType, fieldValue)
687  # ------------------------------------------------------------------------------------------}}}-
688 
689  # -- def _saveField ------------------------------------------------------------------------{{{-
690 
693  def _saveField (self, fieldInfo, xmlNode):
694  if fieldInfo[2] not in ('Trigger'):
695  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = str(fieldInfo[3])
696  else:
697  etree.SubElement(xmlNode, 'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
698  # ------------------------------------------------------------------------------------------}}}-
699 
700  # -- def __convertValue ----------------------------------------------------------------------{{{-
701 
705  def __convertValue (self, value, type):
706  if type == "Integer":
707  value = int(value)
708  elif type in ('Float', 'Double'):
709  value = float(value)
710  elif 'Vector' in type:
711  value = tuple([float(x) for x in value[1:-1].split(',')])
712  elif type == 'Bool':
713  value = value=="True"
714  elif type == 'Trigger':
715  value = ""
716  else:
717  if type not in ('Enum', 'String'):
718  mevis.MLAB.log("Maybe type %s should be supported?!" % (type))
719  return value
720  # --------------------------------------------------------------------------------------------}}}-
721  # --------------------------------------------------------------------------------------------}}}-
722 
723  # -- def __init__ ----------------------------------------------------------------------------{{{-
724 
730  def __init__ (self, name, ctx, xmlNode=None):
731  self.__name = name
732  self.__ctx = ctx
733  self.__fieldValueListDict = { 'Parameterization': FieldValueTestCase.FieldValueList('Parameterization'),
734  'ExpectedResults': FieldValueTestCase.FieldValueList('ExpectedResults'),
735  'ResultsToSave': FieldValueTestCase.FieldList('ResultsToSave'),
736  'SavedResults':{} }
737 
738  # If an XML node is given load it.
739  if xmlNode:
740  self.__load(xmlNode)
741  # --------------------------------------------------------------------------------------------}}}-
742 
743  # -- def getName -----------------------------------------------------------------------------{{{-
744 
746  def getName (self):
747  return self.__name
748  # --------------------------------------------------------------------------------------------}}}-
749 
750  # -- def setName -----------------------------------------------------------------------------{{{-
751 
753  def setName (self, name):
754  self.__name = name
755  # --------------------------------------------------------------------------------------------}}}-
756 
757  # -- def save --------------------------------------------------------------------------------{{{-
758 
762  def save (self, xmlNode=None):
763  # Create an empty XML node if none was specified. Otherwise clean the
764  # existing node and put the current settings in.
765  if not xmlNode:
766  xmlNode = etree.Element('FieldValueTestCase', name=self.__name)
767  settingsNode = etree.SubElement(xmlNode, 'Settings')
768  resultsNode = etree.SubElement(xmlNode, 'Results')
769  else:
770  settingsNode = xmlNode.find('Settings')
771  resultsNode = xmlNode.find('Results')
772  settingsNode.clear()
773  resultsNode.clear()
774 
775  # Save the settings.
776  settingsNode.append(self.__fieldValueListDict['Parameterization'].save())
777  settingsNode.append(self.__fieldValueListDict['ExpectedResults'].save())
778  settingsNode.append(self.__fieldValueListDict['ResultsToSave'].save())
779  for result in self.__fieldValueListDict['SavedResults']:
780  resultsNode.append(self.__fieldValueListDict['SavedResults'][result].save())
781 
782  return xmlNode
783  # --------------------------------------------------------------------------------------------}}}-
784 
785  # -- def getParameterization -----------------------------------------------------------------{{{-
786 
789  return self.__fieldValueListDict['Parameterization']
790  # --------------------------------------------------------------------------------------------}}}-
791 
792  # -- def applyParameterization ---------------------------------------------------------------{{{-
793 
798  def applyParameterization (self, changeSet, verbose=False):
799  for item in self.__fieldValueListDict['Parameterization']:
800  fieldName = "%s.%s" % (item[0], item[1])
801  if item[2] in ("Trigger"):
802  self.__ctx.field(fieldName).touch()
803  else:
804  changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
805  # --------------------------------------------------------------------------------------------}}}-
806 
807  # -- def getExpectedResults ------------------------------------------------------------------{{{-
808 
810  def getExpectedResults (self):
811 
813  return self.__fieldValueListDict['ExpectedResults']
814  # --------------------------------------------------------------------------------------------}}}-
815 
816  # -- def verifyExpectedResults ---------------------------------------------------------------{{{-
817 
822  def verifyExpectedResults (self, failedFieldList=[], findAll=False, verbose=False):
823  retValue = True
824  for item in self.__fieldValueListDict['ExpectedResults']:
825  fieldName = "%s.%s" % (item[0], item[1])
826  field = self.__ctx.field(fieldName)
827  if not field:
828  if verbose:
829  mevis.MLAB.logError("Unknown field: %s." % (fieldName))
830  else:
831  match = False
832  if field.type in ("Float", "Double"):
833  # TODO Use configurable epsilon.
834  match = abs(field.value-item[3]) < 0.0001
835  else:
836  match = field.value == item[3]
837  if not match:
838  failedFieldList.append(fieldName)
839  if verbose:
840  mevis.MLAB.logError("Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3]))
841  if not findAll:
842  return False
843  retValue = False
844  return retValue
845  # --------------------------------------------------------------------------------------------}}}-
846 
847  # -- def getResultsToSave --------------------------------------------------------------------{{{-
848 
850  def getResultsToSave (self):
851  return self.__fieldValueListDict['ResultsToSave']
852  # --------------------------------------------------------------------------------------------}}}-
853 
854  # -- def saveResults -------------------------------------------------------------------------{{{-
855 
860  def saveResults (self, name, overwrite=False):
861  retVal = True
862  if not overwrite and name in self.__fieldValueListDict['SavedResults']:
863  mevis.MLAB.logError("Result with name %s available already." % (name))
864  retVal = False
865  else:
866  resList = self.__fieldValueListDict['SavedResults'][name] = FieldValueTestCase.FieldValueList(name)
867  for item in self.__fieldValueListDict['ResultsToSave']:
868  fldName = "%s.%s" % (item[0], item[1])
869  if item[2] == "Trigger":
870  self.__ctx.field(fldName).touch()
871  fldValue = ""
872  else:
873  fldValue = self.__ctx.field(fldName).value
874  resList.append(tuple((item[0], item[1], item[2], fldValue)))
875  return retVal
876  # --------------------------------------------------------------------------------------------}}}-
877 
878  # -- def getSavedResultList ------------------------------------------------------------------{{{-
879 
881  def getSavedResultList (self):
882  return sorted(self.__fieldValueListDict['SavedResults'].keys())
883  # --------------------------------------------------------------------------------------------}}}-
884 
885  # -- def addSavedResultList ------------------------------------------------------------------{{{-
886 
888  def addSavedResultList (self, name, resultList):
889  retVal = True
890  if name in self.__fieldValueListDict['SavedResults']:
891  mevis.MLAB.logError("Result with name %s available already." % (name))
892  retVal = False
893  else:
894  resList = FieldValueTestCase.FieldValueList(name)
895  resToSaveList = []
896  for item in self.__fieldValueListDict['ResultsToSave']:
897  resToSaveList.append("%s.%s" % (item[0], item[1]))
898  for item in resultList:
899  field = "%s.%s" % (item[0], item[1])
900  if not field in resToSaveList:
901  mevis.MLAB.logError("Field %s not in list of results to save. Ignoring it." % (field))
902  else:
903  resList.add(item)
904  self.__fieldValueListDict['SavedResults'][name] = resList
905  return retVal
906  # --------------------------------------------------------------------------------------------}}}-
907 
908  # -- def getSavedResult ----------------------------------------------------------------------{{{-
909 
911  def getSavedResult (self, name):
912  if name not in self.__fieldValueListDict['SavedResults']:
913  mevis.MLAB.logError('No result with name %s available.' % (name))
914  return None
915  return self.__fieldValueListDict['SavedResults'][name]
916  # --------------------------------------------------------------------------------------------}}}-
917 
918  # -- def __load ------------------------------------------------------------------------------{{{-
919 
921  def __load (self, xmlNode):
922  node = xmlNode.find('Settings')
923  for fieldValueListNode in node.findall('FieldValueList'):
924  self.__fieldValueListDict[fieldValueListNode.get('name')].load(fieldValueListNode)
925  for fieldListNode in node.findall('FieldList'):
926  self.__fieldValueListDict[fieldListNode.get('name')].load(fieldListNode)
927  for resultNode in xmlNode.findall('Results/FieldValueList'):
928  name = resultNode.get('name')
929  self.__fieldValueListDict['SavedResults'].setdefault(name, FieldValueTestCase.FieldValueList(name))
930  self.__fieldValueListDict['SavedResults'][name].load(resultNode)
931  # --------------------------------------------------------------------------------------------}}}-
932 # ----------------------------------------------------------------------------------------------}}}-
933 
934 #//# MeVis signature v1
935 #//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
936 #//# owner: MeVis
937 #//# date: 2010-11-26T09:00:38
938 #//# hash: rMwMlcYQJQHOxPbybrFYtdjiuX+LFIHs/PgH3XbmXzWLw97M+Pv+FGNwyD0VMD9vLOsMHIIyRwUumHpGFR6koQ==
939 #//# MeVis end
def getNumCases(self)
Return the number of cases of the loaded test case set.
def __load(self, xmlNode)
Load the settings from the given XML node.
def prev(self, verbose=False)
Go back to the previous test case.
A class to iterate over the list of field-value test cases of a given set.
def getCurrentIndex(self)
Return the index of the current case.
__name
The name of the field-value test case.
def save(self)
Save the field information to XML.
def isFinished(self)
Are there any test cases left to iterate?
__fieldValueTestCaseSet
The field-value test case set.
def rename(self, name, newName)
Rename a given field-value test case.
def save(self, xmlNode=None)
Save the settings of this field-value test case to an XML node.
def getCurrentCaseName(self)
Return the index of the current case.
Class to handle field changes and make them revertable.
Definition: ChangeSet.py:26
def remove(self, name)
Remove the field-value test case with the given name.
Superclass for the field lists used in the field-value test case.
def update(self, index, fieldInfo)
Update the values of the given fields.
A class implementing the field-value test cases.
def next(self, verbose=False, saveEachCase=True)
Change to the next field-value test case.
def getList(modInfo, itemListName)
Definition: Formal.py:26
__xmlRoot
The root of the XML data structure.
__index
The index of the currently processed test case in the list.
def update(self, index, fieldInfo)
Update the values of the given fields.
__ctx
The context used for the field-value test case.
def load(self, xmlNode)
Load the given xmlNode.
def remove(self, index)
Remove the element at the given index.
__ctx
Context the field-value test cases are working on.
def touch(fieldName, verbose=True)
Touch the given field.
Definition: Fields.py:26
def getStatusString(self)
Return a string containing the current case id, current case name and number of cases.
__fieldValueTestCaseDict
Dictionary with all available field-value test cases.
__running
Variable to track the state of the iterator.
def getSavedResultList(self)
Get a sorted list of names of the saved results.
__filename
The file containing the field-value test cases.
def copy(self, source, destination)
Create a copy of the given field-value test case with the given new name.
def getList(self)
Return the list of names of the existing field-value test cases.
A class collecting a set of field-value test cases.
def addSavedResultList(self, name, resultList)
Add saved results.
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 getResultsToSave(self)
Return the results to save.
__changeSet
The ChangeSet used in to track the field changes.
def __setTestCase(self)
Load the test case and apply the parameterization.
def finish(self, verbose=False)
Finish the current test run.
def swap(self, indexA, indexB)
Swap the given two elements.
List of field information including field values.
def load(self, filename)
Try to load the field-value test case set from the given XML file.
def goToCaseWithId(self, id, verbose=False)
Go to test case with specified id.
def add(self, fieldInfo, index=None)
Add the field with given info to the list.
def __init__(self, ctx, filename, testRunName=None, randomOrder=False)
The default constructor.
def __getFieldValueTestCaseNode(self, name)
Helper method to get the XML node of a field-value test case with a given name.
def setName(self, name)
Set the name of the field-value test case.
def __init__(self, name, ctx, xmlNode=None)
The default constructor.
def applyParameterization(self, changeSet, verbose=False)
Apply the parameterization to the context given in the constructor.
__fieldValueListDict
The dictionary of field value lists.
def __init__(self, context)
The default constructor.
def __convertValue(self, value, type)
Convert a value to a given type.
def verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False)
Return the expected result set.
def save(self, filename)
Save the XML data structure.
def getParameterization(self)
Return the parameterization set.
def saveResults(self, name, overwrite=False)
Save the given field values to the xml data structure.
def getName(self)
Return the name of the field-value test case.
def add(self, fieldValueTestCase)
Add the given field-value test case to the FieldValueTestCaseSet.
def get(self, name)
Return the field-value test case with the given name.