From 2ec5a22c570e17894bb6a15875f886b61fdcbc70 Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Thu, 14 Aug 2014 13:01:39 +0200 Subject: [PATCH] test(analyzers): add a test for running all analyzers with default parameters --- tests/test_analyzers_default.py | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/test_analyzers_default.py diff --git a/tests/test_analyzers_default.py b/tests/test_analyzers_default.py new file mode 100644 index 0000000..98455cf --- /dev/null +++ b/tests/test_analyzers_default.py @@ -0,0 +1,68 @@ +#! /usr/bin/env python + +# Author : Thomas Fillon + + +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()) -- 2.39.5