Spaces:
Build error
Build error
File size: 9,760 Bytes
48f8a08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
"""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 |