Spaces:
Sleeping
Sleeping
Commit
·
23cd97b
1
Parent(s):
a49fb5a
Added TTS tests
Browse files- tests/test_ttsmanager.py +86 -0
tests/test_ttsmanager.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from unittest.mock import patch, Mock
|
| 3 |
+
from api.audio import TTSManager, APIError
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TestTTSManager:
|
| 7 |
+
|
| 8 |
+
def setup_method(self):
|
| 9 |
+
self.config = Mock()
|
| 10 |
+
self.config.tts.key = "test-key"
|
| 11 |
+
self.config.tts.url = "https://api.example.com"
|
| 12 |
+
self.config.tts.name = "test-tts-model"
|
| 13 |
+
self.config.tts.type = "OPENAI_API"
|
| 14 |
+
self.tts_manager = TTSManager(self.config)
|
| 15 |
+
|
| 16 |
+
@patch("requests.post")
|
| 17 |
+
@pytest.mark.parametrize("stream", [False, True])
|
| 18 |
+
def test_read_text(self, mock_post, stream):
|
| 19 |
+
self.tts_manager.streaming = stream
|
| 20 |
+
if stream:
|
| 21 |
+
mock_response = Mock()
|
| 22 |
+
mock_response.status_code = 200
|
| 23 |
+
mock_response.iter_content = Mock(return_value=[b"audio-bytes-part1", b"audio-bytes-part2"])
|
| 24 |
+
mock_post.return_value.__enter__ = Mock(return_value=mock_response)
|
| 25 |
+
mock_post.return_value.__exit__ = Mock(return_value=None)
|
| 26 |
+
|
| 27 |
+
result = list(self.tts_manager.read_text("Hello, world!"))
|
| 28 |
+
assert result == [b"audio-bytes-part1", b"audio-bytes-part2"]
|
| 29 |
+
else:
|
| 30 |
+
mock_response = Mock()
|
| 31 |
+
mock_response.status_code = 200
|
| 32 |
+
mock_response.content = b"audio-bytes"
|
| 33 |
+
mock_post.return_value = mock_response
|
| 34 |
+
|
| 35 |
+
result = list(self.tts_manager.read_text("Hello, world!"))
|
| 36 |
+
assert result == [b"audio-bytes"]
|
| 37 |
+
|
| 38 |
+
@patch("requests.post")
|
| 39 |
+
@pytest.mark.parametrize("stream", [False, True])
|
| 40 |
+
def test_read_text_error(self, mock_post, stream):
|
| 41 |
+
self.tts_manager.streaming = stream
|
| 42 |
+
mock_response = Mock()
|
| 43 |
+
mock_response.status_code = 500
|
| 44 |
+
mock_response.json.return_value = {"error": "Internal Server Error"}
|
| 45 |
+
mock_post.return_value.__enter__ = Mock(return_value=mock_response)
|
| 46 |
+
mock_post.return_value.__exit__ = Mock(return_value=None)
|
| 47 |
+
|
| 48 |
+
with pytest.raises(APIError):
|
| 49 |
+
list(self.tts_manager.read_text("Hello, world!"))
|
| 50 |
+
|
| 51 |
+
@patch("requests.post")
|
| 52 |
+
@pytest.mark.parametrize("stream", [False, True])
|
| 53 |
+
def test_read_last_message(self, mock_post, stream):
|
| 54 |
+
self.tts_manager.streaming = stream
|
| 55 |
+
chat_history = [["user", "Hello, world!"]]
|
| 56 |
+
if stream:
|
| 57 |
+
mock_response = Mock()
|
| 58 |
+
mock_response.status_code = 200
|
| 59 |
+
mock_response.iter_content = Mock(return_value=[b"audio-bytes-part1", b"audio-bytes-part2"])
|
| 60 |
+
mock_post.return_value.__enter__ = Mock(return_value=mock_response)
|
| 61 |
+
mock_post.return_value.__exit__ = Mock(return_value=None)
|
| 62 |
+
|
| 63 |
+
result = list(self.tts_manager.read_last_message(chat_history))
|
| 64 |
+
assert result == [b"audio-bytes-part1", b"audio-bytes-part2"]
|
| 65 |
+
else:
|
| 66 |
+
mock_response = Mock()
|
| 67 |
+
mock_response.status_code = 200
|
| 68 |
+
mock_response.content = b"audio-bytes"
|
| 69 |
+
mock_post.return_value = mock_response
|
| 70 |
+
|
| 71 |
+
result = list(self.tts_manager.read_last_message(chat_history))
|
| 72 |
+
assert result == [b"audio-bytes"]
|
| 73 |
+
|
| 74 |
+
@patch("requests.post")
|
| 75 |
+
@pytest.mark.parametrize("stream", [False, True])
|
| 76 |
+
def test_read_last_message_error(self, mock_post, stream):
|
| 77 |
+
self.tts_manager.streaming = stream
|
| 78 |
+
chat_history = [["user", "Hello, world!"]]
|
| 79 |
+
mock_response = Mock()
|
| 80 |
+
mock_response.status_code = 500
|
| 81 |
+
mock_response.json.return_value = {"error": "Internal Server Error"}
|
| 82 |
+
mock_post.return_value.__enter__ = Mock(return_value=mock_response)
|
| 83 |
+
mock_post.return_value.__exit__ = Mock(return_value=None)
|
| 84 |
+
|
| 85 |
+
with pytest.raises(APIError):
|
| 86 |
+
list(self.tts_manager.read_last_message(chat_history))
|