tts-markup-utility/test_audiogen_validate.py

59 lines
2.7 KiB
Python
Raw Normal View History

2024-04-25 13:13:59 +00:00
from unittest import TestCase
from unittest.mock import patch
from audiogen import AudioGenerator
2024-04-25 13:32:07 +00:00
2024-04-25 13:13:59 +00:00
class TestAudioGeneratorValidate(TestCase):
def setUp(self):
self.valid_parsed_data = [
2024-04-25 13:32:07 +00:00
{"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},
2024-04-25 13:13:59 +00:00
]
self.audio_generator = AudioGenerator(self.valid_parsed_data, "test_output.mp3", api_key="")
2024-04-25 13:13:59 +00:00
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 = [
2024-04-25 13:32:07 +00:00
{"type": "voice", "voice": "invalid_voice"},
{"type": "text", "text": "Hello, world!"},
2024-04-25 13:13:59 +00:00
]
invalid_audio_generator = AudioGenerator(invalid_parsed_data, "test_output.mp3", api_key="")
2024-04-25 13:13:59 +00:00
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 = [
2024-04-25 13:32:07 +00:00
{"type": "voice", "voice": "alloy"},
{"type": "text", "text": "Hello, world!"},
{"type": "insert_section", "section_id": 1},
2024-04-25 13:13:59 +00:00
]
invalid_audio_generator = AudioGenerator(invalid_parsed_data, "test_output.mp3", api_key="")
2024-04-25 13:13:59 +00:00
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))
2024-04-25 13:13:59 +00:00
# @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): ")