diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..e42365c --- /dev/null +++ b/tests/README.md @@ -0,0 +1,16 @@ + + +## Run Tests + +_Run the following commands from the root of the repo._ + +### Run all tests +```sh +ELEVEN_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= PLAY= pytest elevenlabs/tests/api/test_voice.py::test_voice_design -s -v +``` diff --git a/tests/test_history.py b/tests/test_history.py new file mode 100644 index 0000000..852fba0 --- /dev/null +++ b/tests/test_history.py @@ -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 diff --git a/tests/test_model.py b/tests/test_model.py new file mode 100644 index 0000000..cd6f020 --- /dev/null +++ b/tests/test_model.py @@ -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) diff --git a/tests/test_user.py b/tests/test_user.py new file mode 100644 index 0000000..4bef222 --- /dev/null +++ b/tests/test_user.py @@ -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) diff --git a/tests/test_voice.py b/tests/test_voice.py new file mode 100644 index 0000000..bca5b32 --- /dev/null +++ b/tests/test_voice.py @@ -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)