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_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_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_file_cleanup_priority(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) special_text = uuid.uuid4().hex request = SynthesizerRequest(text=special_text, 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)) special_text2 = uuid.uuid4().hex request = SynthesizerRequest(text=special_text2, metadata={}) response = speech_synthesizer._node_request_handler(request) res_dict = json.loads(response.result) audio_file2 = res_dict['Audio File'] self.assertTrue(os.path.exists(audio_file2)) for z in range(2): for i in range(2): request = SynthesizerRequest(text=uuid.uuid4().hex, metadata={}) response = speech_synthesizer._node_request_handler(request) request = SynthesizerRequest(text=special_text, metadata={}) response = speech_synthesizer._node_request_handler(request) res_dict = json.loads(response.result) audio_file1 = res_dict['Audio File'] self.assertFalse(os.path.exists(audio_file2)) self.assertTrue(os.path.exists(audio_file1))
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_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_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)