def test_good_synthesis_with_mostly_default_args_using_polly_lib(
            self, polly_class_mock):
        polly_obj_mock = MagicMock()
        polly_class_mock.return_value = polly_obj_mock

        test_text = 'hello'
        test_metadata = '''
            {
                "output_path": "/tmp/test"
            }
        '''
        expected_polly_synthesize_args = {
            'output_format': 'ogg_vorbis',
            'voice_id': 'Joanna',
            'sample_rate': '22050',
            'text_type': 'text',
            'text': test_text,
            'output_path': "/tmp/test"
        }

        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        speech_synthesizer = SpeechSynthesizer(engine='POLLY_LIBRARY')
        request = SynthesizerRequest(text=test_text, metadata=test_metadata)
        response = speech_synthesizer._node_request_handler(request)

        self.assertGreater(polly_class_mock.call_count, 0)
        polly_obj_mock.synthesize.assert_called_with(
            **expected_polly_synthesize_args)

        self.assertEqual(response.result,
                         polly_obj_mock.synthesize.return_value.result)
    def test_lost_file(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid
        import os
        import json

        db = DB()
        init_num_files = db.get_num_files()
        req_text = uuid.uuid4().hex

        speech_synthesizer = SpeechSynthesizer(engine='DUMMY')

        request = SynthesizerRequest(text=req_text, metadata={})
        response = speech_synthesizer._node_request_handler(request)
        res_dict = json.loads(response.result)
        audio_file1 = res_dict['Audio File']

        self.assertEqual(db.get_num_files(), init_num_files + 1)

        os.remove(audio_file1)

        request = SynthesizerRequest(text=req_text, metadata={})
        response = speech_synthesizer._node_request_handler(request)
        res_dict = json.loads(response.result)
        audio_file2 = res_dict['Audio File']

        self.assertEqual(db.get_num_files(), init_num_files + 1)
        self.assertEqual(audio_file1, audio_file2)
        self.assertTrue(os.path.exists(audio_file2))
    def test_multiple_novel(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid

        db = DB()
        init_num_files = db.get_num_files()
        for i in range(4):
            speech_synthesizer = SpeechSynthesizer(engine='DUMMY')
            request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
            response = speech_synthesizer._node_request_handler(request)

            self.assertEqual(db.get_num_files(), init_num_files + i + 1)
    def test_synthesis_with_bad_metadata_using_polly_lib(
            self, polly_class_mock):
        polly_obj_mock = MagicMock()
        polly_class_mock.return_value = polly_obj_mock

        test_text = 'hello'
        test_metadata = '''I am no JSON'''

        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        speech_synthesizer = SpeechSynthesizer(engine='POLLY_LIBRARY')
        request = SynthesizerRequest(text=test_text, metadata=test_metadata)
        response = speech_synthesizer._node_request_handler(request)

        self.assertTrue(response.result.startswith('Exception: '))
    def test_no_connection_novel(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid

        db = DB()
        init_num_files = db.get_num_files()

        speech_synthesizer = SpeechSynthesizer(engine='DUMMY')
        speech_synthesizer.engine.set_connection(False)

        request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
        response = speech_synthesizer._node_request_handler(request)

        self.assertEqual(db.get_num_files(), init_num_files)
    def test_big_db(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid

        db = DB()
        speech_synthesizer = SpeechSynthesizer(engine='DUMMY',
                                               max_cache_bytes=401)
        speech_synthesizer.engine.set_file_sizes(100)

        for i in range(20):
            request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
            response = speech_synthesizer._node_request_handler(request)

        self.assertEqual(db.get_num_files(), 4)

        speech_synthesizer = SpeechSynthesizer(engine='DUMMY',
                                               max_cache_bytes=40001)
        speech_synthesizer.engine.set_file_sizes(1000)

        for i in range(80):
            request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
            response = speech_synthesizer._node_request_handler(request)

        self.assertEqual(db.get_num_files(), 40)
    def test_bad_engine(self, polly_class_mock):
        polly_obj_mock = MagicMock()
        polly_class_mock.return_value = polly_obj_mock

        ex = None

        from tts.synthesizer import SpeechSynthesizer
        try:
            SpeechSynthesizer(engine='NON-EXIST ENGINE')
        except Exception as e:
            ex = e

        self.assertTrue(isinstance(ex, SpeechSynthesizer.BadEngineError))
    def test_file_cleanup(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid
        import json
        import os

        db = DB()
        speech_synthesizer = SpeechSynthesizer(engine='DUMMY',
                                               max_cache_bytes=401)
        speech_synthesizer.engine.set_file_sizes(100)

        request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
        response = speech_synthesizer._node_request_handler(request)
        res_dict = json.loads(response.result)
        audio_file1 = res_dict['Audio File']
        self.assertTrue(os.path.exists(audio_file1))

        for i in range(5):
            request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={})
            response = speech_synthesizer._node_request_handler(request)

        self.assertFalse(os.path.exists(audio_file1))
    def test_no_connection_existing(self):
        from tts.db import DB
        from tts.synthesizer import SpeechSynthesizer
        from tts.srv import SynthesizerRequest
        import uuid
        import json

        target_text = uuid.uuid4().hex

        speech_synthesizer = SpeechSynthesizer(engine='DUMMY')
        request = SynthesizerRequest(text=target_text, metadata={})
        response = speech_synthesizer._node_request_handler(request)
        res_dict = json.loads(response.result)
        audio_file1 = res_dict['Audio File']

        speech_synthesizer.engine.set_connection(False)

        speech_synthesizer = SpeechSynthesizer(engine='DUMMY')
        request = SynthesizerRequest(text=target_text, metadata={})
        response = speech_synthesizer._node_request_handler(request)
        res_dict = json.loads(response.result)
        audio_file2 = res_dict['Audio File']

        self.assertEqual(audio_file1, audio_file2)
 def test_init(self):
     from tts.synthesizer import SpeechSynthesizer
     speech_synthesizer = SpeechSynthesizer()
     self.assertEqual('text', speech_synthesizer.default_text_type)