tts-markup-utility/test_audiogen_validate.py

59 lines
2.7 KiB
Python

from unittest import TestCase
from unittest.mock import patch
from audiogen import AudioGenerator
class TestAudioGeneratorValidate(TestCase):
def setUp(self):
self.valid_parsed_data = [
{"type": "voice", "voice": "alloy"},
{"type": "text", "text": "Hello, world!"},
{"type": "silence", "duration": 1000},
{"type": "section_start", "section_id": 1},
{"type": "text", "text": "This is section 1."},
{"type": "section_end"},
{"type": "insert_section", "section_id": 1},
]
self.audio_generator = AudioGenerator(self.valid_parsed_data, "test_output.mp3", api_key="abc")
def test_validate_voices_valid(self):
self.audio_generator.validate_voices()
# No assertion needed as the function should not raise any exception
def test_validate_voices_invalid(self):
invalid_parsed_data = [
{"type": "voice", "voice": "invalid_voice"},
{"type": "text", "text": "Hello, world!"},
]
invalid_audio_generator = AudioGenerator(invalid_parsed_data, "test_output.mp3", api_key="abc")
with self.assertRaises(ValueError) as cm:
invalid_audio_generator.validate_voices()
self.assertEqual(str(cm.exception), "Invalid voice(s) found: invalid_voice")
def test_validate_sections_valid(self):
self.audio_generator.validate_sections()
# No assertion needed as the function should not raise any exception
def test_validate_sections_invalid(self):
invalid_parsed_data = [
{"type": "voice", "voice": "alloy"},
{"type": "text", "text": "Hello, world!"},
{"type": "insert_section", "section_id": 1},
]
invalid_audio_generator = AudioGenerator(invalid_parsed_data, "test_output.mp3", api_key="abc")
with self.assertRaises(ValueError) as cm:
invalid_audio_generator.validate_sections()
self.assertIn("Section 1 is used before being defined.", str(cm.exception))
self.assertIn("Undefined section(s) used: 1", str(cm.exception))
# @patch('builtins.input', return_value='no')
# @patch('audiogen.AudioGenerator.text_to_speech')
# def test_text_to_speech_retry_logic(self, mock_text_to_speech, mock_input):
# mock_text_to_speech.side_effect = [Exception('API error'), Exception('API error'), Exception('API error')]
# with self.assertRaises(SystemExit) as cm:
# self.audio_generator.text_to_speech('Hello, world!', 'alloy')
# self.assertEqual(cm.exception.code, 1)
# self.assertEqual(mock_text_to_speech.call_count, 3)
# mock_input.assert_called_once_with("Retry TTS generation? (yes/no): ")