]> git.parisson.com Git - timeside.git/commitdiff
test(analyzers): add a test for running all analyzers with default parameters
authorThomas Fillon <thomas@parisson.com>
Thu, 14 Aug 2014 11:01:39 +0000 (13:01 +0200)
committerThomas Fillon <thomas@parisson.com>
Thu, 14 Aug 2014 11:01:39 +0000 (13:01 +0200)
tests/test_analyzers_default.py [new file with mode: 0644]

diff --git a/tests/test_analyzers_default.py b/tests/test_analyzers_default.py
new file mode 100644 (file)
index 0000000..98455cf
--- /dev/null
@@ -0,0 +1,68 @@
+#! /usr/bin/env python
+
+# Author : Thomas Fillon <thomas@parisson.com>
+
+
+from unit_timeside import unittest, TestRunner
+import timeside
+from timeside.decoder.file import FileDecoder
+import numpy as np
+import os
+
+
+class TestAnalyzers_with_default(unittest.TestCase):
+    """Test analyzer with default parameters"""
+
+    def setUp(self):
+        source = os.path.join(os.path.dirname(__file__),
+                              "samples", "guitar.wav")
+
+        self.decoder = FileDecoder(source)
+
+    def _perform_test(self, analyzer_cls):
+        """Internal function that test if there is NaN in the results
+        of a given analyzer"""
+
+        pipe = (self.decoder | analyzer_cls())
+        pipe.run()
+        for key, result in pipe.results.items():
+            if 'value' in result.data_object.keys():
+                # Test for NaN
+                self.assertFalse(np.any(np.isnan(result.data)),
+                                 'NaN in %s data value' % result.name)
+                # Test for Inf
+                self.assertFalse(np.any(np.isinf(result.data)),
+                                 'Inf in %s data value' % result.name)
+
+
+def _tests_factory(test_class, test_doc, list_analyzers, skip_reasons={}):
+    """Define a test for each analyzer provided in the list"""
+    for analyzer in list_analyzers:
+
+        def test_func_factory(analyzer):
+            test_func = lambda self: self._perform_test(analyzer)
+            test_func.__doc__ = test_doc % analyzer.__name__
+            return test_func
+
+        test_func_name = "test_%s" % analyzer.__name__
+        test_func = test_func_factory(analyzer)
+
+        if analyzer.__name__ not in skip_reasons:
+            setattr(test_class, test_func_name, test_func)
+        else:  # Decorate with unittest.skip to skip test
+            setattr(test_class, test_func_name,
+                    unittest.skip(skip_reasons[analyzer.__name__])(test_func))
+
+
+# Define test to skip and corresponding reasons
+skip_reasons = {}
+
+# For each analyzer in TimeSide, test with constant input
+_tests_factory(test_class=TestAnalyzers_with_default,
+               test_doc="Test analyzer %s with default parameters",
+               list_analyzers=timeside.core.processors(timeside.api.IAnalyzer),
+               skip_reasons=skip_reasons)
+
+
+if __name__ == '__main__':
+    unittest.main(testRunner=TestRunner())