Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import unittest

from models.exceptions import (
CredentialsSetupError,
DbRelatedError,
EventParsingError,
ExternalApiError,
LambdaConfigError,
LambdaLogicError,
LambdaValidationError,
)


class TestCustomExceptions(unittest.TestCase):
"""Test custom exception classes to ensure they can be raised and caught properly."""

def test_lambda_config_error(self):
"""Test LambdaConfigError can be raised and caught."""
with self.assertRaises(LambdaConfigError) as context:
raise LambdaConfigError("Configuration error occurred")
self.assertEqual(str(context.exception), "Configuration error occurred")

def test_credentials_setup_error(self):
"""Test CredentialsSetupError can be raised and caught."""
with self.assertRaises(CredentialsSetupError) as context:
raise CredentialsSetupError("Failed to setup credentials")
self.assertEqual(str(context.exception), "Failed to setup credentials")

def test_db_related_error(self):
"""Test DbRelatedError can be raised and caught."""
with self.assertRaises(DbRelatedError) as context:
raise DbRelatedError("Database connection failed")
self.assertEqual(str(context.exception), "Database connection failed")

def test_event_parsing_error(self):
"""Test EventParsingError can be raised and caught."""
with self.assertRaises(EventParsingError) as context:
raise EventParsingError("Invalid event format")
self.assertEqual(str(context.exception), "Invalid event format")

def test_lambda_logic_error(self):
"""Test LambdaLogicError can be raised and caught."""
with self.assertRaises(LambdaLogicError) as context:
raise LambdaLogicError("Business logic error")
self.assertEqual(str(context.exception), "Business logic error")

def test_lambda_validation_error(self):
"""Test LambdaValidationError can be raised and caught."""
with self.assertRaises(LambdaValidationError) as context:
raise LambdaValidationError("Validation failed")
self.assertEqual(str(context.exception), "Validation failed")

def test_external_api_error(self):
"""Test ExternalApiError can be raised and caught."""
with self.assertRaises(ExternalApiError) as context:
raise ExternalApiError("External API request failed")
self.assertEqual(str(context.exception), "External API request failed")

def test_exception_inheritance(self):
"""Test that all custom exceptions inherit from Exception."""
self.assertTrue(issubclass(LambdaConfigError, Exception))
self.assertTrue(issubclass(CredentialsSetupError, Exception))
self.assertTrue(issubclass(DbRelatedError, Exception))
self.assertTrue(issubclass(EventParsingError, Exception))
self.assertTrue(issubclass(LambdaLogicError, Exception))
self.assertTrue(issubclass(LambdaValidationError, Exception))
self.assertTrue(issubclass(ExternalApiError, Exception))

def test_exception_with_nested_cause(self):
"""Test that exceptions can be raised with a cause."""
original_error = ValueError("Original error")
try:
raise DbRelatedError("Database error") from original_error
except DbRelatedError as e:
self.assertIsInstance(e.__cause__, ValueError)
self.assertEqual(str(e.__cause__), "Original error")

def test_empty_message(self):
"""Test exceptions can be raised with empty messages."""
with self.assertRaises(LambdaConfigError):
raise LambdaConfigError("")

def test_multiline_message(self):
"""Test exceptions with multiline error messages."""
multiline_msg = "Error line 1\nError line 2\nError line 3"
with self.assertRaises(ExternalApiError) as context:
raise ExternalApiError(multiline_msg)
self.assertEqual(str(context.exception), multiline_msg)


if __name__ == "__main__":
unittest.main()
Loading
Loading