import unittest class AvizoTest(unittest.TestCase): """This class is the base class of all avizo unit test. """ def assert_generic_property(self, type, instance, property_name, values, expected_values): """assert_generic_property(type, instance, property_name, values, expected_values) This method could be used to test validity of a property. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the property to test. values : list different values that will be set to the property. expected_values : list different values that will be checked by reading the property. Notes ----- This function will firstly test existence of the property. Then for each `item` of `values` the property will be set to `item` and the property will be read and an assert is performed to know if the value expected on the property is the one expected in `expected_values`. """ self.assertEqual( len(values), len(expected_values), "Test badly formatted. Not the same qty of values and expectedValues." ) self.assertTrue( isinstance( property_name, basestring ), "Test badly formatted. property_name should be a string." ) self.assertTrue( isinstance(instance, type), "Test badly formatted. instance provided is not an instance of " + type.__name__ ) for i in range(values.__len__()): instance.__setattr__( property_name, values[i] ) self.assertEqual( instance.__getattribute__( property_name ), expected_values[i], "Property test " + type.__name__ + "." + property_name + " failed:\n" + "Setting property to value : " + values[i].__str__() + "\n" + "getting : " + instance.__getattribute__( property_name ).__str__() + "\n" + "expecting : " + expected_values[i].__str__() ) def assert_property(self, type, instance, property_name, values): """assert_property(type, instance, property_name, values) This method could be used to test validity of a property. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the property to test. values : list different values that will be set to the property. Notes ----- This function will firstly test existence of the property. Then for each `item` of `values` the property will be set to `item` and the property will be read and an assert is performed to know if the value expected on the property is `item`. """ self.assert_generic_property(type, instance, property_name, values, values) def assert_mutable_sequence_generic_property(self, type, instance, property_name, values, expected_values): """assert_mutable_sequence_generic_property(type, instance, property_name, values, expected_values) This method could be used to test validity of a property which is a mutable sequence. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the mutable sequence property to test. values : list different values that will be set to the property. (a list of list) expected_values : list different values that will be checked by reading the property. (a list of list) Notes ----- This function will firstly test existence of the property. Then for each `item` of `values` the property will be set to `item` and the property will be read and an assert is performed to know if the value expected on the property is the one expected in `expected_values`. """ self.assertEqual( len(values), len(expected_values), "Test badly formatted. Not the same qty of values and expectedValues." ) self.assertTrue( isinstance( property_name, basestring ), "Test badly formatted. property_name should be a string." ) self.assertTrue( isinstance(instance, type), "Test badly formatted. instance provided is not an instance of " + type.__name__ ) for i in range(values.__len__()): instance.__setattr__( property_name, values[i] ) ret_val = instance.__getattribute__( property_name ) self.assertEqual(len(ret_val), len(expected_values[i])) for k in range(len(ret_val)): self.assertEqual( ret_val[k], expected_values[i][k], "Property test " + type.__name__ + "." + property_name + " failed:\n" + "Setting property to value : " + values[i].__str__() + "\n" + "getting : " + ret_val.__str__() + "\n" + "expecting : " + expected_values[i].__str__() ) def assert_mutable_sequence_property(self, type, instance, property_name, values): """assert_mutable_sequence_property(type, instance, property_name, values) This method could be used to test validity of a property which is a mutable sequence. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the mutable sequence property to test. values : list different values that will be set to the property. (a list of list) Notes ----- This function will firstly test existence of the property. Then for each `item` of `values` the property will be set to `item` and the property will be read and an assert is performed to know if the value expected on the property is the one expected in `expected_values`. """ self.assert_mutable_sequence_generic_property(type, instance, property_name, values, values) def assert_boolean_property(self, type, instance, property_name): """assert_boolean_property(type, instance, property_name) This method could be used to test validity of a property. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the property to test. Notes ----- This function will firstly test existence of the property. Then the property will be set to `True` and assert than reading the property returns `True`. Then the property will be set to `False` and assert than reading the property returns `False`. """ self.assert_property(type, instance, property_name, [True, False]) def assert_range_generic_property(self, type, instance, property_name, range_values, expected_range_values): """assert_range_property(type, instance, property_name, range_values) This method could be used to test validity of a range property. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the property to test. range_values : list of pair of values that will be set range_values : list of pair of values that will be compared to the range that is read. Notes ----- This function will firstly test existence of the property. Then the property will be set to each value of value_range and assert than reading the property returns the value that has been set. Setting the value is performed in two different fashion: >>> instance.property_name = range_values[i] and >>> instance.property_name[0] = range_values[i][0] >>> instance.property_name[1] = range_values[i][1] """ self.assertTrue( isinstance( property_name, basestring ), "Test badly formatted. property_name should be a string." ) self.assertTrue( isinstance(instance, type), "Test badly formatted. instance provided is not an instance of " + type.__name__ ) self.assertEqual(len(range_values), len(expected_range_values), msg="Test badly formatted. Not the same amount of ranges and expected ranges.") # test the RangeModifier interface. for i in range(len(range_values)): srange = range_values[i] expected_range = expected_range_values[i] instance.__getattribute__( property_name )[0] = srange[0] instance.__getattribute__( property_name )[1] = srange[1] self.assertAlmostEqual( instance.__getattribute__( property_name )[0], expected_range[0], msg= "Property test using __get__().__setitem__() " + type.__name__ + "." + property_name + " failed:\n" + "Setting property[0] to value : " + srange[0].__str__() + "\n" + "getting : " + instance.__getattribute__( property_name )[0].__str__() + "\n" + "expecting : " + expected_range[0].__str__() ) self.assertAlmostEqual( instance.__getattribute__( property_name )[1], expected_range[1], msg= "Property test using __get__().__setitem__() " + type.__name__ + "." + property_name + " failed:\n" + "Setting property[1] to value : " + srange[1].__str__() + "\n" + "getting : " + instance.__getattribute__( property_name )[1].__str__() + "\n" + "expecting : " + expected_range[1].__str__() ) # test the property setter. for i in range(len(range_values)): srange = range_values[i] expected_range = expected_range_values[i] instance.__setattr__(property_name, srange) self.assertAlmostEqual( instance.__getattribute__( property_name )[0], expected_range[0], msg= "Property test using __set__() " + type.__name__ + "." + property_name + " failed:\n" + "Setting property[0] to value : " + srange[0].__str__() + "\n" + "getting : " + instance.__getattribute__( property_name )[0].__str__() + "\n" + "expecting : " + expected_range[0].__str__() ) self.assertAlmostEqual( instance.__getattribute__( property_name )[1], expected_range[1], msg= "Property test using __set__() " + type.__name__ + "." + property_name + " failed:\n" + "Setting property[1] to value : " + srange[1].__str__() + "\n" + "getting : " + instance.__getattribute__( property_name )[1].__str__() + "\n" + "expecting : " + expected_range[1].__str__() ) def assert_range_property(self, type, instance, property_name, range_values): """assert_range_property(type, instance, property_name, range_values) This method could be used to test validity of a range property. Parameters ---------- type : python class class that owns the property instance : `type` instance of `type` on which property check must be performed. property_name : string name of the property to test. range_values : list of pair of values that will be set range_values : list of pair of values that will be compared to the range that is read. Notes ----- This function will firstly test existence of the property. Then the property will be set to each value of value_range and assert than reading the property returns the value that has been set. Setting the value is performed in two different fashion: >>> instance.property_name = range_values[i] and >>> instance.property_name[0] = range_values[i][0] >>> instance.property_name[1] = range_values[i][1] """ self.assert_range_generic_property(type, instance, property_name, range_values, range_values) def assert_class_has_docstrings(self, SomeClass): """This method assert that the class has a __doc__ arguments and that all of its member that are not prefixed by an underscore have a __doc__ too. Parameters ---------- SomeClass : python class Class on which docstrings presence must be checked. """ self.assertNotEqual ( SomeClass.__doc__, None, "Testing presence of docstrings for class " + SomeClass.__name__) for attribute in dir(SomeClass): # Skip attribute that are "protected" and "private" if attribute[0] != "_": self.assertNotEqual( getattr(SomeClass, attribute).__doc__, None, "Testing presence of docstrings of " + SomeClass.__name__ + "." + attribute ) def assert_tuple_almost_equal(self, first, second, places=7): """This method tests that all elements of `first`and `second` tuples are approximately equal by computing the difference element by element and rounding to the given decimal place (default 7) and comparing to zero. Parameters ---------- first: tuple of float or double elements First tuple to compare. second: tuple of float or double elements Second tuple to compare. """ self.assertEqual(len(first), len(second)) for i in range(len(first)): self.assertAlmostEqual(first[i], second[i], places)