-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_execeptions.py
More file actions
192 lines (148 loc) · 7.28 KB
/
test_execeptions.py
File metadata and controls
192 lines (148 loc) · 7.28 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
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
"""Test cases for the custom exceptions in the sqliter module."""
import sqlite3
import pytest
from pytest_mock import MockerFixture
from sqliter.exceptions import (
DatabaseConnectionError,
RecordDeletionError,
RecordInsertionError,
RecordNotFoundError,
RecordUpdateError,
SqliterError,
TableCreationError,
)
from sqliter.sqliter import SqliterDB
from tests.conftest import ExampleModel
class TestExceptions:
"""Test class for the custom exceptions in the sqliter module."""
def test_sqliter_error_with_template(self) -> None:
"""Test SqliterError formats the message correctly with a template."""
class CustomError(SqliterError):
message_template = "Custom error occurred with variable: '{}'"
exc = CustomError("test_variable")
assert (
str(exc) == "Custom error occurred with variable: 'test_variable'"
)
def test_sqliter_error_without_template(self) -> None:
"""Test that SqliterError uses the default message if no template."""
exc = SqliterError()
assert str(exc) == "An error occurred in the SQLiter package."
def test_database_connection_error(self, mocker: MockerFixture) -> None:
"""Test that DatabaseConnectionError is raised when connection fails."""
# Mock sqlite3.connect to raise an error
mocker.patch("sqlite3.connect", side_effect=sqlite3.Error)
# Create a SqliterDB instance
db = SqliterDB("fake_db.db")
# Assert that DatabaseConnectionError is raised when connecting
with pytest.raises(DatabaseConnectionError) as exc_info:
db.connect()
# Verify the exception message contains the database file name
assert "Failed to connect to the database: 'fake_db.db'" in str(
exc_info.value
)
# @pytest.mark.skip(reason="This is no longer a valid test case.")
def test_insert_duplicate_primary_key(self, db_mock: SqliterDB) -> None:
"""Test that exception raised when inserting duplicate primary key."""
# Create a model instance with a unique primary key
example_model = ExampleModel(
slug="test", name="Test License", content="Test Content"
)
# Insert the record for the first time, should succeed
result = db_mock.insert(example_model)
# Try inserting the same record again, which should raise our exception
with pytest.raises(RecordInsertionError) as exc_info:
db_mock.insert(result)
# Verify that the exception message contains the table name
assert "Failed to insert record into table: 'test_table'" in str(
exc_info.value
)
def test_create_table_error(
self, db_mock: SqliterDB, mocker: MockerFixture
) -> None:
"""Test exception is raised when creating table with invalid model."""
# Mock sqlite3.connect to raise an error
mocker.patch("sqliter.SqliterDB.connect", side_effect=sqlite3.Error)
# Try creating the table, which should raise an exception
with pytest.raises(TableCreationError) as exc_info:
db_mock.create_table(ExampleModel)
# Verify that the exception message contains the table name
assert "Failed to create the table: 'test_table'" in str(exc_info.value)
def test_update_not_found_error(self, db_mock: SqliterDB) -> None:
"""Test exception raised when updating a record that does not exist."""
# Create a model instance with a unique primary key
example_model = ExampleModel(
slug="test", name="Test License", content="Test Content"
)
# Try updating the record, which should raise an exception
with pytest.raises(RecordNotFoundError) as exc_info:
db_mock.update(example_model)
# Verify that the exception message contains the table name
assert "Failed to find that record in the table (key '0')" in str(
exc_info.value
)
def test_update_exception_error(
self, db_mock: SqliterDB, mocker: MockerFixture
) -> None:
"""Test an exception is raised when updating a record with an error."""
# Create a model instance with a unique primary key
example_model = ExampleModel(
slug="test", name="Test License", content="Test Content"
)
# Insert the record for the first time, should succeed
db_mock.insert(example_model)
# Mock sqlite3.connect to raise an error
mocker.patch("sqliter.SqliterDB.connect", side_effect=sqlite3.Error)
# Try updating the record, which should raise an exception
with pytest.raises(RecordUpdateError) as exc_info:
db_mock.update(example_model)
# Verify that the exception message contains the table name
assert "Failed to update record in table: 'test_table'" in str(
exc_info.value
)
def test_delete_exception_error(
self, db_mock: SqliterDB, mocker: MockerFixture
) -> None:
"""Test that exception raised when deleting a record with an error."""
# Create a model instance with a unique primary key
example_model = ExampleModel(
slug="test", name="Test License", content="Test Content"
)
# Insert the record for the first time, should succeed
db_mock.insert(example_model)
# Mock sqlite3.connect to raise an error
mocker.patch("sqliter.SqliterDB.connect", side_effect=sqlite3.Error)
# Try deleting the record, which should raise an exception
with pytest.raises(RecordDeletionError) as exc_info:
db_mock.delete(ExampleModel, "test")
# Verify that the exception message contains the table name
assert "Failed to delete record from table: 'test_table'" in str(
exc_info.value
)
def test_root_exception_with_no_traceback(self) -> None:
"""Test the root exception message."""
with pytest.raises(SqliterError) as exc:
raise SqliterError
assert str(exc.value) == "An error occurred in the SQLiter package."
def test_exception_with_no_traceback(self) -> None:
"""Test custom exception with an original exception but no traceback."""
def raise_original_exception() -> None:
"""Helper function to raise an original exception."""
err = "Original error"
raise ValueError(err)
def trigger_sqliter_error_with_no_traceback() -> None:
"""Helper function to trigger SqliterError with no traceback."""
try:
# Simulate an original exception
raise_original_exception()
except ValueError as original_exc:
# Manually clear the traceback from the exception
original_exc.__traceback__ = None # Remove the traceback
# Raise the custom exception and chain it
raise SqliterError from original_exc
# Simulate the exception chain with no traceback using pytest.raises
with pytest.raises(SqliterError) as exc_info:
trigger_sqliter_error_with_no_traceback()
# Access the raised exception instance
exc = exc_info.value
# Verify that the exception message contains "unknown location"
assert "unknown location" in str(exc)