Skip to content

Commit 0a282eb

Browse files
committed
# This is a combination of 2 commits.
# This is the 1st commit message: feat: add e2e tests # This is the commit message elevenlabs#2: feat: update tests
1 parent b5063c5 commit 0a282eb

File tree

9 files changed

+122
-194
lines changed

9 files changed

+122
-194
lines changed

tests/test_async_generation.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/test_client.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/test_history.py

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1-
import time
2-
from random import randint
3-
4-
from elevenlabs import GetSpeechHistoryResponse, \
5-
play
6-
7-
from .utils import IN_GITHUB, client
1+
from elevenlabs import GetSpeechHistoryResponse, ElevenLabs
82

93

104
def test_history():
5+
client = ElevenLabs()
116
page_size = 5
127
history = client.history.get_all(page_size=page_size)
138
assert isinstance(history, GetSpeechHistoryResponse)
14-
15-
16-
def test_history_item_delete():
17-
text = f"Test {randint(0, 1000)}"
18-
audio = client.generate(text=text)
19-
if not IN_GITHUB:
20-
play(audio) # type: ignore
21-
22-
time.sleep(1)
23-
24-
history = client.history.get_all().history
25-
print(history)
26-
history_item = history[0]
27-
28-
assert history_item.text != None
29-
30-
# Check that item matches
31-
# assert history_item.text == text
32-
# client.history.delete(history_item.history_item_id)
33-
34-
# Test that the history item was deleted
35-
# history = client.history.get_all(page_size=1).history
36-
# assert len(history) == 0 or history[0].text != text

tests/test_model.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/test_models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from elevenlabs import Model
2+
from elevenlabs.client import ElevenLabs
3+
4+
5+
def test_models():
6+
client = ElevenLabs()
7+
models = client.models.get_all()
8+
assert len(models) > 0
9+
assert isinstance(models[0], Model)

tests/test_tts.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import asyncio
2+
3+
from elevenlabs import VoiceSettings, play
4+
from elevenlabs.client import AsyncElevenLabs, ElevenLabs
5+
6+
from .utils import IN_GITHUB, DEFAULT_TEXT, DEFAULT_VOICE, DEFAULT_MODEL
7+
import base64
8+
9+
10+
def test_tts_convert() -> None:
11+
"""Test basic text-to-speech generation."""
12+
client = ElevenLabs()
13+
audio_generator = client.text_to_speech.convert(text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL)
14+
audio = b"".join(audio_generator)
15+
assert isinstance(audio, bytes), "TTS should return bytes"
16+
if not IN_GITHUB:
17+
play(audio)
18+
19+
20+
def test_tts_convert_with_voice_settings() -> None:
21+
"""Test TTS with custom voice settings."""
22+
client = ElevenLabs()
23+
audio_generator = client.text_to_speech.convert(
24+
text=DEFAULT_TEXT,
25+
voice_id=DEFAULT_VOICE,
26+
model_id=DEFAULT_MODEL,
27+
voice_settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True),
28+
)
29+
audio = b"".join(audio_generator)
30+
assert isinstance(audio, bytes), "TTS with voice settings should return bytes"
31+
if not IN_GITHUB:
32+
play(audio)
33+
34+
35+
def test_tts_convert_as_stream():
36+
async def main():
37+
async_client = AsyncElevenLabs()
38+
results = async_client.text_to_speech.convert_as_stream(
39+
text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL
40+
)
41+
out = b""
42+
async for value in results:
43+
assert isinstance(value, bytes), "Stream chunks should be bytes"
44+
out += value
45+
if not IN_GITHUB:
46+
play(out)
47+
48+
asyncio.run(main())
49+
50+
51+
def test_tts_convert_with_timestamps() -> None:
52+
"""Test TTS generation with timestamps."""
53+
client = ElevenLabs()
54+
result = client.text_to_speech.convert_with_timestamps(
55+
text=DEFAULT_TEXT, voice_id=DEFAULT_VOICE, model_id=DEFAULT_MODEL
56+
)
57+
58+
assert "alignment" in result # type: ignore
59+
assert "characters" in result["alignment"] # type: ignore
60+
61+
if not IN_GITHUB:
62+
audio_bytes = base64.b64decode(result["audio_base64"]) # type: ignore
63+
play(audio_bytes)
64+
65+
66+
def test_tts_stream_with_timestamps():
67+
async def main():
68+
async_client = AsyncElevenLabs()
69+
audio_data = b""
70+
async_stream = async_client.text_to_speech.stream_with_timestamps(
71+
voice_id=DEFAULT_VOICE,
72+
text=DEFAULT_TEXT,
73+
model_id=DEFAULT_MODEL,
74+
)
75+
async for chunk in async_stream:
76+
if hasattr(chunk, "audio_base_64"):
77+
audio_bytes = base64.b64decode(chunk.audio_base_64)
78+
audio_data += audio_bytes
79+
80+
if not IN_GITHUB:
81+
play(audio_data)
82+
83+
asyncio.run(main())

tests/test_voice.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/test_voices.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from elevenlabs import Voice, VoiceSettings, ElevenLabs
2+
from .utils import DEFAULT_VOICE
3+
4+
5+
def test_get_voice():
6+
client = ElevenLabs()
7+
voice_id = DEFAULT_VOICE
8+
9+
voice = client.voices.get(voice_id)
10+
assert isinstance(voice, Voice)
11+
12+
assert voice.voice_id == voice_id
13+
if voice.settings is not None:
14+
assert isinstance(voice.settings, VoiceSettings)
15+
16+
17+
def test_get_voices():
18+
client = ElevenLabs()
19+
response = client.voices.get_all()
20+
21+
assert len(response.voices) > 0
22+
23+
for voice in response.voices:
24+
assert isinstance(voice, Voice)

tests/utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import httpx
44

55
from typing import Sequence, Generator
6-
from elevenlabs.client import ElevenLabs, \
7-
AsyncElevenLabs
86

97
IN_GITHUB = "GITHUB_ACTIONS" in os.environ
108

11-
client = ElevenLabs()
12-
13-
async_client = AsyncElevenLabs()
9+
DEFAULT_VOICE = "21m00Tcm4TlvDq8ikWAM"
10+
DEFAULT_TEXT = "Hello"
11+
DEFAULT_MODEL = "eleven_multilingual_v2"
1412

1513

1614
def as_local_files(urls: Sequence[str]) -> Generator[str, None, None]:
@@ -25,4 +23,4 @@ def as_local_files(urls: Sequence[str]) -> Generator[str, None, None]:
2523
yield temp_file.name
2624
# Remove the files
2725
for temp_file in temp_files:
28-
temp_file.close()
26+
temp_file.close()

0 commit comments

Comments
 (0)