Skip to content

Commit 3f7fcf6

Browse files
Copilotalvarolopez
andcommitted
Fix flake8 and black code formatting issues
Co-authored-by: alvarolopez <[email protected]>
1 parent 7cf3412 commit 3f7fcf6

File tree

2 files changed

+180
-114
lines changed

2 files changed

+180
-114
lines changed

caso/tests/extract/test_manager.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
from unittest import mock
2626

2727
from caso.extract import manager
28-
import caso.manager # Import to register the spooldir option
2928

3029
CONF = cfg.CONF
30+
CONF.register_opts(
31+
[cfg.StrOpt("spooldir", default="/var/spool/caso", help="Spool directory.")]
32+
)
3133

3234

3335
class TestCasoManager(unittest.TestCase):
@@ -152,58 +154,68 @@ def test_lastrun_is_invalid(self):
152154
def test_get_records_with_invalid_lastrun_continues(self):
153155
"""Test that get_records continues when lastrun file is invalid."""
154156
self.flags(projects=["project1", "project2"])
155-
157+
156158
# Mock the file system operations to test exception handling path
157-
with unittest.mock.patch.object(self.manager, "get_project_vo", return_value="test-vo") as m_vo:
158-
with unittest.mock.patch('os.path.exists', return_value=True):
159+
with unittest.mock.patch.object(
160+
self.manager, "get_project_vo", return_value="test-vo"
161+
) as m_vo:
162+
with unittest.mock.patch("os.path.exists", return_value=True):
159163
# One project gets invalid date, one gets valid date
160164
file_contents = ["invalid-date-format", "2020-01-01 00:00:00"]
161165
mock_file = unittest.mock.mock_open()
162166
mock_file.return_value.read.side_effect = file_contents
163-
164-
with unittest.mock.patch('builtins.open', mock_file):
165-
ret = self.manager.get_records()
166-
167-
# Should call get_project_vo for both projects since VO lookup happens before lastrun
167+
168+
with unittest.mock.patch("builtins.open", mock_file):
169+
self.manager.get_records()
170+
171+
# Should call get_project_vo for both projects since VO
172+
# lookup happens before lastrun
168173
self.assertEqual(m_vo.call_count, 2)
169174
m_vo.assert_any_call("project1")
170175
m_vo.assert_any_call("project2")
171-
172-
# One project should succeed - doesn't matter which one for this test
173-
# The key is that get_records continues processing despite one project failing
176+
177+
# One project should succeed - doesn't matter which one
178+
# The key is that get_records continues processing
179+
# despite one project failing
174180
self.m_extractor.assert_called_once()
175181
args, kwargs = self.m_extractor.call_args
176182
self.assertEqual(args[1], "test-vo") # VO should be correct
177-
self.assertIn(args[0], ["project1", "project2"]) # Should be one of the projects
183+
# Should be one of the projects
184+
self.assertIn(args[0], ["project1", "project2"])
178185

179186
def test_get_records_with_future_extract_from_continues(self):
180187
"""Test that get_records continues when extract_from is in the future."""
181188
self.flags(projects=["project1", "project2"])
182189
future_date = datetime.datetime.now(tz.tzutc()) + datetime.timedelta(days=1)
183190
past_date = datetime.datetime.now(tz.tzutc()) - datetime.timedelta(days=1)
184-
191+
185192
# Mock file system operations and return dates
186-
with unittest.mock.patch.object(self.manager, "get_project_vo", return_value="test-vo") as m_vo:
187-
with unittest.mock.patch('os.path.exists', return_value=True):
193+
with unittest.mock.patch.object(
194+
self.manager, "get_project_vo", return_value="test-vo"
195+
) as m_vo:
196+
with unittest.mock.patch("os.path.exists", return_value=True):
188197
# One project gets future date, one gets past date
189198
file_contents = [str(future_date), str(past_date)]
190199
mock_file = unittest.mock.mock_open()
191200
mock_file.return_value.read.side_effect = file_contents
192-
193-
with unittest.mock.patch('builtins.open', mock_file):
194-
ret = self.manager.get_records()
195-
196-
# Should call get_project_vo for both projects since VO lookup happens before lastrun
201+
202+
with unittest.mock.patch("builtins.open", mock_file):
203+
self.manager.get_records()
204+
205+
# Should call get_project_vo for both projects since VO
206+
# lookup happens before lastrun
197207
self.assertEqual(m_vo.call_count, 2)
198208
m_vo.assert_any_call("project1")
199209
m_vo.assert_any_call("project2")
200-
201-
# One project should succeed - doesn't matter which one for this test
202-
# The key is that get_records continues processing despite one project failing
210+
211+
# One project should succeed - doesn't matter which one
212+
# The key is that get_records continues processing
213+
# despite one project failing
203214
self.m_extractor.assert_called_once()
204215
args, kwargs = self.m_extractor.call_args
205216
self.assertEqual(args[1], "test-vo") # VO should be correct
206-
self.assertIn(args[0], ["project1", "project2"]) # Should be one of the projects
217+
# Should be one of the projects
218+
self.assertIn(args[0], ["project1", "project2"])
207219

208220
def test_get_lastrun_invalid_date_logs_exception(self):
209221
"""Test that get_lastrun properly logs exceptions when date parsing fails."""
@@ -212,19 +224,19 @@ def test_get_lastrun_invalid_date_logs_exception(self):
212224
else:
213225
builtins_open = "__builtin__.open"
214226
fopen = unittest.mock.mock_open(read_data="invalid-date-format")
215-
227+
216228
with unittest.mock.patch("os.path.exists") as path:
217229
with unittest.mock.patch(builtins_open, fopen):
218230
with unittest.mock.patch("caso.extract.manager.LOG") as mock_log:
219231
path.return_value = True
220-
232+
221233
with self.assertRaises(ValueError):
222234
self.manager.get_lastrun("test-project")
223-
235+
224236
# Verify that both error and exception were logged
225237
mock_log.error.assert_called_once()
226238
mock_log.exception.assert_called_once()
227-
239+
228240
# Check the error message contains the expected text
229241
error_call = mock_log.error.call_args[0][0]
230242
self.assertIn("Cannot read date from lastrun file", error_call)

0 commit comments

Comments
 (0)