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
67# -- local imports -----------------------------------------------------------------------------{{{-
68import mevis
69
70from TestSupport.ChangeSet import ChangeSet
71
72# ----------------------------------------------------------------------------------------------}}}-
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
103
104 self.__index = 0
105 self.__running = True
106 self.__statusString = ""
107
108 # Set test run name to given value or a timestamp.
109 self.__testRunName = testRunName if testRunName else str(datetime.now())
110
111 self.__changeSet = ChangeSet(ctx)
112 self.__changeSet.enableAutoRevert(False)
113
114 if not self.__fieldValueTestCaseSet.load(self.__filename):
115 self.__running = False
116 self.__testCaseList = []
117 else:
118 # Build up the list of available test cases.
119 self.__testCaseList = sorted(self.__fieldValueTestCaseSet.getList())
120 if randomOrder:
121 shuffle(self.__testCaseList)
122 self.__setTestCase()
123
124 # --------------------------------------------------------------------------------------------}}}-
125
126 # -- def getCurrentIndex --------------------------------------------------------------------------{{{-
127
130 return self.__index
131
132 # --------------------------------------------------------------------------------------------}}}-
133
134 # -- def getStatusString --------------------------------------------------------------------------{{{-
135
138 return self.__statusString
139
140 # --------------------------------------------------------------------------------------------}}}-
141
142 # -- def getCurrentCaseName --------------------------------------------------------------------------{{{-
143
146 return self.__testCaseList[self.__index]
147
148 # --------------------------------------------------------------------------------------------}}}-
149
150 # -- def getIndex --------------------------------------------------------------------------{{{-
151
153 def getNumCases(self):
154 return len(self.__testCaseList)
155
156 # --------------------------------------------------------------------------------------------}}}-
157
158 # -- def isFinished --------------------------------------------------------------------------{{{-
159
161 def isFinished(self):
162 return not self.__running
163
164 # --------------------------------------------------------------------------------------------}}}-
165
166 # -- def next --------------------------------------------------------------------------------{{{-
167
173 def next(self, verbose=False, saveEachCase=True):
174 failedFieldList = []
175 if not self.__running or not self.__testCase.verifyExpectedResults(
176 failedFieldList=failedFieldList, findAll=True, verbose=verbose
177 ):
178 return False, failedFieldList
179 self.__testCase.saveResults(self.__testRunName, overwrite=True)
180 self.__index += 1
181 if self.__index < len(self.__testCaseList):
182 if saveEachCase:
183 # save result after each step. that way, in case the application crashes, results wont't be lost
184 self.__fieldValueTestCaseSet.save(self.__filename)
185 self.__setTestCase()
186 else:
187 self.__index = -1
188 self.__running = False
189 self.__fieldValueTestCaseSet.save(self.__filename)
190 self.__statusString = "finished"
191 return True, []
192
193 # --------------------------------------------------------------------------------------------}}}-
194
195 # -- def finish --------------------------------------------------------------------------------{{{-
196
197 def finish(self, verbose=False):
198 if not self.__running:
199 return False
200 if verbose:
201 print("finishing run")
202 self.__index = -1
203 self.__running = False
204 self.__fieldValueTestCaseSet.save(self.__filename)
205 self.__statusString = "finished"
206 return True
207
208 # --------------------------------------------------------------------------------------------}}}-
209
210 # -- def prev --------------------------------------------------------------------------------{{{-
211
212 def prev(self, verbose=False):
213 if not self.__running or self.__index == 0:
214 return False
215 if verbose:
216 print("going to prev test case")
217 self.__index -= 1
218 self.__setTestCase()
219 return True
220
221 # --------------------------------------------------------------------------------------------}}}-
222
223 # -- def goToCaseWithId ----------------------------------------------------------------------{{{-
224
225 def goToCaseWithId(self, id, verbose=False):
226 if not self.__running or id < 0 or id >= self.getNumCases():
227 return False
228 if verbose:
229 print("going to case with id ", id)
230 self.__index = id
231 self.__setTestCase()
232 return True
233
234 # --------------------------------------------------------------------------------------------}}}-
235
236 # -- def __setTestCase -----------------------------------------------------------------------{{{-
237
238 def __setTestCase(self):
239 self.__changeSet.revert()
240 self.__testCase = self.__fieldValueTestCaseSet.get(self.__testCaseList[self.__index])
241 self.__testCase.applyParameterization(self.__changeSet)
242 self.__statusString = "%d/%d (%s)" % (self.getCurrentIndex() + 1, self.getNumCases(), self.getCurrentCaseName())
243
244 # --------------------------------------------------------------------------------------------}}}-
245
246
247# ----------------------------------------------------------------------------------------------}}}-
248
249
250# -- class FieldValueTestCaseSet ---------------------------------------------------------------{{{-
251
265 # -- member variables ------------------------------------------------------------------------{{{-
266
267 __ctx = None
268
269
270 __xmlTree = None
271
272 __xmlRoot = None
273
274
275 __fieldValueTestCaseDict = None
276 # --------------------------------------------------------------------------------------------}}}-
277
278 # -- def __init__ ----------------------------------------------------------------------------{{{-
279
281 def __init__(self, context):
283
284 self.__ctx = context
285
286 self.__xmlRoot = etree.Element("FieldValueTestCaseSet")
287 self.__xmlTree = etree.ElementTree(self.__xmlRoot)
288
289 # --------------------------------------------------------------------------------------------}}}-
290
291 # -- def load --------------------------------------------------------------------------------{{{-
292
297 def load(self, filename):
298 retVal = True
299 if len(self.__fieldValueTestCaseDict) != 0:
300 mevis.MLAB.logError(
301 "There are already field-value test cases in this set. Loading not supported in this case!"
302 )
303 retVal = False
304 # Verify file with given filename exists.
305 elif not os.path.isfile(filename):
306 mevis.MLAB.logError("File %s does not exist." % (filename))
307 retVal = False
308 else:
309 # Load file and try to verify the file's content.
310 xmlTree = etree.parse(filename)
311 xmlRoot = xmlTree.getroot()
312 if not self.__verify(xmlRoot):
313 retVal = False
314 else:
315 self.__xmlTree = xmlTree
316 self.__xmlRoot = xmlRoot
317
318 for node in self.__xmlRoot.findall("FieldValueTestCase"):
319 testCaseName = node.get("name")
320 self.__fieldValueTestCaseDict[testCaseName] = None
321 return retVal
322
323 # --------------------------------------------------------------------------------------------}}}-
324
325 # -- def save --------------------------------------------------------------------------------{{{-
326
327 def save(self, filename):
328 # Update the XML datastructure from the field-value test case objects.
329 for node in self.__xmlRoot.findall("FieldValueTestCase"):
330 name = node.get("name")
331 if self.__fieldValueTestCaseDict[name]:
332 self.__fieldValueTestCaseDict[name].save(node)
333 self.__xmlTree.write(filename)
334
335 # --------------------------------------------------------------------------------------------}}}-
336
337 # -- def getList -----------------------------------------------------------------------------{{{-
338
340 def getList(self):
341 return list(self.__fieldValueTestCaseDict.keys())
342
343 # --------------------------------------------------------------------------------------------}}}-
344
345 # -- def add ---------------------------------------------------------------------------------{{{-
346
351 def add(self, fieldValueTestCase):
352 retVal = True
353 name = fieldValueTestCase.getName()
354 if name in self.__fieldValueTestCaseDict:
355 mevis.MLAB.logError("Test with name %s already known!" % (name))
356 retVal = False
357 else:
358 self.__fieldValueTestCaseDict[name] = fieldValueTestCase
359 self.__xmlRoot.append(fieldValueTestCase.save())
360 return retVal
361
362 # --------------------------------------------------------------------------------------------}}}-
363
364 # -- def copy --------------------------------------------------------------------------------{{{-
365
371 def copy(self, source, destination):
372 retVal = True
373 if not source in self.__fieldValueTestCaseDict:
374 mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (source))
375 retVal = False
376 else:
377 node = self.__getFieldValueTestCaseNode(source)
378 if node is None:
379 retVal = False
380 else:
381 # Save source to be sure to copy the current state. If the source has
382 # not be used after loading there is no FieldValueTestCase object in
383 # the dict and then nothing must be saved.
384 if not self.__fieldValueTestCaseDict[source] is None:
385 self.__fieldValueTestCaseDict[source].save(node)
386 # Add new node to the internal data structures.
387 retVal = self.add(FieldValueTestCase(destination, self.__ctx, node))
388 return retVal
389
390 # --------------------------------------------------------------------------------------------}}}-
391
392 # -- def remove ------------------------------------------------------------------------------{{{-
393
397 def remove(self, name):
398 retVal = True
399 if not name in self.__fieldValueTestCaseDict:
400 mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
401 retVal = False
402 else:
403 node = self.__getFieldValueTestCaseNode(name)
404 if node is None:
405 retVal = False
406 else:
407 self.__xmlRoot.remove(node)
408 del self.__fieldValueTestCaseDict[name]
409 return retVal
410
411 # --------------------------------------------------------------------------------------------}}}-
412
413 # -- def rename ------------------------------------------------------------------------------{{{-
414
421 def rename(self, name, newName):
422 retVal = True
423 if not name in self.__fieldValueTestCaseDict:
424 mevis.MLAB.logError("FieldValueTestCase with name %s not known!" % (name))
425 retVal = False
426 elif newName in self.__fieldValueTestCaseDict:
427 mevis.MLAB.logError("FieldValueTestCase with name %s already exists!" % (newName))
428 retVal = False
429 else:
430 node = self.__getFieldValueTestCaseNode(name)
431 if node is None:
432 # Failed to find the given field-value test case in the XML data structure.
433 retVal = False
434 else:
435 # Set node name in the XML data structure.
436 node.set("name", newName)
437 # If field-value test case is loaded set name there and rename it in
438 # the dictionary with all field-value test cases.
439 fieldValueTestCase = self.__fieldValueTestCaseDict[name]
440 if not fieldValueTestCase is None:
441 fieldValueTestCase.setName(newName)
442 self.__fieldValueTestCaseDict[newName] = fieldValueTestCase
443 del self.__fieldValueTestCaseDict[name]
444 return retVal
445
446 # --------------------------------------------------------------------------------------------}}}-
447
448 # -- def get ---------------------------------------------------------------------------------{{{-
449
453 def get(self, name):
454 fieldValueTestCase = None
455 if name not in self.__fieldValueTestCaseDict:
456 mevis.MLAB.logError("FieldValueTestCase with name %s not found!" % (name))
457 else:
458 # If the field-value test case has not yet been created: do that.
459 if not self.__fieldValueTestCaseDict[name]:
460 node = self.__getFieldValueTestCaseNode(name)
461 if not node is None:
462 self.__fieldValueTestCaseDict[name] = FieldValueTestCase(name, self.__ctx, node)
463 fieldValueTestCase = self.__fieldValueTestCaseDict[name]
464 return fieldValueTestCase
465
466 # --------------------------------------------------------------------------------------------}}}-
467
468 # -- def __verify ----------------------------------------------------------------------------{{{-
469 def __verify(self, xmlRoot):
470 if not xmlRoot.tag == "FieldValueTestCaseSet":
471 return False
472
473 for fieldValueTestCaseNode in xmlRoot:
474 if not fieldValueTestCaseNode.tag == "FieldValueTestCase":
475 mevis.MLAB.logError("Only FieldValueTestCase nodes allowed.")
476 return False
477 settingsNode = fieldValueTestCaseNode.find("Settings")
478 resultsNode = fieldValueTestCaseNode.find("Results")
479 if settingsNode is None or resultsNode is None:
480 mevis.MLAB.logError("Settings and Results node must be defined.")
481 return False
482 if len(fieldValueTestCaseNode) != 2:
483 mevis.MLAB.logError("Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode)))
484 return False
485 flList = [False, False, False]
486 for fieldListNode in settingsNode:
487 if fieldListNode.tag == "FieldValueList":
488 name = fieldListNode.get("name")
489 if name == "Parameterization" and not flList[0]:
490 flList[0] = True
491 elif name == "ExpectedResults" and not flList[1]:
492 flList[1] = True
493 else:
494 mevis.MLAB.logError(
495 "Only FieldValueLists with name Parameterization or ExpectedResults allowed here."
496 )
497 return False
498 elif fieldListNode.tag == "FieldList":
499 name = fieldListNode.get("name")
500 if name == "ResultsToSave" and not flList[2]:
501 flList[2] = True
502 else:
503 mevis.MLAB.logError("Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
504 return False
505 else:
506 mevis.MLAB.logError(
507 "Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag)
508 )
509 return False
510 for child in fieldListNode:
511 if child.tag != "Field":
512 mevis.MLAB.logError("There must only be nodes of type Field.")
513 return False
514 if not (child.get("module") != None and child.get("field") != None and child.get("type") != None):
515 mevis.MLAB.logError("Each field must have information on the module, field and type.")
516 return False
517 if not flList[0] or not flList[1] or not flList[2]:
518 mevis.MLAB.logError(
519 "There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave)."
520 )
521 return False
522 for fieldListNode in resultsNode:
523 pass
524 return True
525
526 # --------------------------------------------------------------------------------------------}}}-
527
528 # -- def __getFieldValueTestCaseNode ---------------------------------------------------------{{{-
529
537 def __getFieldValueTestCaseNode(self, name):
538 for node in self.__xmlRoot.findall("FieldValueTestCase"):
539 if node.get("name") == name:
540 return node
541 mevis.MLAB.logError("FieldValueTestCase %s not found in XML data-structure!" % (name))
542 return None
543
544 # --------------------------------------------------------------------------------------------}}}-
545
546
547# ----------------------------------------------------------------------------------------------}}}-
548
549
550# -- class FieldValueTestCase -----------------------------------------------------------------{{{-
551
558 # -- member variables ------------------------------------------------------------------------{{{-
559
560 __name = None
561
562 __ctx = None
563
564
565 __fieldValueListDict = None
566 # --------------------------------------------------------------------------------------------}}}-
567
568 # -- class FieldListBase ---------------------------------------------------------------------{{{-
569
571 class FieldListBase(list):
572 # -- member variables ----------------------------------------------------------------------{{{-
573
575 _type = None
576 # ------------------------------------------------------------------------------------------}}}-
577
578 # -- def __init__ --------------------------------------------------------------------------{{{-
579
581 def __init__(self, name):
582 self.__name = name
583
584 # ------------------------------------------------------------------------------------------}}}-
585
586 # -- def add -------------------------------------------------------------------------------{{{-
587
593 def add(self, fieldInfo, index=None):
594 retVal = index
595 if not index is None:
596 if index < 0:
597 index = 0
598 if index > len(self):
599 index = len(self)
600 else:
601 index = len(self)
602 return self._add(index, fieldInfo)
603
604 # ------------------------------------------------------------------------------------------}}}-
605
606 # -- def remove ----------------------------------------------------------------------------{{{-
607
610 def remove(self, index):
611 retVal = True
612 if index >= 0 and index < len(self):
613 del self[index]
614 else:
615 retVal = False
616 return retVal
617
618 # ------------------------------------------------------------------------------------------}}}-
619
620 # -- def swap ------------------------------------------------------------------------------{{{-
621
625 def swap(self, indexA, indexB):
626 retVal = True
627 if indexA >= 0 and indexA < len(self) and indexB >= 0 and indexB < len(self) and indexA != indexB:
628 tmp = self[indexA]
629 self[indexA] = self[indexB]
630 self[indexB] = tmp
631 else:
632 retVal = False
633 return retVal
634
635 # ------------------------------------------------------------------------------------------}}}-
636
637 # -- def load ------------------------------------------------------------------------------{{{-
638
640 def load(self, xmlNode):
641 for fieldNode in xmlNode.findall("Field"):
642 self.append(self._loadField(fieldNode))
643
644 # ------------------------------------------------------------------------------------------}}}-
645
646 # -- def save ------------------------------------------------------------------------------{{{-
647
649 def save(self):
650 xmlNode = etree.Element(self._type, name=self.__name)
651 for fieldInfo in self:
652 self._saveField(fieldInfo, xmlNode)
653 return xmlNode
654
655 # ------------------------------------------------------------------------------------------}}}-
656
657 # --------------------------------------------------------------------------------------------}}}-
658
659 # -- class FieldList -------------------------------------------------------------------------{{{-
660
662 # -- def __init__ --------------------------------------------------------------------------{{{-
663
665 def __init__(self, name):
666 self._type_type = "FieldList"
667 FieldValueTestCase.FieldListBase.__init__(self, name)
668
669 # ------------------------------------------------------------------------------------------}}}-
670
671 # -- def update ----------------------------------------------------------------------------{{{-
672
675 def update(self, index, fieldInfo):
676 return
677
678 # ------------------------------------------------------------------------------------------}}}-
679
680 # -- def _add ------------------------------------------------------------------------------{{{-
681
684 def _add(self, index, fieldInfo):
685 self.insert(index, fieldInfo[0:3])
686
687 # ------------------------------------------------------------------------------------------}}}-
688
689 # -- def _loadField ------------------------------------------------------------------------{{{-
690
693 def _loadField(self, xmlNode):
694 return (xmlNode.get("module"), xmlNode.get("field"), xmlNode.get("type"))
695
696 # ------------------------------------------------------------------------------------------}}}-
697
698 # -- def _saveField ------------------------------------------------------------------------{{{-
699
702 def _saveField(self, fieldInfo, xmlNode):
703 etree.SubElement(xmlNode, "Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
704
705 # ------------------------------------------------------------------------------------------}}}-
706
707 # --------------------------------------------------------------------------------------------}}}-
708
709 # -- class FieldValueList --------------------------------------------------------------------{{{-
710
712 # -- def __init__ --------------------------------------------------------------------------{{{-
713 def __init__(self, name):
714 self._type_type = "FieldValueList"
715 FieldValueTestCase.FieldListBase.__init__(self, name)
716
717 # ------------------------------------------------------------------------------------------}}}-
718
719 # -- def update ----------------------------------------------------------------------------{{{-
720
723 def update(self, index, fieldInfo):
724 fieldType = fieldInfo[2]
725 fieldValue = self.__convertValue(fieldInfo[3], fieldType)
726 self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
727
728 # ------------------------------------------------------------------------------------------}}}-
729
730 # -- def _add ------------------------------------------------------------------------------{{{-
731
734 def _add(self, index, fieldInfo):
735 self.insert(index, fieldInfo)
736
737 # ------------------------------------------------------------------------------------------}}}-
738
739 # -- def _loadField ------------------------------------------------------------------------{{{-
740
743 def _loadField(self, xmlNode):
744 fieldType = xmlNode.get("type")
745 fieldValue = self.__convertValue(xmlNode.text, fieldType)
746 return (xmlNode.get("module"), xmlNode.get("field"), fieldType, fieldValue)
747
748 # ------------------------------------------------------------------------------------------}}}-
749
750 # -- def _saveField ------------------------------------------------------------------------{{{-
751
754 def _saveField(self, fieldInfo, xmlNode):
755 if fieldInfo[2] not in ("Trigger"):
756 etree.SubElement(xmlNode, "Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = (
757 str(fieldInfo[3])
758 )
759 else:
760 etree.SubElement(xmlNode, "Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
761
762 # ------------------------------------------------------------------------------------------}}}-
763
764 # -- def __convertValue ----------------------------------------------------------------------{{{-
765
769 def __convertValue(self, value, type):
770 if type == "Integer":
771 value = int(value)
772 elif type in ("Float", "Double"):
773 value = float(value)
774 elif "Vector" in type:
775 value = tuple([float(x) for x in value[1:-1].split(",")])
776 elif type == "Bool":
777 value = value == "True"
778 elif type == "Trigger":
779 value = ""
780 else:
781 if type not in ("Enum", "String"):
782 mevis.MLAB.log("Maybe type %s should be supported?!" % (type))
783 return value
784
785 # --------------------------------------------------------------------------------------------}}}-
786
787 # --------------------------------------------------------------------------------------------}}}-
788
789 # -- def __init__ ----------------------------------------------------------------------------{{{-
790
796 def __init__(self, name, ctx, xmlNode=None):
797 self.__name = name
798 self.__ctx = ctx
799 self.__fieldValueListDict = {
800 "Parameterization": FieldValueTestCase.FieldValueList("Parameterization"),
801 "ExpectedResults": FieldValueTestCase.FieldValueList("ExpectedResults"),
802 "ResultsToSave": FieldValueTestCase.FieldList("ResultsToSave"),
803 "SavedResults": {},
804 }
805
806 # If an XML node is given load it.
807 if xmlNode:
808 self.__load(xmlNode)
809
810 # --------------------------------------------------------------------------------------------}}}-
811
812 # -- def getName -----------------------------------------------------------------------------{{{-
813
815 def getName(self):
816 return self.__name
817
818 # --------------------------------------------------------------------------------------------}}}-
819
820 # -- def setName -----------------------------------------------------------------------------{{{-
821
823 def setName(self, name):
824 self.__name = name
825
826 # --------------------------------------------------------------------------------------------}}}-
827
828 # -- def save --------------------------------------------------------------------------------{{{-
829
833 def save(self, xmlNode=None):
834 # Create an empty XML node if none was specified. Otherwise clean the
835 # existing node and put the current settings in.
836 if not xmlNode:
837 xmlNode = etree.Element("FieldValueTestCase", name=self.__name)
838 settingsNode = etree.SubElement(xmlNode, "Settings")
839 resultsNode = etree.SubElement(xmlNode, "Results")
840 else:
841 settingsNode = xmlNode.find("Settings")
842 resultsNode = xmlNode.find("Results")
843 settingsNode.clear()
844 resultsNode.clear()
845
846 # Save the settings.
847 settingsNode.append(self.__fieldValueListDict["Parameterization"].save())
848 settingsNode.append(self.__fieldValueListDict["ExpectedResults"].save())
849 settingsNode.append(self.__fieldValueListDict["ResultsToSave"].save())
850 for result in self.__fieldValueListDict["SavedResults"]:
851 resultsNode.append(self.__fieldValueListDict["SavedResults"][result].save())
852
853 return xmlNode
854
855 # --------------------------------------------------------------------------------------------}}}-
856
857 # -- def getParameterization -----------------------------------------------------------------{{{-
858
861 return self.__fieldValueListDict["Parameterization"]
862
863 # --------------------------------------------------------------------------------------------}}}-
864
865 # -- def applyParameterization ---------------------------------------------------------------{{{-
866
871 def applyParameterization(self, changeSet, verbose=False):
872 for item in self.__fieldValueListDict["Parameterization"]:
873 fieldName = "%s.%s" % (item[0], item[1])
874 if item[2] in ("Trigger"):
875 self.__ctx.field(fieldName).touch()
876 else:
877 changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
878
879 # --------------------------------------------------------------------------------------------}}}-
880
881 # -- def getExpectedResults ------------------------------------------------------------------{{{-
882
885
887 return self.__fieldValueListDict["ExpectedResults"]
888
889 # --------------------------------------------------------------------------------------------}}}-
890
891 # -- def verifyExpectedResults ---------------------------------------------------------------{{{-
892
897 def verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False):
898 retValue = True
899 for item in self.__fieldValueListDict["ExpectedResults"]:
900 fieldName = "%s.%s" % (item[0], item[1])
901 field = self.__ctx.field(fieldName)
902 if not field:
903 if verbose:
904 mevis.MLAB.logError("Unknown field: %s." % (fieldName))
905 else:
906 match = False
907 if field.type in ("Float", "Double"):
908 # TODO Use configurable epsilon.
909 match = abs(field.value - item[3]) < 0.0001
910 else:
911 match = field.value == item[3]
912 if not match:
913 failedFieldList.append(fieldName)
914 if verbose:
915 mevis.MLAB.logError(
916 "Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3])
917 )
918 if not findAll:
919 return False
920 retValue = False
921 return retValue
922
923 # --------------------------------------------------------------------------------------------}}}-
924
925 # -- def getResultsToSave --------------------------------------------------------------------{{{-
926
929 return self.__fieldValueListDict["ResultsToSave"]
930
931 # --------------------------------------------------------------------------------------------}}}-
932
933 # -- def saveResults -------------------------------------------------------------------------{{{-
934
939 def saveResults(self, name, overwrite=False):
940 retVal = True
941 if not overwrite and name in self.__fieldValueListDict["SavedResults"]:
942 mevis.MLAB.logError("Result with name %s available already." % (name))
943 retVal = False
944 else:
945 resList = self.__fieldValueListDict["SavedResults"][name] = FieldValueTestCase.FieldValueList(name)
946 for item in self.__fieldValueListDict["ResultsToSave"]:
947 fldName = "%s.%s" % (item[0], item[1])
948 if item[2] == "Trigger":
949 self.__ctx.field(fldName).touch()
950 fldValue = ""
951 else:
952 fldValue = self.__ctx.field(fldName).value
953 resList.append(tuple((item[0], item[1], item[2], fldValue)))
954 return retVal
955
956 # --------------------------------------------------------------------------------------------}}}-
957
958 # -- def getSavedResultList ------------------------------------------------------------------{{{-
959
962 return sorted(self.__fieldValueListDict["SavedResults"].keys())
963
964 # --------------------------------------------------------------------------------------------}}}-
965
966 # -- def addSavedResultList ------------------------------------------------------------------{{{-
967
969 def addSavedResultList(self, name, resultList):
970 retVal = True
971 if name in self.__fieldValueListDict["SavedResults"]:
972 mevis.MLAB.logError("Result with name %s available already." % (name))
973 retVal = False
974 else:
976 resToSaveList = []
977 for item in self.__fieldValueListDict["ResultsToSave"]:
978 resToSaveList.append("%s.%s" % (item[0], item[1]))
979 for item in resultList:
980 field = "%s.%s" % (item[0], item[1])
981 if not field in resToSaveList:
982 mevis.MLAB.logError("Field %s not in list of results to save. Ignoring it." % (field))
983 else:
984 resList.add(item)
985 self.__fieldValueListDict["SavedResults"][name] = resList
986 return retVal
987
988 # --------------------------------------------------------------------------------------------}}}-
989
990 # -- def getSavedResult ----------------------------------------------------------------------{{{-
991
993 def getSavedResult(self, name):
994 if name not in self.__fieldValueListDict["SavedResults"]:
995 mevis.MLAB.logError("No result with name %s available." % (name))
996 return None
997 return self.__fieldValueListDict["SavedResults"][name]
998
999 # --------------------------------------------------------------------------------------------}}}-
1000
1001 # -- def __load ------------------------------------------------------------------------------{{{-
1002
1004 def __load(self, xmlNode):
1005 node = xmlNode.find("Settings")
1006 for fieldValueListNode in node.findall("FieldValueList"):
1007 self.__fieldValueListDict[fieldValueListNode.get("name")].load(fieldValueListNode)
1008 for fieldListNode in node.findall("FieldList"):
1009 self.__fieldValueListDict[fieldListNode.get("name")].load(fieldListNode)
1010 for resultNode in xmlNode.findall("Results/FieldValueList"):
1011 name = resultNode.get("name")
1012 self.__fieldValueListDict["SavedResults"].setdefault(name, FieldValueTestCase.FieldValueList(name))
1013 self.__fieldValueListDict["SavedResults"][name].load(resultNode)
1014
1015 # --------------------------------------------------------------------------------------------}}}-
1016
1017
1018# ----------------------------------------------------------------------------------------------}}}-
1019
1020# //# MeVis signature v1
1021# //# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBANEfsmYse2e1dRhkQ9AQbreCq9uxwzWLoGom13MNYmyfwoJqQOEXljLFAgw2eEjaT12G4CdqKWhRxh9ANP6n7GMCARE=:VI/mB8bT4u+mRtf/ru8yUQi8BzpaS3UeL2x62YxsUYnVqCWuLrVNLiukIIjnJMKQXlc8ezmgOIcVAV7pgvgKpQ==
1022# //# owner: MeVis
1023# //# date: 2010-11-26T09:00:38
1024# //# hash: rMwMlcYQJQHOxPbybrFYtdjiuX+LFIHs/PgH3XbmXzWLw97M+Pv+FGNwyD0VMD9vLOsMHIIyRwUumHpGFR6koQ==
1025# //# MeVis end
Class to handle field changes and make them revertable.
Definition ChangeSet.py:29
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)
Use the given field-value list to verify the given context's field values.
__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.