changeset 146:daeb73d16dbf

Add tests for _get_audio_parameters with invalid values
author Amine Sehili <amine.sehili@gmail.com>
date Tue, 19 Feb 2019 21:07:19 +0100
parents 85d26e96d259
children 90dd15daf373
files auditok/io.py tests/test_io.py
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/auditok/io.py	Tue Feb 19 20:18:45 2019 +0100
+++ b/auditok/io.py	Tue Feb 19 21:07:19 2019 +0100
@@ -138,7 +138,9 @@
                                          channels,
                                          use_channel)
     """
-    err_message = "'{ln}' (or '{sn}') must be an integer, found: '{val}'"
+    err_message = (
+        "'{ln}' (or '{sn}') must be a positive integer, found: '{val}'"
+    )
     parameters = []
     for (long_name, short_name) in (
         ("sampling_rate", "sr"),
@@ -146,7 +148,7 @@
         ("channels", "ch"),
     ):
         param = param_dict.get(long_name, param_dict.get(short_name))
-        if param is None or not isinstance(param, int):
+        if param is None or not isinstance(param, int) or param <= 0:
             raise AudioParameterError(
                 err_message.format(ln=long_name, sn=short_name, val=param)
             )
--- a/tests/test_io.py	Tue Feb 19 20:18:45 2019 +0100
+++ b/tests/test_io.py	Tue Feb 19 21:07:19 2019 +0100
@@ -176,6 +176,25 @@
         self.assertEqual(result, expected)
 
     @genty_dataset(
+        str_sampling_rate=(("x", 2, 1, 0),),
+        negative_sampling_rate=((-8000, 2, 1, 0),),
+        str_sample_width=((8000, "x", 1, 0),),
+        negative_sample_width=((8000, -2, 1, 0),),
+        str_channels=((8000, 2, "x", 0),),
+        negative_channels=((8000, 2, -1, 0),),
+    )
+    def test_get_audio_parameters_invalid(self, values):
+        params = {
+            k: v
+            for k, v in zip(
+                ("sampling_rate", "sample_width", "channels", "use_channel"),
+                values,
+            )
+        }
+        with self.assertRaises(AudioParameterError):
+            _get_audio_parameters(params)
+
+    @genty_dataset(
         mono_1byte=([400], 1),
         stereo_1byte=([400, 600], 1),
         three_channel_1byte=([400, 600, 2400], 1),