Skip to content

Commit

Permalink
Added testing files
Browse files Browse the repository at this point in the history
  • Loading branch information
applebym committed Jan 17, 2024
1 parent b6af279 commit c780ccb
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


## Run Tests

_Run the following commands from the root of the repo._

### Run all tests
```sh
ELEVEN_API_KEY=<YOUR_API_KEY> PLAY= pytest -s -v
```

### Run specific test
_e.g. `test_voice_design` in the `elevenlabs/tests/api/test_voice.py` file_
```sh
ELEVEN_API_KEY=<YOUR_API_KEY> PLAY= pytest elevenlabs/tests/api/test_voice.py::test_voice_design -s -v
```
38 changes: 38 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import warnings

def test_history():
from elevenlabs import History, HistoryItem

page_size = 5
# Test that we can get history
history = History.from_api(page_size=page_size)
assert isinstance(history, History)

# Test that we can iterate over multiple pages lazily
it = iter(history)
for i in range(page_size * 3):
try:
assert isinstance(next(it), HistoryItem)
except StopIteration:
warnings.warn("Warning: not enough history items to test multiple pages.")
break


def test_history_item_delete():
import time
from random import randint

from elevenlabs import History, generate

# Random text
text = f"Test {randint(0, 1000)}"
generate(text=text) # Generate a history item to delete
time.sleep(1)
history = History.from_api(page_size=1)
history_item = history[0]
# Check that item matches
assert history_item.text == text
history_item.delete()
# Test that the history item was deleted
history = History.from_api(page_size=1)
assert len(history) == 0 or history[0].text != text
8 changes: 8 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def test_model():
from elevenlabs import Model, Models

# Test that we can get all models
models = Models.from_api()
assert isinstance(models, Models)
assert len(models) > 0
assert isinstance(models[0], Model)
7 changes: 7 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def test_user():
from elevenlabs import Subscription, User

# Test that we can get current user
user = User.from_api()
assert isinstance(user, User)
assert isinstance(user.subscription, Subscription)
100 changes: 100 additions & 0 deletions tests/test_voice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from .utils import use_play


def test_voice_from_id():
from elevenlabs import Voice, VoiceSettings

# Test that we can get a voice from id
voice_id = "21m00Tcm4TlvDq8ikWAM"
voice = Voice.from_id(voice_id)
assert isinstance(voice, Voice)

assert voice.voice_id == voice_id
assert voice.name == "Rachel"
assert voice.category == "premade"
assert isinstance(voice.settings, VoiceSettings)


def test_voice_clone():
from elevenlabs import Voice, clone, generate, play

from ..utils import as_local_files

voice_file_urls = [
"https://user-images.githubusercontent.com/12028621/235474694-584f7103-dab2-4c39-bb9a-8e5f00be85da.webm",
"https://user-images.githubusercontent.com/12028621/235474694-584f7103-dab2-4c39-bb9a-8e5f00be85da.webm",
]

with as_local_files(voice_file_urls) as files:
voice = clone(
name="Alex",
description=(
"An old American male voice with a slight hoarseness in his throat."
" Perfect for news"
),
files=files,
)

assert isinstance(voice, Voice)
assert voice.voice_id is not None
assert voice.name == "Alex"
assert voice.category == "cloned"
assert len(voice.samples) == len(voice_file_urls)

audio = generate(
text="Voice clone test successful.",
voice=voice,
)
assert isinstance(audio, bytes) and len(audio) > 0
voice.delete()

if use_play:
play(audio)


def test_voice_design():
from elevenlabs import Accent, Age, Gender, Voice, VoiceDesign, generate, play

voice_design = VoiceDesign(
name="Lexa",
text=(
"Hi! My name is Lexa, I'm a voice design test. I should have a middle aged"
" female voice with a british accent. "
),
voice_description="Middle aged female with british accent.",
gender=Gender.female,
age=Age.middle_aged,
accent=Accent.british,
accent_strength=1.5,
)

audio = voice_design.generate()
assert isinstance(audio, bytes) and len(audio) > 0
if use_play:
play(audio)

voice = Voice.from_design(voice_design)
assert isinstance(voice, Voice)

audio = generate(
text="Voice design test successful.",
voice=voice,
)
assert isinstance(audio, bytes) and len(audio) > 0
voice.delete()
if use_play:
play(audio)


def test_voices():
from elevenlabs import Voice, Voices

# Test that we can get voices from api
voices = Voices.from_api()

assert isinstance(voices, Voices)
assert len(voices) > 0
assert isinstance(voices[0], Voice)

for voice in voices:
assert isinstance(voice, Voice)

0 comments on commit c780ccb

Please sign in to comment.