from unittest.mock import MagicMock, patch import pytest from control_backend.logging.dated_file_handler import DatedFileHandler @patch("control_backend.logging.dated_file_handler.DatedFileHandler._open") def test_reset(open_): stream = MagicMock() open_.return_value = stream # A file should be opened when the logger is created handler = DatedFileHandler(file_prefix="anything") assert open_.call_count == 1 # Upon reset, the current file should be closed, and a new one should be opened handler.do_rollover() assert stream.close.call_count == 1 assert open_.call_count == 2 @patch("control_backend.logging.dated_file_handler.Path") @patch("control_backend.logging.dated_file_handler.DatedFileHandler._open") def test_creates_dir(open_, Path_): stream = MagicMock() open_.return_value = stream test_path = MagicMock() test_path.parent.is_dir.return_value = False Path_.return_value = test_path DatedFileHandler(file_prefix="anything") # The directory should've been created test_path.parent.mkdir.assert_called_once() @patch("control_backend.logging.dated_file_handler.DatedFileHandler._open") def test_invalid_constructor(_): with pytest.raises(ValueError): DatedFileHandler(file_prefix=None) with pytest.raises(ValueError): DatedFileHandler(file_prefix="")