-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_functions.py
224 lines (191 loc) · 6.35 KB
/
test_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import pytest
from unittest.mock import patch, MagicMock, mock_open
from io import StringIO
import pandas as pd
from functions import (
detect_string_variables,
detect_phonetic_variables,
detect_phonetic_conversion,
)
from Elevenlabs_functions import (
fetch_models,
fetch_voices,
get_voice_id,
generate_audio,
process_text,
bulk_generate_audio,
)
# Existing tests
def test_detect_string_variables():
text = "Hello {name}, welcome to {place}!"
result = detect_string_variables(text)
assert result == ["name", "place"]
def test_detect_phonetic_variables():
text = "The pronunciation of [[word]] is important."
result = detect_phonetic_variables(text)
assert result == ["word"]
def test_detect_phonetic_conversion():
script = "Say [[english:hello]] and [[french:bonjour]]"
result = detect_phonetic_conversion(script)
assert result == [("english", "hello"), ("french", "bonjour")]
def test_detect_string_variables_no_variables():
text = "Hello, welcome to our place!"
result = detect_string_variables(text)
assert result == []
def test_detect_phonetic_variables_no_variables():
text = "The pronunciation is important."
result = detect_phonetic_variables(text)
assert result == []
def test_detect_phonetic_conversion_no_conversion():
script = "Say hello and bonjour"
result = detect_phonetic_conversion(script)
assert result == []
# Tests for Elevenlabs functions
@pytest.fixture
def mock_requests():
with patch('Elevenlabs_functions.requests.get') as mock_get:
yield mock_get
def test_fetch_models(mock_requests):
mock_response = MagicMock()
mock_response.json.return_value = [
{"model_id": "model1", "name": "Model 1"},
{"model_id": "model2", "name": "Model 2"}
]
mock_requests.return_value = mock_response
result = fetch_models("fake_api_key")
assert result == [("model1", "Model 1"), ("model2", "Model 2")]
def test_fetch_voices(mock_requests):
mock_response = MagicMock()
mock_response.json.return_value = {
"voices": [
{"voice_id": "voice1", "name": "Voice 1"},
{"voice_id": "voice2", "name": "Voice 2"}
]
}
mock_requests.return_value = mock_response
result = fetch_voices("fake_api_key")
assert result == [("voice1", "Voice 1"), ("voice2", "Voice 2")]
def test_get_voice_id():
voices = [("voice1", "Voice 1"), ("voice2", "Voice 2")]
assert get_voice_id(voices, "Voice 1") == "voice1"
assert get_voice_id(voices, "Voice 2") == "voice2"
assert get_voice_id(voices, "Voice 3") is None
@patch('Elevenlabs_functions.requests.post')
@patch('Elevenlabs_functions.open', new_callable=mock_open)
def test_generate_audio(mock_file, mock_post):
mock_response = MagicMock()
mock_response.ok = True
mock_response.content = b"fake audio content"
mock_response.headers = {"x-seed": "12345"}
mock_post.return_value = mock_response
success, seed = generate_audio(
"fake_api_key",
0.5,
"model1",
0.7,
0.5,
True,
"voice1",
"Hello, world!",
"output.mp3"
)
assert success is True
assert seed == "12345"
mock_file.assert_called_once_with("output.mp3", "wb")
mock_file().write.assert_called_once_with(b"fake audio content")
@patch('Elevenlabs_functions.requests.post')
def test_generate_audio_failure(mock_post):
mock_response = MagicMock()
mock_response.ok = False
mock_response.text = "API Error"
mock_post.return_value = mock_response
success, seed = generate_audio(
"fake_api_key",
0.5,
"model1",
0.7,
0.5,
True,
"voice1",
"Hello, world!",
"output.mp3"
)
assert success is False
assert seed is None
# New tests for process_text and bulk_generate_audio
def test_process_text():
text = "Hello {name}\\nWelcome to {place}!"
processed_text, variables = process_text(text)
assert processed_text == "Hello {name}\nWelcome to {place}!"
assert variables == ["name", "place"]
@patch('Elevenlabs_functions.generate_audio')
def test_bulk_generate_audio(mock_generate_audio):
mock_generate_audio.return_value = (True, "12345")
csv_content = "text,filename\nHello {name},greeting_{name}\nWelcome to {place},welcome_{place}"
csv_file = StringIO(csv_content)
voice_settings = {
"stability": 0.5,
"similarity_boost": 0.7,
"style": 0.5,
"speaker_boost": True
}
result_df = bulk_generate_audio(
"fake_api_key",
"model1",
"voice1",
csv_file,
"output_dir",
voice_settings,
"Fixed",
seed="54321"
)
assert len(result_df) == 2
assert result_df['filename'].tolist() == ['greeting_{name}.mp3', 'welcome_{place}.mp3']
assert result_df['text'].tolist() == ['Hello {name}', 'Welcome to {place}']
assert all(result_df['success'])
assert all(result_df['seed'] == "12345")
@patch('Elevenlabs_functions.generate_audio')
def test_bulk_generate_audio_with_empty_csv(mock_generate_audio):
csv_file = StringIO("")
voice_settings = {
"stability": 0.5,
"similarity_boost": 0.7,
"style": 0.5,
"speaker_boost": True
}
result_df = bulk_generate_audio(
"fake_api_key",
"model1",
"voice1",
csv_file,
"output_dir",
voice_settings,
"Fixed",
seed="54321"
)
assert result_df.empty
@patch('Elevenlabs_functions.generate_audio')
def test_bulk_generate_audio_with_random_seed(mock_generate_audio):
mock_generate_audio.return_value = (True, None)
csv_content = "text,filename\nHello {name},greeting_{name}"
csv_file = StringIO(csv_content)
voice_settings = {
"stability": 0.5,
"similarity_boost": 0.7,
"style": 0.5,
"speaker_boost": True
}
result_df = bulk_generate_audio(
"fake_api_key",
"model1",
"voice1",
csv_file,
"output_dir",
voice_settings,
"Random"
)
assert len(result_df) == 1
assert result_df['filename'].tolist() == ['greeting_{name}.mp3']
assert result_df['text'].tolist() == ['Hello {name}']
assert all(result_df['success'])
assert result_df['seed'].iloc[0] is not None # Random seed should be generated