-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_debug_logging.py
More file actions
383 lines (319 loc) · 14.7 KB
/
test_debug_logging.py
File metadata and controls
383 lines (319 loc) · 14.7 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""Tests the debug logging for the SqliterDB class."""
import logging
import pytest
from pytest_mock import MockerFixture
from sqliter.sqliter import SqliterDB
from tests.conftest import ComplexModel
class TestDebugLogging:
"""Test class for the debug logging in the SqliterDB class."""
def test_sqliterdb_debug_default(self) -> None:
"""Test that the default value for the debug flag is False."""
db = SqliterDB(":memory:") # No debug argument passed
assert db.debug is False, "The debug flag should be False by default."
def test_sqliterdb_debug_set_false(self) -> None:
"""Test that the default value for the debug flag is False."""
db = SqliterDB(":memory:", debug=False) # Set debug argument to False
assert db.debug is False, (
"The debug flag should be False when explicitly passed as False."
)
def test_sqliterdb_debug_set_true(self) -> None:
"""Test that the debug flag can be set to True."""
db = SqliterDB(":memory:", debug=True) # Set debug argument to True
assert db.debug is True, (
"The debug flag should be True when explicitly passed as True."
)
def test_debug_sql_output_basic_query(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the debug output correctly prints the SQL query and values."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).filter(
age=30.5
).fetch_all()
# Assert the SQL query was printed
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" WHERE "age" = 30.5'
in caplog.text
)
def test_debug_sql_output_string_values(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that the debug output correctly handles string values."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).filter(
name="Alice"
).fetch_all()
# Assert the SQL query was printed with the string properly quoted
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" WHERE "name" = \'Alice\''
in caplog.text
)
def test_debug_sql_output_multiple_conditions(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that the debug output works with multiple conditions."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).filter(
name="Alice", age=30.5
).fetch_all()
# Assert the SQL query was printed with multiple conditions
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" WHERE "name" = \'Alice\' '
'AND "age" = 30.5' in caplog.text
)
def test_debug_sql_output_order_and_limit(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that the debug output works with order and limit."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).order(
"age", reverse=True
).limit(1).fetch_all()
# Assert the SQL query was printed with ORDER and LIMIT
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" ORDER BY "age" DESC LIMIT 1'
in caplog.text
)
def test_debug_sql_output_with_null_value(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that the debug output works when filtering on a NULL value."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.insert(
ComplexModel(
pk=4,
name="David",
age=40.0,
is_active=True,
score=80.0,
nullable_field=None,
)
)
db_mock_complex_debug.select(ComplexModel).filter(
age__isnull=True
).fetch_all()
# Assert the SQL query was printed with IS NULL
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" WHERE "age" IS NULL'
in caplog.text
)
def test_debug_sql_output_with_fields_single(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test debug output correct when selecting a single field."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).fields(
["name"]
).fetch_all()
# Assert the SQL query only selects the 'name' field
assert (
'Executing SQL: SELECT "name", "pk" FROM "complex_model"'
in caplog.text
)
def test_debug_sql_output_with_fields_multiple(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that the debug output correct when selecting multiple fields."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).fields(
["name", "age"]
).fetch_all()
# Assert the SQL query only selects the 'name' and 'age' fields
assert (
'Executing SQL: SELECT "name", "age", "pk" FROM "complex_model"'
in caplog.text
)
def test_debug_sql_output_with_fields_and_filter(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the debug output correct with selected fields and a filter."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).fields(
["name", "score"]
).filter(score__gt=85).fetch_all()
# Assert the SQL query selects 'name' and 'score' and applies the filter
assert (
'Executing SQL: SELECT "name", "score", "pk" FROM "complex_model" '
'WHERE "score" > 85' in caplog.text
)
def test_no_log_output_when_debug_false(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that no log output occurs when debug=False."""
db = SqliterDB(":memory:", debug=False)
db.create_table(ComplexModel)
with caplog.at_level(logging.DEBUG):
db.select(ComplexModel).filter(age=30.5).fetch_all()
# Assert that there is no log output
assert caplog.text == ""
def test_no_log_output_above_debug_level(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test no DEBUG log output occurs when log level is above DEBUG."""
with caplog.at_level(logging.INFO): # Set log level higher than DEBUG
db_mock_complex_debug.select(ComplexModel).filter(
age=30.5
).fetch_all()
# Assert that no DEBUG messages are present in the logs
assert caplog.text == ""
def test_manual_logger_respects_debug_flag(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that a manually passed logger respects the debug flag."""
custom_logger = logging.getLogger("CustomLogger")
custom_logger.setLevel(logging.DEBUG)
db = SqliterDB(":memory:", debug=True, logger=custom_logger)
assert db.logger is custom_logger
db.create_table(ComplexModel)
with caplog.at_level(logging.DEBUG):
db.select(ComplexModel).filter(age=30.5).fetch_all()
# Assert that log output was captured with the manually passed logger
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", ' in caplog.text
)
def test_manual_logger_above_debug_level(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""Ensure no log output when manually passed logger is above DEBUG."""
custom_logger = logging.getLogger("CustomLogger")
custom_logger.setLevel(logging.INFO) # Set log level higher than DEBUG
db = SqliterDB(":memory:", debug=True, logger=custom_logger)
db.create_table(ComplexModel)
with caplog.at_level(logging.INFO): # Use caplog at INFO level
db.select(ComplexModel).filter(age=30.5).fetch_all()
# Assert that no DEBUG messages were logged
assert caplog.text == ""
def test_debug_sql_output_no_matching_records(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the debug output occurs even when no records match the query."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).filter(
age=100
).fetch_all() # No records with age=100
# Assert that the SQL query was logged despite no matching records
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model" WHERE "age" = 100'
in caplog.text
)
def test_debug_sql_output_empty_query(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test debug output occurs for empty query (no filters, etc)."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.select(ComplexModel).fetch_all()
# Assert that the SQL query was logged for a full table scan
assert (
'Executing SQL: SELECT "pk", "created_at", "updated_at", "name", '
'"age", "is_active", "score", '
'"nullable_field" FROM "complex_model"' in caplog.text
)
def test_debug_output_drop_table(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test debug output when dropping a table."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.drop_table(ComplexModel)
# Assert the SQL query for dropping the table was logged
assert (
'Executing SQL: DROP TABLE IF EXISTS "complex_model"' in caplog.text
)
def test_reset_database_debug_logging(
self, temp_db_path: str, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that resetting the database logs debug information."""
with caplog.at_level(logging.DEBUG):
SqliterDB(temp_db_path, reset=True, debug=True)
assert "Database reset: 0 user-created tables dropped." in caplog.text
def test_debug_output_insert(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that insert operations produce debug log output."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.insert(
ComplexModel(
name="DebugTest",
age=25.0,
is_active=True,
score=90.0,
nullable_field=None,
)
)
assert "Executing SQL:" in caplog.text
assert 'INSERT INTO "complex_model"' in caplog.text
def test_debug_output_get(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that get operations produce debug log output."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.get(ComplexModel, 1)
assert "Executing SQL:" in caplog.text
assert "SELECT" in caplog.text
assert "complex_model" in caplog.text
def test_debug_output_update(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that update operations produce debug log output."""
record = db_mock_complex_debug.get(ComplexModel, 1)
assert record is not None
record.name = "Updated"
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.update(record)
assert "Executing SQL:" in caplog.text
assert 'UPDATE "complex_model"' in caplog.text
def test_debug_output_delete(
self, db_mock_complex_debug: SqliterDB, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that delete operations produce debug log output."""
with caplog.at_level(logging.DEBUG):
db_mock_complex_debug.delete(ComplexModel, 1)
assert "Executing SQL:" in caplog.text
assert 'DELETE FROM "complex_model"' in caplog.text
def test_debug_output_table_names(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that table_names property produces debug log output."""
db = SqliterDB(":memory:", debug=True)
db.create_table(ComplexModel)
with caplog.at_level(logging.DEBUG):
_ = db.table_names
assert "Executing SQL:" in caplog.text
assert "sqlite_master" in caplog.text
def test_setup_logger_else_clause(self, mocker: MockerFixture) -> None:
"""Test the else clause configuration for the logger setup."""
# Mock the root logger's hasHandlers BEFORE creating the instance
has_handlers_mock = mocker.patch.object(
logging.getLogger(), "hasHandlers", return_value=False
)
# Now create the instance which will trigger _setup_logger
instance = SqliterDB(":memory:", debug=True)
# Verify the else clause configuration
logger = instance.logger
assert logger is not None
assert logger.name == "sqliter"
assert len(logger.handlers) == 1
handler = logger.handlers[0]
assert isinstance(handler, logging.StreamHandler)
assert handler.formatter is not None
assert handler.formatter._fmt == "%(levelname)-8s%(message)s"
assert logger.level == logging.DEBUG
assert logger.propagate is False
has_handlers_mock.assert_called_once()
# Cleanup - crucial to prevent test pollution
for hdlr in logger.handlers[:]:
logger.removeHandler(hdlr)
logger.setLevel(logging.NOTSET)
logger.propagate = True