-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
97 lines (75 loc) · 3.62 KB
/
test_exceptions.py
File metadata and controls
97 lines (75 loc) · 3.62 KB
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
"""Unit tests for pysqlit/exceptions.py module."""
import pytest
from pysqlit.exceptions import DatabaseError, TransactionError
class TestDatabaseError:
"""Test cases for DatabaseError exception."""
def test_database_error_creation(self):
"""Test basic DatabaseError creation."""
error = DatabaseError("Test error message")
assert str(error) == "Test error message"
assert isinstance(error, Exception)
def test_database_error_with_cause(self):
"""Test DatabaseError with underlying cause."""
cause = ValueError("Underlying error")
error = DatabaseError("Database error")
assert "Database error" in str(error)
assert error.__cause__ is None # Standard Exception doesn't store cause this way
def test_database_error_inheritance(self):
"""Test DatabaseError inheritance."""
error = DatabaseError("test")
assert isinstance(error, Exception)
assert type(error).__name__ == "DatabaseError"
class TestTransactionError:
"""Test cases for TransactionError exception."""
def test_transaction_error_creation(self):
"""Test basic TransactionError creation."""
error = TransactionError("Transaction failed")
assert str(error) == "Transaction failed"
assert isinstance(error, DatabaseError)
def test_transaction_error_inheritance(self):
"""Test TransactionError inheritance."""
error = TransactionError("test")
assert isinstance(error, TransactionError)
assert isinstance(error, DatabaseError)
assert isinstance(error, Exception)
def test_transaction_error_with_details(self):
"""Test TransactionError with transaction details."""
error = TransactionError("Transaction 123 failed due to deadlock")
assert "Transaction 123" in str(error)
assert "deadlock" in str(error)
class TestExceptionUsage:
"""Test cases for exception usage patterns."""
def test_database_error_raising(self):
"""Test raising DatabaseError."""
with pytest.raises(DatabaseError) as exc_info:
raise DatabaseError("Database connection failed")
assert str(exc_info.value) == "Database connection failed"
def test_transaction_error_raising(self):
"""Test raising TransactionError."""
with pytest.raises(TransactionError) as exc_info:
raise TransactionError("Transaction rollback failed")
assert str(exc_info.value) == "Transaction rollback failed"
assert isinstance(exc_info.value, DatabaseError)
def test_exception_catching(self):
"""Test catching specific exceptions."""
try:
raise DatabaseError("Test error")
except DatabaseError as e:
assert str(e) == "Test error"
try:
raise TransactionError("Test transaction error")
except TransactionError as e:
assert str(e) == "Test transaction error"
# Test catching DatabaseError also catches TransactionError
try:
raise TransactionError("Test transaction error")
except DatabaseError as e:
assert str(e) == "Test transaction error"
def test_exception_with_formatting(self):
"""Test exception with formatted messages."""
table_name = "users"
error = DatabaseError(f"Table {table_name} does not exist")
assert str(error) == "Table users does not exist"
tx_id = 123
error = TransactionError(f"Transaction {tx_id} timed out")
assert str(error) == "Transaction 123 timed out"