Skip to content

Commit

Permalink
Made a start on issue to test optional
Browse files Browse the repository at this point in the history
  • Loading branch information
willwade committed May 21, 2024
1 parent 1fc8658 commit bfcaf29
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test_optional_dependencies.py
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

0 comments on commit bfcaf29

Please sign in to comment.