teachingAssistant / tests /unit /domain /test_exceptions.py
Michael Hu
Create unit tests for domain layer
48f8a08
"""Unit tests for domain exceptions."""
import pytest
from src.domain.exceptions import (
DomainException,
InvalidAudioFormatException,
InvalidTextContentException,
TranslationFailedException,
SpeechRecognitionException,
SpeechSynthesisException,
InvalidVoiceSettingsException,
AudioProcessingException
)
class TestDomainExceptions:
"""Test cases for domain exceptions."""
def test_domain_exception_is_base_exception(self):
"""Test that DomainException is the base exception."""
exception = DomainException("Base domain error")
assert isinstance(exception, Exception)
assert str(exception) == "Base domain error"
def test_domain_exception_without_message(self):
"""Test DomainException without message."""
exception = DomainException()
assert isinstance(exception, Exception)
assert str(exception) == ""
def test_invalid_audio_format_exception_inheritance(self):
"""Test that InvalidAudioFormatException inherits from DomainException."""
exception = InvalidAudioFormatException("Invalid audio format")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Invalid audio format"
def test_invalid_audio_format_exception_usage(self):
"""Test InvalidAudioFormatException usage scenario."""
try:
raise InvalidAudioFormatException("Unsupported format: xyz")
except DomainException as e:
assert "Unsupported format: xyz" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_invalid_text_content_exception_inheritance(self):
"""Test that InvalidTextContentException inherits from DomainException."""
exception = InvalidTextContentException("Invalid text content")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Invalid text content"
def test_invalid_text_content_exception_usage(self):
"""Test InvalidTextContentException usage scenario."""
try:
raise InvalidTextContentException("Text content is empty")
except DomainException as e:
assert "Text content is empty" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_translation_failed_exception_inheritance(self):
"""Test that TranslationFailedException inherits from DomainException."""
exception = TranslationFailedException("Translation failed")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Translation failed"
def test_translation_failed_exception_usage(self):
"""Test TranslationFailedException usage scenario."""
try:
raise TranslationFailedException("Translation service unavailable")
except DomainException as e:
assert "Translation service unavailable" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_speech_recognition_exception_inheritance(self):
"""Test that SpeechRecognitionException inherits from DomainException."""
exception = SpeechRecognitionException("Speech recognition failed")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Speech recognition failed"
def test_speech_recognition_exception_usage(self):
"""Test SpeechRecognitionException usage scenario."""
try:
raise SpeechRecognitionException("STT model not available")
except DomainException as e:
assert "STT model not available" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_speech_synthesis_exception_inheritance(self):
"""Test that SpeechSynthesisException inherits from DomainException."""
exception = SpeechSynthesisException("Speech synthesis failed")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Speech synthesis failed"
def test_speech_synthesis_exception_usage(self):
"""Test SpeechSynthesisException usage scenario."""
try:
raise SpeechSynthesisException("TTS voice not found")
except DomainException as e:
assert "TTS voice not found" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_invalid_voice_settings_exception_inheritance(self):
"""Test that InvalidVoiceSettingsException inherits from DomainException."""
exception = InvalidVoiceSettingsException("Invalid voice settings")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Invalid voice settings"
def test_invalid_voice_settings_exception_usage(self):
"""Test InvalidVoiceSettingsException usage scenario."""
try:
raise InvalidVoiceSettingsException("Voice speed out of range")
except DomainException as e:
assert "Voice speed out of range" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_audio_processing_exception_inheritance(self):
"""Test that AudioProcessingException inherits from DomainException."""
exception = AudioProcessingException("Audio processing failed")
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
assert str(exception) == "Audio processing failed"
def test_audio_processing_exception_usage(self):
"""Test AudioProcessingException usage scenario."""
try:
raise AudioProcessingException("Pipeline validation failed")
except DomainException as e:
assert "Pipeline validation failed" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
def test_all_exceptions_inherit_from_domain_exception(self):
"""Test that all domain exceptions inherit from DomainException."""
exceptions = [
InvalidAudioFormatException("test"),
InvalidTextContentException("test"),
TranslationFailedException("test"),
SpeechRecognitionException("test"),
SpeechSynthesisException("test"),
InvalidVoiceSettingsException("test"),
AudioProcessingException("test")
]
for exception in exceptions:
assert isinstance(exception, DomainException)
assert isinstance(exception, Exception)
def test_exception_chaining_support(self):
"""Test that exceptions support chaining."""
original_error = ValueError("Original error")
try:
raise TranslationFailedException("Translation failed") from original_error
except TranslationFailedException as e:
assert e.__cause__ is original_error
assert str(e) == "Translation failed"
def test_exception_with_none_message(self):
"""Test exceptions with None message."""
exception = AudioProcessingException(None)
assert isinstance(exception, DomainException)
# Python converts None to empty string for exception messages
assert str(exception) == "None"
def test_exception_hierarchy_catching(self):
"""Test catching exceptions at different levels of hierarchy."""
# Test catching specific exception
try:
raise SpeechSynthesisException("TTS failed")
except SpeechSynthesisException as e:
assert "TTS failed" in str(e)
except Exception:
pytest.fail("Should have caught SpeechSynthesisException")
# Test catching at domain level
try:
raise SpeechSynthesisException("TTS failed")
except DomainException as e:
assert "TTS failed" in str(e)
except Exception:
pytest.fail("Should have caught as DomainException")
# Test catching at base level
try:
raise SpeechSynthesisException("TTS failed")
except Exception as e:
assert "TTS failed" in str(e)
def test_exception_equality(self):
"""Test exception equality comparison."""
exc1 = AudioProcessingException("Same message")
exc2 = AudioProcessingException("Same message")
exc3 = AudioProcessingException("Different message")
# Exceptions are not equal even with same message (different instances)
assert exc1 is not exc2
assert exc1 is not exc3
# But they have the same type and message
assert type(exc1) == type(exc2)
assert str(exc1) == str(exc2)
assert str(exc1) != str(exc3)
def test_exception_repr(self):
"""Test exception string representation."""
exception = TranslationFailedException("Translation service error")
# Test that repr includes class name and message
repr_str = repr(exception)
assert "TranslationFailedException" in repr_str
assert "Translation service error" in repr_str
def test_exception_args_property(self):
"""Test exception args property."""
message = "Test error message"
exception = SpeechRecognitionException(message)
assert exception.args == (message,)
assert exception.args[0] == message