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