forked from mediatechlab/tts-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made a start on issue to test optional
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# test_optional_dependencies.py | ||
import importlib | ||
import sys | ||
import pytest | ||
|
||
# List of optional dependencies and their corresponding TTS classes from tts_wrapper | ||
optional_dependencies = { | ||
"boto3": ("tts_wrapper", "PollyTTS", "PollyClient"), | ||
"ibm_watson": ("tts_wrapper", "WatsonTTS", "WatsonClient"), | ||
"google_cloud_texttospeech": ("tts_wrapper", "GoogleTTS", "GoogleClient"), | ||
"pyttsx3": ("tts_wrapper", "SapiTTS", "SapiClient"), | ||
"azure_cognitiveservices_speech": ("tts_wrapper", "MicrosoftTTS", "MicrosoftClient"), | ||
"requests": ("tts_wrapper", "ElevenLabsTTS", "ElevenLabsClient"), | ||
} | ||
|
||
@pytest.mark.parametrize("module_name, tts_wrapper, tts_class, client_class", optional_dependencies.items()) | ||
def test_optional_dependencies(module_name, tts_wrapper, tts_class, client_class): | ||
# Temporarily remove the module from sys.modules | ||
original_module = sys.modules.pop(module_name, None) | ||
|
||
try: | ||
# Attempt to import the TTS and Client classes without the optional dependency | ||
tts_module = importlib.import_module(tts_wrapper) | ||
tts_cls = getattr(tts_module, tts_class) | ||
client_cls = getattr(tts_module, client_class) | ||
except (ModuleNotFoundError, AttributeError): | ||
# If the import fails, it should be due to the missing optional dependency | ||
assert module_name not in sys.modules | ||
else: | ||
# If the import succeeds, ensure the classes are indeed available | ||
assert tts_cls is not None | ||
assert client_cls is not None | ||
finally: | ||
# Restore the original module if it was removed | ||
if original_module: | ||
sys.modules[module_name] = original_module |