TestCenter Reference
Fields.py
Go to the documentation of this file.
1 #
2 # Copyright 2009, MeVis Medical Solutions AG
3 #
4 # The user may use this file in accordance with the license agreement provided with
5 # the Software or, alternatively, in accordance with the terms contained in a
6 # written agreement between the user and MeVis Medical Solutions AG.
7 #
8 # For further information use the contact form at https://www.mevislab.de/contact
9 #
10 
11 
13 
14 # -- system imports ----------------------------------------------------------------------------{{{-
15 import os
16 import sys
17 import traceback
18 
20 from TestSupport.ChangeSet import ChangeSet
21 # ----------------------------------------------------------------------------------------------}}}-
22 
23 # -- local imports -----------------------------------------------------------------------------{{{-
24 import mevis
25 
26 from . import Base
27 from . import Logging
28 from TestSupport.TestHelper import TestHelper
29 # ----------------------------------------------------------------------------------------------}}}-
30 
31 # -- def touch ---------------------------------------------------------------------------------{{{-
32 
33 def touch (fieldName, verbose=True):
34  TestHelper.getInstance().getChangeSet().setFieldValue(fieldName, None, verbose=verbose)
35 # ----------------------------------------------------------------------------------------------}}}-
36 
37 # -- def setValues -----------------------------------------------------------------------------{{{-
38 
43 def setValues (fieldValueDict, verbose=True):
44  changeSet = TestHelper.getInstance().getChangeSet()
45  for key in fieldValueDict:
46  changeSet.setFieldValue(key, fieldValueDict[key], verbose=verbose)
47 # --------------------------------------------------------------------------------------------}}}-
48 
49 # -- def varyValues --------------------------------------------------------------------------{{{-
50 
58 def varyValues (function, funcParams, fieldDict, parentChangeSet=None, verbose=True):
59  if not parentChangeSet:
60  parentChangeSet = TestHelper.getInstance().getChangeSet()
61 
62  # No more fields to set, call given function.
63  if len(fieldDict) == 0:
64  function(*funcParams)
65  else:
66  # Create change set for local changes.
67  Base.pushChangeSet()
68 
69  fieldName, fieldValues = fieldDict.popitem()
70  field = Base.getTestCaseContext().field(fieldName)
71  # If list of field values is empty use available.
72  if fieldValues == []:
73  # TODO This is only valid for enums. Should find good handling for other
74  # data types or exclude using [] for those.
75  fieldValues = list(field.items())
76 
77  changeSet = TestHelper().getInstance().getChangeSet()
78 
79  # For each value in the list call the sub dictionary.
80  for fieldValue in fieldValues:
81  changeSet.setFieldValue(fieldName, fieldValue, verbose=verbose)
82  varyValues(function, funcParams, fieldDict, changeSet, verbose)
83 
84  Base.popChangeSet()
85 
86  fieldDict[fieldName] = fieldValues
87 # ----------------------------------------------------------------------------------------------}}}-
88 
89 # -- def getValue ------------------------------------------------------------------------------{{{-
90 
93 def getValue (fieldName):
94  field = Base.getTestCaseContext().field(fieldName)
95  if not field:
96  raise Exception("Field (%s) not found in current test's context!" % (fieldName))
97  if field.type in ('String', 'Enum'):
98  return field.stringValue()
99  elif field.type == 'Bool':
100  return field.boolValue()
101  elif field.type == 'Integer':
102  return field.intValue()
103  elif field.type == 'Float':
104  return field.floatValue()
105  elif field.type == 'Double':
106  return field.doubleValue()
107  elif 'Vector6' in field.type:
108  return field.stringValue()
109  elif field.type in ('Vector2', 'Vector3', 'Vector4', 'Plane', 'Rotation'):
110  return field.vectorValue()
111  elif field.type in ('Matrix'):
112  return field.matrixValue()
113  elif field.type in ('Color'):
114  return field.colorValue()
115  elif field.type == 'Image':
116  return field.image()
117  elif 'MLBase' in field.type:
118  return field.object()
119  else:
120  return field.value
121 # ----------------------------------------------------------------------------------------------}}}-
122 
123 # -- def setValue ------------------------------------------------------------------------------{{{-
124 
130 def setValue (fieldName, fieldValue, verbose=True):
131  TestHelper.getInstance().getChangeSet().setFieldValue(fieldName, fieldValue, verbose=verbose)
132 # ----------------------------------------------------------------------------------------------}}}-
133 
134 # -- def runFieldValueTestCase -----------------------------------------------------------------{{{-
135 
142 def runFieldValueTestCase (fieldValueTestCaseSet, testCaseName, verbose=True):
143  testCase = fieldValueTestCaseSet.get(testCaseName)
144  testCase.applyParameterization(TestHelper.getInstance().getChangeSet(), verbose=verbose)
145  if not testCase.verifyExpectedResults(verbose=verbose):
146  Logging.error("Failed to verify expected results.", depth=1)
147 # ----------------------------------------------------------------------------------------------}}}-
148 
Singleton for important helper facilities for the different tests.
Definition: TestHelper.py:271
def setFieldValue(field, fieldValue)
Definition: ChangeSet.py:155
def touch(fieldName, verbose=True)
Touch the given field.
Definition: Fields.py:33
def setValue(fieldName, fieldValue, verbose=True)
Set value of the given field.
Definition: Fields.py:130
def runFieldValueTestCase(fieldValueTestCaseSet, testCaseName, verbose=True)
Run a field-value test case.
Definition: Fields.py:142
def varyValues(function, funcParams, fieldDict, parentChangeSet=None, verbose=True)
Set field values and for each combination call the given function with the given parameters.
Definition: Fields.py:58
def getValue(fieldName)
Get value of the given field.
Definition: Fields.py:93
def setValues(fieldValueDict, verbose=True)
Set field values to the given values.
Definition: Fields.py:43