|
| 1 | +import logging |
1 | 2 | from unittest.mock import MagicMock, patch |
2 | 3 |
|
3 | 4 | import pytest |
@@ -94,35 +95,34 @@ def test_apply_strikes_and_filter( |
94 | 95 | assert "HASH1" not in result |
95 | 96 |
|
96 | 97 |
|
97 | | -def test_log_change_logs_expected_strike_changes(): |
| 98 | +def test_log_change_logs_expected_strike_changes(caplog): |
98 | 99 | handler = StrikesHandler(job_name="remove_stalled", arr=MagicMock(), max_strikes=3) |
99 | 100 | handler.tracker = MagicMock() |
100 | 101 | handler.tracker.defective = { |
101 | 102 | "remove_stalled": { |
102 | | - "hash_new": {"title": "A", "strikes": 1}, # should show in added |
103 | | - "hash_inc": {"title": "B", "strikes": 2}, # should show in incremented |
| 103 | + "hash_new": {"title": "A", "strikes": 1}, # should show in added |
| 104 | + "hash_inc": {"title": "B", "strikes": 2}, # should show in incremented |
104 | 105 | "hash_paused": {"title": "C", "strikes": 2}, # should show in paused |
105 | 106 | } |
106 | 107 | } |
| 108 | + |
107 | 109 | recovered = ["hash_old"] |
108 | 110 | paused = ["hash_paused"] |
109 | 111 | affected = {"hash_gone": {"title": "Gone"}} |
110 | 112 |
|
111 | | - with patch("src.jobs.strikes_handler.logger") as mock_logger: |
| 113 | + with caplog.at_level(logging.DEBUG, logger="src.utils.log_setup"): |
112 | 114 | handler.log_change(recovered, paused, affected) |
113 | 115 |
|
114 | | - mock_logger.debug.assert_called_once() |
115 | | - args, _ = mock_logger.debug.call_args |
116 | | - |
117 | | - log_msg = args[0] |
118 | | - # Check keywords in the message string |
119 | | - for keyword in ["Added", "Incremented", "Recovered", "Removed", "Paused"]: |
120 | | - assert keyword in log_msg |
| 116 | + log_messages = "\n".join(record.message for record in caplog.records) |
121 | 117 |
|
122 | | - # Check keys in the entire call arguments (as string) |
123 | | - for key in ["hash_new", "hash_inc", "hash_old", "hash_gone", "hash_paused"]: |
124 | | - assert key in str(args) |
| 118 | + # Check category keywords exist |
| 119 | + for keyword in ["Added", "Incremented", "Recovered", "Removed", "Paused"]: |
| 120 | + assert keyword in log_messages |
125 | 121 |
|
| 122 | + # Check actual IDs exist |
| 123 | + for key in ["hash_new", "hash_inc", "hash_old", "hash_gone", "hash_paused"]: |
| 124 | + assert key in log_messages |
| 125 | + |
126 | 126 |
|
127 | 127 | @pytest.mark.parametrize( |
128 | 128 | "max_strikes, initial_strikes, expected_removed_after_two_runs", |
|
0 commit comments