brickfrog commited on
Commit
62ca4de
·
verified ·
1 Parent(s): f75c84c

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. tests/test_exceptions.py +82 -0
tests/test_exceptions.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from unittest.mock import MagicMock
3
+ from ankigen.exceptions import (
4
+ AnkigenError,
5
+ ValidationError,
6
+ SecurityError,
7
+ APIError,
8
+ OpenAIAPIError,
9
+ Context7APIError,
10
+ ExportError,
11
+ CardGenerationError,
12
+ ConfigurationError,
13
+ handle_exception,
14
+ )
15
+
16
+
17
+ def test_exception_hierarchy():
18
+ """Test that all exceptions inherit correctly from AnkigenError."""
19
+ assert issubclass(ValidationError, AnkigenError)
20
+ assert issubclass(SecurityError, AnkigenError)
21
+ assert issubclass(APIError, AnkigenError)
22
+ assert issubclass(OpenAIAPIError, APIError)
23
+ assert issubclass(OpenAIAPIError, AnkigenError)
24
+ assert issubclass(Context7APIError, APIError)
25
+ assert issubclass(Context7APIError, AnkigenError)
26
+ assert issubclass(ExportError, AnkigenError)
27
+ assert issubclass(CardGenerationError, AnkigenError)
28
+ assert issubclass(ConfigurationError, AnkigenError)
29
+
30
+
31
+ def test_exception_catching():
32
+ """Test that exceptions can be caught by their parent classes."""
33
+ with pytest.raises(AnkigenError):
34
+ raise ValidationError("Validation failed")
35
+
36
+ with pytest.raises(APIError):
37
+ raise OpenAIAPIError("OpenAI API error")
38
+
39
+ with pytest.raises(AnkigenError):
40
+ raise OpenAIAPIError("OpenAI API error")
41
+
42
+
43
+ def test_handle_exception_logging():
44
+ """Test that handle_exception logs the error."""
45
+ mock_logger = MagicMock()
46
+ exc = ValueError("Test error")
47
+ message = "An error occurred"
48
+
49
+ try:
50
+ handle_exception(exc, mock_logger, message, reraise=False)
51
+ except Exception:
52
+ pytest.fail("handle_exception raised an error when reraise=False")
53
+
54
+ mock_logger.error.assert_called_once_with(f"{message}: {exc}", exc_info=True)
55
+
56
+
57
+ def test_handle_exception_reraise_true():
58
+ """Test that handle_exception re-raises the original exception when reraise=True."""
59
+ mock_logger = MagicMock()
60
+
61
+ try:
62
+ raise ValueError("Test error")
63
+ except ValueError as exc:
64
+ with pytest.raises(ValueError) as excinfo:
65
+ handle_exception(exc, mock_logger, "Error", reraise=True)
66
+ assert str(excinfo.value) == "Test error"
67
+
68
+
69
+ def test_handle_exception_reraise_as():
70
+ """Test that handle_exception wraps the exception in reraise_as type."""
71
+ mock_logger = MagicMock()
72
+ exc = ValueError("Original error")
73
+ message = "Wrapped error"
74
+
75
+ with pytest.raises(AnkigenError) as excinfo:
76
+ handle_exception(
77
+ exc, mock_logger, message, reraise=True, reraise_as=AnkigenError
78
+ )
79
+
80
+ assert isinstance(excinfo.value, AnkigenError)
81
+ assert str(excinfo.value) == f"{message}: {exc}"
82
+ assert excinfo.value.__cause__ is exc