Exemplo n.º 1
0
    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 = os.path.join(base_cache_path,
                                 "5c186d1e123be2667fb5fd54640e4fd0.tts")

        if os.path.isfile(file_path):
            # Remove the file
            FileManager.remove_file(file_path)
        # Create a tmp file
        if not os.path.exists(base_cache_path):
            os.makedirs(base_cache_path)
        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.")
Exemplo n.º 2
0
    def _generate_audio_file(self):
        """
        Generic method used as a Callback in TTSModule
        """

        # Prepare payload
        payload = self.get_payload()

        headers = {"Content-Type": "application/json", "Accept": "audio/wav"}

        endpoint_location = self.location if self.location.endswith(
            '/') else self.location + "/"
        url = "%s/synthesize?voice=%s" % (endpoint_location + API_VERSION,
                                          self.voice)

        response = requests.post(url,
                                 auth=HTTPBasicAuth("apikey", self.apikey),
                                 headers=headers,
                                 json=payload)

        logger.debug("[Watson TTS] status code: %s" % response.status_code)

        if response.status_code == 200:
            # OK we get the audio we can write the sound file
            FileManager.write_in_file(self.file_path, response.content)
        else:
            logger.debug("[Watson TTS] Fail to get audio. Header: %s" %
                         response.headers)
Exemplo n.º 3
0
    def _generate_audio_file(self):
        """
        Generic method used as a Callback in TTSModule
        """

        # Prepare payload
        payload = self.get_payload()

        headers = {
            "Content-Type": "application/json",
            "Accept": "audio/wav"
        }

        url = "%s/synthesize?voice=%s" % (TTS_URL, self.voice)

        response = requests.post(url,
                                 auth=HTTPBasicAuth(self.username, self.password),
                                 headers=headers,
                                 json=payload)

        logger.debug("[Watson TTS] status code: %s" % response.status_code)

        if response.status_code == 200:
            # OK we get the audio we can write the sound file
            FileManager.write_in_file(self.file_path, response.content)
        else:
            logger.debug("[Watson TTS] Fail to get audio. Header: %s" % response.headers)
Exemplo n.º 4
0
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)
Exemplo n.º 5
0
    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 = os.path.join(base_cache_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")

        if os.path.isfile(file_path):
            # Remove the file
            FileManager.remove_file(file_path)
        # Create a tmp file
        if not os.path.exists(base_cache_path):
            os.makedirs(base_cache_path)
        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.")
Exemplo n.º 6
0
    def test_generate_and_play(self):
        """
        Test to generate and play sound
        """
        def new_play_audio(TTSModule):
            pass

        words = "kalliope"

        with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
            settings = Settings(cache_path="/tmp/kalliope/tests")
            self.TTSMod.settings = settings

            # test missing callback
            with self.assertRaises(TtsGenerateAudioFunctionNotFound):
                self.TTSMod.generate_and_play(words=words)

            # Assert Callback is called
            # no Cache
            self.TTSMod.cache = False
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True but not existing on system
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True and existing on system
            # create tmp file
            tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
            file_path = os.path.join(tmp_base_path,
                                     "5c186d1e123be2667fb5fd54640e4fd0.tts")
            if os.path.isfile(file_path):
                # Remove the file
                FileManager.remove_file(file_path)
            if not os.path.exists(tmp_base_path):
                os.makedirs(tmp_base_path)
            FileManager.write_in_file(
                file_path, "[kalliope-test] test_generate_and_play")
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=
                                          generate_audio_function_from_child)
            generate_audio_function_from_child.assert_not_called()
            # Remove the tmp file
            FileManager.remove_file(file_path)
Exemplo n.º 7
0
    def test_generate_and_play(self):
        """
        Test to generate and play sound
        """
        def new_play_audio(TTSModule):
            pass

        words = "kalliope"

        with mock.patch.object(TTSModule, 'play_audio', new=new_play_audio):
            settings = Settings(cache_path="/tmp/kalliope/tests")
            self.TTSMod.settings = settings

            # test missing callback
            with self.assertRaises(TtsGenerateAudioFunctionNotFound):
                self.TTSMod.generate_and_play(words=words)

            # Assert Callback is called
            # no Cache
            self.TTSMod.cache = False
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True but not existing on system
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_called()

            # with cache True and existing on system
            # create tmp file
            tmp_base_path = "/tmp/kalliope/tests/TTSModule/tests/default/"
            file_path = os.path.join(tmp_base_path, "5c186d1e123be2667fb5fd54640e4fd0.tts")
            if os.path.isfile(file_path):
                # Remove the file
                FileManager.remove_file(file_path)
            if not os.path.exists(tmp_base_path):
                os.makedirs(tmp_base_path)
            FileManager.write_in_file(file_path, "[kalliope-test] test_generate_and_play")
            self.TTSMod.cache = True
            generate_audio_function_from_child = mock.Mock()
            self.TTSMod.generate_and_play(words=words,
                                          generate_audio_function_from_child=generate_audio_function_from_child)
            generate_audio_function_from_child.assert_not_called()
            # Remove the tmp file
            FileManager.remove_file(file_path)
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        if sys.version_info[0] > 2:
            # Test writing of bytes object for python3
            FileManager.write_in_file(file_path=file_path,
                                      content=bytes(in_file_text, 'utf-8'))
            with open(file_path, 'r') as content_file:
                content = content_file.read()
                self.assertEqual(content, in_file_text,
                                 "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)

        # run into IOError by trying to write something in root
        dir_path = "/root/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        self.assertFalse(
            FileManager.write_in_file(file_path=file_path,
                                      content=in_file_text))
Exemplo n.º 9
0
    def test_write_in_file(self):
        """
        Test to write in file.
        """

        # set up the context
        dir_path = "/tmp/kalliope/tests/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path,file_name)
        in_file_text = "[Kalliope] Testing the write_in_file method from Utils.FileManager"
        if os.path.exists(file_path):
            os.remove(file_path)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        # Test FileManager.write_in_file
        FileManager.write_in_file(file_path=file_path, content=in_file_text)
        with open(file_path, 'r') as content_file:
            content = content_file.read()
            self.assertEqual(content, in_file_text,
                             "Fail writing in the file ")

        if sys.version_info[0] > 2:
            # Test writing of bytes object for python3
            FileManager.write_in_file(file_path=file_path, content=bytes(in_file_text, 'utf-8'))
            with open(file_path, 'r') as content_file:
                content = content_file.read()
                self.assertEqual(content, in_file_text,
                                "Fail writing in the file ")

        # Clean up
        if os.path.exists(file_path):
            os.remove(file_path)

        # run into IOError by trying to write something in root
        dir_path = "/root/"
        file_name = "test_FileManager_writeInFile"
        file_path = os.path.join(dir_path, file_name)
        self.assertFalse(FileManager.write_in_file(file_path=file_path, content=in_file_text))