|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Unit tests for VachanaTTS integration |
| 4 | +""" |
| 5 | +import unittest |
| 6 | +from unittest.mock import Mock, patch, MagicMock |
| 7 | +from pythaitts import TTS |
| 8 | + |
| 9 | + |
| 10 | +class TestVachanaIntegration(unittest.TestCase): |
| 11 | + """Test VachanaTTS integration""" |
| 12 | + |
| 13 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 14 | + def test_vachana_model_initialization(self, mock_vachana): |
| 15 | + """Test that VachanaTTS model can be initialized""" |
| 16 | + # Create TTS instance with vachana model |
| 17 | + tts = TTS(pretrained="vachana") |
| 18 | + |
| 19 | + # Verify model is loaded |
| 20 | + self.assertIsNotNone(tts.model) |
| 21 | + self.assertEqual(tts.pretrained, "vachana") |
| 22 | + |
| 23 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 24 | + def test_vachana_tts_call(self, mock_vachana_class): |
| 25 | + """Test calling tts method with vachana model""" |
| 26 | + # Setup mock |
| 27 | + mock_instance = Mock() |
| 28 | + mock_instance.return_value = "/tmp/output.wav" |
| 29 | + mock_vachana_class.return_value = mock_instance |
| 30 | + |
| 31 | + # Create TTS instance |
| 32 | + tts = TTS(pretrained="vachana") |
| 33 | + |
| 34 | + # Call tts method |
| 35 | + result = tts.tts("สวัสดีครับ", speaker_idx="th_f_1", filename="/tmp/test.wav") |
| 36 | + |
| 37 | + # Verify the model was called with correct parameters |
| 38 | + mock_instance.assert_called_once() |
| 39 | + call_args = mock_instance.call_args |
| 40 | + self.assertEqual(call_args.kwargs['text'], "สวัสดีครับ") |
| 41 | + self.assertEqual(call_args.kwargs['speaker_idx'], "th_f_1") |
| 42 | + self.assertEqual(call_args.kwargs['filename'], "/tmp/test.wav") |
| 43 | + self.assertEqual(call_args.kwargs['return_type'], "file") |
| 44 | + |
| 45 | + @patch('pythaitts.pretrained.vachana_tts.VachanaTTS') |
| 46 | + def test_vachana_with_preprocessing(self, mock_vachana_class): |
| 47 | + """Test that preprocessing works with vachana model""" |
| 48 | + # Setup mock |
| 49 | + mock_instance = Mock() |
| 50 | + mock_instance.return_value = "/tmp/output.wav" |
| 51 | + mock_vachana_class.return_value = mock_instance |
| 52 | + |
| 53 | + # Create TTS instance |
| 54 | + tts = TTS(pretrained="vachana") |
| 55 | + |
| 56 | + # Call tts method with text that needs preprocessing |
| 57 | + result = tts.tts("มี 5 คนๆ", speaker_idx="th_f_1", preprocess=True) |
| 58 | + |
| 59 | + # Verify preprocessing was applied |
| 60 | + mock_instance.assert_called_once() |
| 61 | + call_args = mock_instance.call_args |
| 62 | + processed_text = call_args.kwargs['text'] |
| 63 | + |
| 64 | + # Text should have numbers converted and ๆ expanded |
| 65 | + self.assertNotIn("5", processed_text) |
| 66 | + self.assertNotIn("ๆ", processed_text) |
| 67 | + self.assertIn("ห้า", processed_text) |
| 68 | + self.assertIn("คนคน", processed_text) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + unittest.main() |
0 commit comments