python/tests/test_sink.py: trivial test for sink 'with' interface (PEP 343)
[aubio.git] / python / tests / test_sink.py
index 24ee9a5..c31564a 100755 (executable)
@@ -1,10 +1,13 @@
 #! /usr/bin/env python
 
-from numpy.testing import TestCase, assert_equal, assert_almost_equal
+from nose2 import main
 from nose2.tools import params
+from numpy.testing import TestCase
 from aubio import fvec, source, sink
-from numpy import array
-from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
+from .utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
+
+import warnings
+warnings.filterwarnings('ignore', category=UserWarning, append=True)
 
 list_of_sounds = list_all_sounds('sounds')
 samplerates = [0, 44100, 8000, 32000]
@@ -26,6 +29,26 @@ class aubio_sink_test_case(TestCase):
         if not len(list_of_sounds):
             self.skipTest('add some sound files in \'python/tests/sounds\'')
 
+    def test_wrong_filename(self):
+        with self.assertRaises(RuntimeError):
+            sink('')
+
+    def test_wrong_samplerate(self):
+        with self.assertRaises(RuntimeError):
+            sink(get_tmp_sink_path(), -1)
+
+    def test_wrong_samplerate_too_large(self):
+        with self.assertRaises(RuntimeError):
+            sink(get_tmp_sink_path(), 1536001, 2)
+
+    def test_wrong_channels(self):
+        with self.assertRaises(RuntimeError):
+            sink(get_tmp_sink_path(), 44100, -1)
+
+    def test_wrong_channels_too_large(self):
+        with self.assertRaises(RuntimeError):
+            sink(get_tmp_sink_path(), 44100, 202020)
+
     def test_many_sinks(self):
         from tempfile import mkdtemp
         import os.path
@@ -37,7 +60,7 @@ class aubio_sink_test_case(TestCase):
             g = sink(path, 0)
             sink_list.append(g)
             write = 32
-            for n in range(200):
+            for _ in range(200):
                 vec = fvec(write)
                 g(vec, write)
             g.close()
@@ -93,6 +116,12 @@ class aubio_sink_test_case(TestCase):
         g.close()
         del_tmp_sink_path(sink_path)
 
+    def test_read_with(self):
+        sink_path =get_tmp_sink_path()
+        vec = fvec(128)
+        with sink(sink_path, samplerate) as g:
+            for i in range(10):
+                g(vec, 128)
+
 if __name__ == '__main__':
-    from nose2 import main
     main()