changeset 132:a9d74315f2ee

Add tests for _save_wave missing audio parameter
author Amine Sehili <amine.sehili@gmail.com>
date Sat, 09 Feb 2019 18:42:14 +0100
parents 92a6f1d5f265
children d8ae412e5aa8
files auditok/io.py tests/test_io.py
diffstat 2 files changed, 30 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/io.py	Sat Feb 09 18:01:00 2019 +0100
+++ b/auditok/io.py	Sat Feb 09 18:42:14 2019 +0100
@@ -887,7 +887,10 @@
     Saves audio data to a wave file.
     See also :func:`to_file`.
     """
-    # use standard python's wave module
+    if None in (sampling_rate, sample_width, channels):
+        raise AudioParameterError(
+            "All audio parameters are required to save wave audio files"
+        )
     with wave.open(file, "w") as fp:
         fp.setframerate(sampling_rate)
         fp.setsampwidth(sample_width)
--- a/tests/test_io.py	Sat Feb 09 18:01:00 2019 +0100
+++ b/tests/test_io.py	Sat Feb 09 18:42:14 2019 +0100
@@ -201,20 +201,6 @@
             from_file(filename, audio_format, **kwargs)
         self.assertTrue(patch_function.called)
 
-    @genty_dataset(
-        mono=("mono_400Hz.raw", (400,)),
-        three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)),
-    )
-    def test_save_raw(self, filename, frequencies):
-        filename = "tests/data/test_16KHZ_{}".format(filename)
-        sample_width = 2
-        fmt = DATA_FORMAT[sample_width]
-        mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
-        data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
-        tmpfile = NamedTemporaryFile()
-        _save_raw(tmpfile.name, data)
-        self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
-
     def test_from_file_no_pydub(self):
         with patch("auditok.io._WITH_PYDUB", False):
             with self.assertRaises(AudioIOError):
@@ -539,6 +525,20 @@
                     self.assertFalse(ext_mock.called)
 
     @genty_dataset(
+        mono=("mono_400Hz.raw", (400,)),
+        three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)),
+    )
+    def test_save_raw(self, filename, frequencies):
+        filename = "tests/data/test_16KHZ_{}".format(filename)
+        sample_width = 2
+        fmt = DATA_FORMAT[sample_width]
+        mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
+        data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
+        tmpfile = NamedTemporaryFile()
+        _save_raw(tmpfile.name, data)
+        self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
+
+    @genty_dataset(
         mono=("mono_400Hz.wav", (400,)),
         three_channel=("3channel_400-800-1600Hz.wav", (400, 800, 1600)),
     )
@@ -553,3 +553,15 @@
         tmpfile = NamedTemporaryFile()
         _save_wave(tmpfile.name, data, sampling_rate, sample_width, channels)
         self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
+
+    @genty_dataset(
+        missing_sampling_rate=("sr",),
+        missing_sample_width=("sw",),
+        missing_channels=("ch",),
+    )
+    def test_save_wave_missing_audio_param(self, missing_param):
+        with self.assertRaises(AudioParameterError):
+            params = AUDIO_PARAMS_SHORT.copy()
+            del params[missing_param]
+            srate, swidth, channels, _ = _get_audio_parameters(params)
+            _save_wave("audio", b"\0\0", srate, swidth, channels)