From 0ac0235a3f32ba008db691c11431a55106bbba9c Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Mon, 15 Apr 2013 02:07:54 -0500 Subject: [PATCH] tests/test_AnalyzerResult.py: add more tests --- tests/test_AnalyzerResult.py | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/test_AnalyzerResult.py b/tests/test_AnalyzerResult.py index de06f89..94f953d 100755 --- a/tests/test_AnalyzerResult.py +++ b/tests/test_AnalyzerResult.py @@ -2,6 +2,8 @@ from unit_timeside import * from timeside.analyzer.core import * +from numpy import ones, array +from math import pi verbose = 0 @@ -35,9 +37,93 @@ class TestAnalyzerResult(TestCase): "list of lists result" self.result.value = [[0,1], [0,1,2]] + def testOnNumpyVectorOfFloat(self): + "numpy vector of float" + self.result.value = ones(2, dtype = 'float') * pi + + def testOnNumpy2DArrayOfFloat64(self): + "numpy 2d array of float64" + self.result.value = ones([2,3], dtype = 'float64') * pi + + def testOnNumpy3DArrayOfInt32(self): + "numpy 3d array of int32" + self.result.value = ones([2,3,2], dtype = 'int32') * pi + + def testOnNumpyArrayOfStrings(self): + "numpy array of strings" + self.result.value = array(['hello', 'hola']) + + def testOnEmptyList(self): + "empty list" + self.result.value = [] + + def testOnNone(self): + "None" + self.result.value = None + + def testOnUnicode(self): + "None" + self.result.value = None + def tearDown(self): pass +good_numpy_data_types = [ + 'float64', + 'float32', + 'float16', + 'int64', + 'int16', + 'int32', + 'int8', + 'uint16', + 'uint32', + 'uint64', + 'uint8', +] + +bad_numpy_data_types = [ + # not understood by json or yaml + 'float128', + # complex can not be serialized in json + 'complex256', + 'complex128', + 'complex64', + # ? + 'datetime64', + 'timedelta64', + ] + +def create_good_method_func (numpy_data_type): + def method(self): + "numpy %s" % numpy_data_type + import numpy + self.result.value = getattr(numpy, numpy_data_type)(pi) + return method + +def create_bad_method_func (numpy_data_type): + def method(self): + "numpy %s" % numpy_data_type + import numpy + try: + value = getattr(numpy, numpy_data_type)(pi) + except ValueError: + value = getattr(numpy, numpy_data_type)() + self.assertRaises(TypeError, self.result.__setattr__, 'value', value) + return method + +for numpy_data_type in good_numpy_data_types: + test_method = create_good_method_func (numpy_data_type) + test_method.__name__ = 'testOnNumpy_%s' % numpy_data_type + test_method.__doc__ = 'groks a numpy %s' % numpy_data_type + setattr (TestAnalyzerResult, test_method.__name__, test_method) + +for numpy_data_type in bad_numpy_data_types: + test_method = create_bad_method_func (numpy_data_type) + test_method.__name__ = 'testOnNumpy_%s' % numpy_data_type + test_method.__doc__ = 'gasps on numpy %s' % numpy_data_type + setattr (TestAnalyzerResult, test_method.__name__, test_method) + class TestAnalyzerResultNumpy(TestAnalyzerResult): """ test AnalyzerResult numpy serialize """ -- 2.39.5