# Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
# **InsertLicense** code author="Lennart Tautz"
from __future__ import absolute_import
import os
from TestSupport import Base
from NetworkScripting.datatofieldloader import DataToFieldLoader
[docs]class TestCenterTestCaseBase(object):
"""Base class for all TestCenter tests, adding setup and tearDown."""
[docs] def setUp(self):
"""Called before a test function is executed. Do not forget to first call this superclass implementation
when overwriting in a derived class.
"""
self._ctx = Base.getTestCaseContext()
[docs] def tearDown(self):
"""Called after a test function is fully executed. When overwriting in a derived class, do not forget
to call this superclass implementation *at the end* of your custom implementation.
"""
pass
[docs]class TestCenterTestCase(TestCenterTestCaseBase):
"""Base class for TestCenter tests, adding extending TestCenterTestCaseBase with convenience methods implemented
via dynamic module creation.
"""
[docs] def setUp(self):
"""Called before a test function is executed. Do not forget to first call this superclass implementation
when overwriting in a derived class.
"""
super().setUp()
self.__addedModules = []
self.__loader = DataToFieldLoader(self._ctx)
[docs] def tearDown(self):
"""Called after a test function is fully executed. When overwriting in a derived class, do not forget
to call this superclass implementation *at the end* of your custom implementation.
"""
self.__removeAddedModules()
self.__loader.clear()
super().tearDown()
def __removeAddedModules(self):
for mod in self.__addedModules:
mod.remove()
self.__addedModules = []
[docs] def _loadImageFromDataTo(self, subPath, targetField):
"""Loads an image file from a relative path in test's local 'Data' directory
and connects it to given 'Image' typed field.
"""
self._loadImageTo(self.__composeFullPathFromData(subPath), targetField)
def __composeFullPathFromData(self, subPath):
return os.path.join(Base.getDataDirectory(), subPath)
[docs] def _loadImageTo(self, fullPath, targetField):
"""Loads an image file from given path and connects it to given 'Image' typed field.
"""
self.__loader.loadImageTo(fullPath, targetField)
[docs] def _loadAndConnectImageFromDataDirectoryTo(self, filename, targetField):
"""Just redirects to _loadImageFromDataTo().
"""
self._loadImageFromDataTo(filename, targetField)
[docs] def _loadAndConnectImageTo(self, fullPath, targetField):
"""Just redirects to _loadImageTo().
"""
self._loadImageTo(fullPath, targetField)
[docs] def _addModule(self, name):
"""Adds a new module temporary to test network and returns corresponding MLABModule object.
The module will automatically be removed in tearDown().
"""
module = self._ctx.addModule(name)
self.__assertAddedModule(module, name)
self.__addedModules.append(module)
return module
def __assertAddedModule(self, module, name):
if module is None:
raise ValueError('Adding module \'{}\' failed. See log for details.'.format(name))
[docs] def _loadMLBaseFromDataTo(self, subPath, targetField):
"""Loads an MLBase file from a relative path in test's local 'Data' directory
and connects it to given 'MLBase' typed field.
"""
self._loadMLBaseTo(self.__composeFullPathFromData(subPath), targetField)
[docs] def _loadMLBaseTo(self, fullPath, targetField):
"""Loads an MLBase file from given path and connects it to given 'MLBase' typed field.
"""
self.__loader.loadMLBaseTo(fullPath, targetField)
[docs] def _loadCSOListFromDataTo(self, subPath, targetField):
"""Loads a CSOList file from a relative path in test's local 'Data' directory
and connects it to given 'MLBase' typed field.
"""
self._loadCSOListTo(self.__composeFullPathFromData(subPath), targetField)
[docs] def _loadCSOListTo(self, fullPath, targetField):
"""Loads a CSOList file from given path and connects it to given 'MLBase' typed field.
"""
self.__loader.loadCSOListTo(fullPath, targetField)
[docs] def _loadWEMFromDataTo(self, subPath, targetField):
"""Loads a WEM file from a relative path in test's local 'Data' directory
and connects it to given 'MLBase' typed field.
"""
self._loadWEMTo(self.__composeFullPathFromData(subPath), targetField)
[docs] def _loadWEMTo(self, fullPath, targetField):
"""Loads a WEM file from given path and connects it to given 'MLBase' typed field.
"""
self.__loader.loadWEMTo(fullPath, targetField)
[docs] def _compareImageValuesWithFileFromData(self, field, subPath, epsilon):
"""Compares image values of 'Image' typed field with a file
located in test's local 'Data' directory using given epsilon.
"""
path = self.__composeFullPathFromData(subPath)
self._compareImageValuesWithFile(field, path, epsilon)
[docs] def _compareImageValuesWithFile(self, field, fullPath, epsilon):
"""Compares image values of 'Image' typed field with a file using given epsilon.
"""
imageCompare = self.__addImageCompareToCompareImageValues(epsilon)
imageCompare.field('input0').connectFrom(field)
self._loadImageTo(fullPath, imageCompare.field('input1'))
self.__executeImageComparison(imageCompare)
def __executeImageComparison(self, imageCompare):
imageCompare.field('compare').touch()
if not imageCompare.field('testPassed').value:
status = imageCompare.field('status').value
msg = 'Image values comparison failed with reason: {}'.format(status)
raise ValueError(msg)
def __addImageCompareToCompareImageValues(self, epsilon):
module = self._addModule('ImageCompare')
module.field('updateMode').value = 'AutoClear'
module.field('voxValEpsilon').value = epsilon
module.field('handleOpenInputsAsErr').value = 'AnyOpenInputIsError'
module.field('checkDataType').value = False
module.field('checkImgExt').value = True
module.field('checkPageExt').value = False
module.field('checkMinVoxVal').value = False
module.field('checkMaxVoxVal').value = False
module.field('checkUpToDate').value = True
module.field('checkVoxelSize').value = True
module.field('checkWorldMatrix').value = False
module.field('checkIsWorldMatrixValid').value = True
module.field('checkCDimInfos').value = False
module.field('checkTDimInfos').value = False
module.field('checkUDimInfos').value = False
module.field('checkImgPropExts').value = False
module.field('checkVoxelData').value = True
module.field('compareAllVoxels').value = False
return module