|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Unit tests for VachanaTTS integration |
| 4 | +""" |
| 5 | +import unittest |
| 6 | +from unittest.mock import Mock, patch, MagicMock |
| 7 | +import numpy as np |
| 8 | +from pythaitts import TTS |
| 9 | + |
| 10 | + |
| 11 | +class TestVachanaIntegration(unittest.TestCase): |
| 12 | + """Test VachanaTTS integration""" |
| 13 | + |
| 14 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 15 | + def test_vachana_model_initialization(self, mock_vachana): |
| 16 | + """Test that VachanaTTS model can be initialized""" |
| 17 | + # Create TTS instance with vachana model |
| 18 | + tts = TTS(pretrained="vachana") |
| 19 | + |
| 20 | + # Verify model is loaded |
| 21 | + self.assertIsNotNone(tts.model) |
| 22 | + self.assertEqual(tts.pretrained, "vachana") |
| 23 | + |
| 24 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 25 | + def test_vachana_tts_call(self, mock_vachana_class): |
| 26 | + """Test calling tts method with vachana model""" |
| 27 | + # Setup mock |
| 28 | + mock_instance = Mock() |
| 29 | + mock_instance.return_value = "/tmp/output.wav" |
| 30 | + mock_vachana_class.return_value = mock_instance |
| 31 | + |
| 32 | + # Create TTS instance |
| 33 | + tts = TTS(pretrained="vachana") |
| 34 | + |
| 35 | + # Call tts method |
| 36 | + result = tts.tts("สวัสดีครับ", speaker_idx="th_f_1", filename="/tmp/test.wav") |
| 37 | + |
| 38 | + # Verify the model was called with correct parameters |
| 39 | + mock_instance.assert_called_once() |
| 40 | + call_args = mock_instance.call_args |
| 41 | + self.assertEqual(call_args.kwargs['text'], "สวัสดีครับ") |
| 42 | + self.assertEqual(call_args.kwargs['speaker_idx'], "th_f_1") |
| 43 | + self.assertEqual(call_args.kwargs['filename'], "/tmp/test.wav") |
| 44 | + self.assertEqual(call_args.kwargs['return_type'], "file") |
| 45 | + |
| 46 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 47 | + def test_vachana_with_preprocessing(self, mock_vachana_class): |
| 48 | + """Test that preprocessing works with vachana model""" |
| 49 | + # Setup mock |
| 50 | + mock_instance = Mock() |
| 51 | + mock_instance.return_value = "/tmp/output.wav" |
| 52 | + mock_vachana_class.return_value = mock_instance |
| 53 | + |
| 54 | + # Create TTS instance |
| 55 | + tts = TTS(pretrained="vachana") |
| 56 | + |
| 57 | + # Call tts method with text that needs preprocessing |
| 58 | + result = tts.tts("มี 5 คนๆ", speaker_idx="th_f_1", preprocess=True) |
| 59 | + |
| 60 | + # Verify preprocessing was applied |
| 61 | + mock_instance.assert_called_once() |
| 62 | + call_args = mock_instance.call_args |
| 63 | + processed_text = call_args.kwargs['text'] |
| 64 | + |
| 65 | + # Text should have numbers converted and ๆ expanded |
| 66 | + self.assertNotIn("5", processed_text) |
| 67 | + self.assertNotIn("ๆ", processed_text) |
| 68 | + self.assertIn("ห้า", processed_text) |
| 69 | + self.assertIn("คนคน", processed_text) |
| 70 | + |
| 71 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 72 | + def test_vachana_all_supported_voices(self, mock_vachana_class): |
| 73 | + """Test that all supported voices work correctly""" |
| 74 | + # Setup mock |
| 75 | + mock_instance = Mock() |
| 76 | + mock_instance.return_value = "/tmp/output.wav" |
| 77 | + mock_vachana_class.return_value = mock_instance |
| 78 | + |
| 79 | + # Create TTS instance |
| 80 | + tts = TTS(pretrained="vachana") |
| 81 | + |
| 82 | + # Test all supported voices |
| 83 | + supported_voices = ["th_f_1", "th_m_1", "th_f_2", "th_m_2"] |
| 84 | + for voice in supported_voices: |
| 85 | + mock_instance.reset_mock() |
| 86 | + result = tts.tts("สวัสดี", speaker_idx=voice) |
| 87 | + |
| 88 | + # Verify the voice was passed correctly |
| 89 | + call_args = mock_instance.call_args |
| 90 | + self.assertEqual(call_args.kwargs['speaker_idx'], voice) |
| 91 | + |
| 92 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 93 | + def test_vachana_waveform_return(self, mock_vachana_class): |
| 94 | + """Test waveform return type functionality""" |
| 95 | + # Setup mock |
| 96 | + mock_instance = Mock() |
| 97 | + mock_waveform = np.array([0.1, 0.2, 0.3, 0.4]) |
| 98 | + mock_instance.return_value = mock_waveform |
| 99 | + mock_vachana_class.return_value = mock_instance |
| 100 | + |
| 101 | + # Create TTS instance |
| 102 | + tts = TTS(pretrained="vachana") |
| 103 | + |
| 104 | + # Call tts method with waveform return type |
| 105 | + result = tts.tts("สวัสดี", speaker_idx="th_f_1", return_type="waveform") |
| 106 | + |
| 107 | + # Verify the return type was set correctly |
| 108 | + call_args = mock_instance.call_args |
| 109 | + self.assertEqual(call_args.kwargs['return_type'], "waveform") |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + unittest.main() |
0 commit comments