Parcourir la source

[Tests] Second part of the cache module testing ... to be continued

monf il y a 8 ans
Parent
commit
ffbf7c69e0
2 fichiers modifiés avec 66 ajouts et 9 suppressions
  1. 10 9
      core/TTS/TTSModule.py
  2. 56 0
      core/Tests/test_tts_module.py

+ 10 - 9
core/TTS/TTSModule.py

@@ -64,9 +64,9 @@ class TTSModule(object):
         FileManager.create_directory(base_path)
 
         logger.debug("Class TTSModule called from module %s, cache: %s, language: %s, voice: %s" % (self.tts_caller_name,
-                                                                                             self.cache,
-                                                                                             self.language,
-                                                                                             self.voice))
+                                                                                                     self.cache,
+                                                                                                     self.language,
+                                                                                                     self.voice))
 
     def play_audio(self):
         """
@@ -96,7 +96,7 @@ class TTSModule(object):
             generate_audio_function_from_child()
         else:
             # we check if the file already exist. If not we generate it with the TTS engine
-            if not self.is_file_already_in_cache():
+            if not self._is_file_already_in_cache(self.base_cache_path, self.file_path):
                 generate_audio_function_from_child()
 
         # then play the generated audio file
@@ -136,18 +136,19 @@ class TTSModule(object):
             words = words.encode('utf-8')
         return hashlib.md5(words).hexdigest()
 
-    def is_file_already_in_cache(self):
+    @staticmethod
+    def _is_file_already_in_cache(base_cache_path, file_path):
         """
         Return true if the file to generate has already been generated before
         """
         # generate sub folder
-        FileManager.create_directory(self.base_cache_path)
+        FileManager.create_directory(base_cache_path)
 
         # check if the audio file exist
-        exist_in_cache = os.path.exists(self.file_path)
+        exist_in_cache = os.path.exists(file_path)
 
         if exist_in_cache:
-            logger.debug("TTSModule, File already in cache: %s" % self.file_path)
+            logger.debug("TTSModule, File already in cache: %s" % file_path)
         else:
-            logger.debug("TTSModule, File not yet in cache: %s" % self.file_path)
+            logger.debug("TTSModule, File not yet in cache: %s" % file_path)
         return exist_in_cache

+ 56 - 0
core/Tests/test_tts_module.py

@@ -1,6 +1,9 @@
 import unittest
+import os
 
 from core.TTS.TTSModule import TTSModule
+from core.Models.Settings import Settings
+from core.FileManager import FileManager
 
 
 class TestTTSModule(unittest.TestCase):
@@ -16,9 +19,62 @@ class TestTTSModule(unittest.TestCase):
         """
         Test generate md5 method
         """
+
         word = "kalliope"
         expected_result = "5c186d1e123be2667fb5fd54640e4fd0"
 
         self.assertEquals(TTSModule.generate_md5_from_words(words=word),
                           expected_result,
                           "Fail md5")
+
+    def test_get_path_to_store_audio(self):
+        """
+        Test the path to store audio
+        """
+
+        self.TTSMod.words = "kalliope"
+        settings = Settings(cache_path="/tmp/kalliope/tests")
+        self.TTSMod.settings = settings
+
+        expected_result = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
+
+        self.assertEquals(self.TTSMod._get_path_to_store_audio(),
+                          expected_result,
+                          "fail test_get_path_to_store_audio, expected path not corresponding to result")
+
+    def test_generate_and_play(self):
+        """
+        Test to generate and play sound
+        """
+        def play_audio():
+            pass
+
+        self.TTSMod.words = "kalliope"
+        settings = Settings(cache_path="/tmp/kalliope/tests")
+        self.TTSMod.settings = settings
+        self.TTSMod.play_audio
+
+
+    def test_is_file_already_in_cache(self):
+        """
+        Test if file is already stored in cache
+        """
+
+        base_cache_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
+        md5_word = "5c186d1e123be2667fb5fd54640e4fd0"
+        file_path = "/tmp/kalliope/tests/TTSModule/tests/default/5c186d1e123be2667fb5fd54640e4fd0.tts"
+
+        # Create a tmp file
+        tmp_path = os.path.join(base_cache_path, md5_word+".tts")
+        FileManager.write_in_file(tmp_path, "[kalliope-test] test_is_file_already_in_cache")
+
+        # Test true
+        self.assertTrue(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
+                        "Fail retrieving the cached file. The file does not exist but it should !")
+
+        # Remove the tmp file
+        FileManager.remove_file(tmp_path)
+
+        # Test False
+        self.assertFalse(TTSModule._is_file_already_in_cache(base_cache_path=base_cache_path, file_path=file_path),
+                         "Fail asserting that the file does not exist.")