فهرست منبع

[Test] unitesting convert to wav

Fix to return directly the incoming file if it is a .wav
ThiBuff 7 سال پیش
والد
کامیت
d34f7c7c6e
2فایلهای تغییر یافته به همراه32 افزوده شده و 9 حذف شده
  1. 21 1
      Tests/test_rest_api.py
  2. 11 8
      kalliope/core/RestAPI/FlaskAPI.py

+ 21 - 1
Tests/test_rest_api.py

@@ -1,10 +1,11 @@
 import json
 import os
+import tempfile
 import unittest
-import ast
 
 from flask import Flask
 from flask_testing import LiveServerTestCase
+from mock import mock
 
 from kalliope.core import LIFOBuffer
 from kalliope.core.Models import Singleton
@@ -275,6 +276,25 @@ class TestRestAPI(LiveServerTestCase):
         #         self.assertEqual(json.dumps(expected_content), json.dumps(json.loads(result.get_data())))
         #         self.assertEqual(result.status_code, 201)
 
+    def test_convert_to_wav(self):
+        """
+        Test the api function to convert incoming sound file to wave.
+        """
+
+        with mock.patch("os.system") as mock_os_system:
+            # Scenario 1 : input wav file
+            temp_file = "/tmp/kalliope/tempfile.wav" # tempfile.NamedTemporaryFile(suffix=".wav")
+            result_file = FlaskAPI._convert_to_wav(temp_file)
+            self.assertEqual(temp_file, result_file)
+            mock_os_system.assert_not_called()
+
+            # Scenario 2 : input not a wav file
+            temp_file = "/tmp/kalliope/tempfile.amr"  # tempfile.NamedTemporaryFile(suffix=".wav")
+            expected_result = "/tmp/kalliope/tempfile.wav"
+            result_file = FlaskAPI._convert_to_wav(temp_file)
+            self.assertEqual(expected_result, result_file)
+            mock_os_system.assert_called_once_with("avconv -y -i " + temp_file + " " + expected_result)
+
 if __name__ == '__main__':
     unittest.main()
 

+ 11 - 8
kalliope/core/RestAPI/FlaskAPI.py

@@ -252,17 +252,20 @@ class FlaskAPI(threading.Thread):
     @staticmethod
     def _convert_to_wav(audio_file_path):
         """
-        Convert an incoming audio file to wav format. Using either system avconv (raspberry)
-        :param audio_file_path: the current file path
-        :return: new Wave file path
+        If not already .wav, convert an incoming audio file to wav format. Using system avconv (raspberry)
+        :param audio_file_path: the current full file path
+        :return: Wave file path
         """
         # Not allowed so convert into wav using avconv (raspberry)
         base = os.path.splitext(audio_file_path)[0]
-        new_file_path = base + ".wav"
-        os.system("avconv -y -i " + audio_file_path + " " + new_file_path)  # --> deprecated
-        # subprocess.call(['avconv', '-y', '-i', audio_path, new_file_path], shell=True) # Not working ...
-
-        return new_file_path
+        extension = os.path.splitext(audio_file_path)[1]
+        if extension != ".wav":
+            current_file_path = audio_file_path
+            audio_file_path = base + ".wav"
+            os.system("avconv -y -i " + current_file_path + " " + audio_file_path)  # --> deprecated
+            # subprocess.call(['avconv', '-y', '-i', audio_path, new_file_path], shell=True) # Not working ...
+
+        return audio_file_path
 
     @requires_auth
     def shutdown_server(self):