def interval_validity_test(self): """Test if BaseMethod handles parameter validity correctly.""" parameters = ["param1", "param2", "param3", "param4"] b = BaseMethod(parameters) # overwrite parameter validity dictionary for testing b._parameterIntervals = { "param1": [0.0, 1.0, False, False], "param2": [0.0, 1.0, False, True], "param3": [0.0, 1.0, True, False], "param4": [0.0, 1.0, True, True] } # definetely invalid parameters for value in [-1.5, 3.2]: for parameter in parameters: if b._in_valid_interval(parameter, value): assert False # pragma: no cover # definetly valid parameters for value in [0.3, 0.42]: for parameter in parameters: if not b._in_valid_interval(parameter, value): assert False # pragma: no cover
def value_error_message_test(self): """Test the value error message.""" parameters = ["param1", "param2", "param3", "param4"] b = BaseMethod(parameters) # overwrite parameter validity dictionary for testing b._parameterIntervals = { "param1": [0.0, 1.0, False, False], "param2": [0.0, 1.0, False, True], "param3": [0.0, 1.0, True, False], "param4": [0.0, 1.0, True, True] } # Unknown parameters should return no message if None != b._get_value_error_message_for_invalid_prarameter( "unknown", 0.0): assert False # pragma: no cover # Known parameters should return a message for parameter in parameters: if not isinstance( b._get_value_error_message_for_invalid_prarameter( parameter, 0.4), basestring): assert False # pragma: no cover
def parameter_set_test(self): """Test if the parameters of a method are set correctly.""" b = BaseMethod(["param1", "param2"]) b.set_parameter("param1", 1) b.set_parameter("param2", 2) b.set_parameter("param1", 1) if not len(b._parameters) == 2: raise AssertionError
def initialization_test(self): """Test BaseMethod initialization.""" hasToBeSorted = random.choice([True, False]) hasToBeNormalized = random.choice([True, False]) b = BaseMethod(["param1", "param2"], hasToBeSorted=hasToBeSorted, hasToBeNormalized=hasToBeNormalized) if not b.has_to_be_sorted() == hasToBeSorted: raise AssertionError if not b.has_to_be_normalized() == hasToBeNormalized: raise AssertionError
def required_parameter_test(self): """Test for required parameters.""" parameters = ["param1", "param2"] b = BaseMethod(parameters) requiredParameters = b.get_required_parameters() for parameter in parameters: if not parameter in requiredParameters: raise AssertionError # pragma: no cover assert len(parameters) == len(requiredParameters)
def execute_not_implemented_exception_test(self): """Test the correct interface of BaseMethod.""" b = BaseMethod(["param1", "param2"]) data = [[0.0, 0.0], [1, 0.1], [2, 0.2], [3, 0.3], [4, 0.4]] ts = TimeSeries.from_twodim_list(data) ts.normalize("second") try: b.execute(ts) except NotImplementedError: pass else: assert False # pragma: no cover
def method_completition_test(self): """Test if methods detect their executable state correctly.""" b = BaseMethod(["param1", "param2"]) if b.can_be_executed(): raise AssertionError b.set_parameter("param1", 1) if b.can_be_executed(): raise AssertionError b.set_parameter("param2", 2) if not b.can_be_executed(): raise AssertionError
def method_test(self): """Test if TimeSeries apply branches work correctly. This is mainly to increase code coverage.""" mOne = BaseMethod([], hasToBeSorted=True, hasToBeNormalized=True) mTwo = BaseMethod([], hasToBeSorted=False, hasToBeNormalized=True) mThree = BaseMethod([], hasToBeSorted=True, hasToBeNormalized=False) mFour = BaseMethod([], hasToBeSorted=False, hasToBeNormalized=False) ts = TimeSeries(isNormalized=True) ts.add_entry(0.0, 0.0) ts.add_entry(0.1, 0.1) ts.add_entry(0.2, 0.2) ts.add_entry(0.3, 0.3) ts.add_entry(0.4, 0.4) try: ts.apply(mOne) except NotImplementedError: pass else: assert False # pragma: no cover try: ts.apply(mTwo) except NotImplementedError: pass else: assert False # pragma: no cover try: ts.apply(mThree) except NotImplementedError: pass else: assert False # pragma: no cover try: ts.apply(mFour) except NotImplementedError: pass else: assert False # pragma: no cover
def normalized_method_requirement_test(self): """Test for StandardError.""" def nothing(self): return data = [[0.0, 0.0], [1.1, 1.0], [2.0, 2.0], [5.0, 5.0]] ts = TimeSeries.from_twodim_list(data) mOne = BaseMethod([], hasToBeSorted=True, hasToBeNormalized=True) mOne.execute = nothing try: ts.apply(mOne) except StandardError: pass else: assert False # pragma: no cover ts.normalize("second") ts.apply(mOne)
def get_interval_test(self): """Test if correct intervals are returned.""" parameters = ["param1", "param2", "param3", "param4"] b = BaseMethod(parameters) # overwrite parameter validity dictionary for testing parameterIntervals = { "param1": [0.0, 1.0, False, False], "param2": [0.0, 1.0, False, True], "param3": [0.0, 1.0, True, False], "param4": [0.0, 1.0, True, True] } b._parameterIntervals = parameterIntervals for parameter in parameters: i = b.get_interval(parameter) if not i == parameterIntervals[parameter]: raise AssertionError # pragma: no cover assert None == b.get_interval("unknown")
def value_error_message_test(self): """Test the value error message.""" parameters = ["param1", "param2", "param3", "param4"] b = BaseMethod(parameters) # overwrite parameter validity dictionary for testing b._parameterIntervals = { "param1": [0.0, 1.0, False, False], "param2": [0.0, 1.0, False, True], "param3": [0.0, 1.0, True, False], "param4": [0.0, 1.0, True, True] } # Unknown parameters should return no message if None != b._get_value_error_message_for_invalid_prarameter("unknown", 0.0): assert False # pragma: no cover # Known parameters should return a message for parameter in parameters: if not isinstance(b._get_value_error_message_for_invalid_prarameter(parameter, 0.4), basestring): assert False # pragma: no cover
def optimize_value_error_test(self): """Test the optimize call.""" bom = BaseOptimizationMethod(BaseErrorMeasure, precision=-3) bm = BaseMethod() bom.optimize(TimeSeries(), [bm]) try: bom.optimize(TimeSeries(), []) except ValueError: pass else: assert False # pragma: no cover
def parameter_get_test(self): """Test the parameter set function.""" b = BaseMethod() b.set_parameter("param1", 42.23) param1 = b.get_parameter("param1") assert param1 == 42.23 try: b.get_parameter("param2") except KeyError: pass else: assert False # pragma: no cover