Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ibex_bluesky_core/callbacks/file_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ def start(self, doc: RunStart) -> None:
)
header_data[START_TIME] = formatted_time

if not self.filename.parent.exists():
self.filename.parent.mkdir()

with open(self.filename, "a", newline="", encoding="utf-8") as outfile:
for key, value in header_data.items():
outfile.write(f"{key}: {value}\n")
outfile.writelines([f"{key}: {value}\n" for key, value in header_data.items()])

logger.debug("successfully wrote header in %s", self.filename)

Expand Down
42 changes: 37 additions & 5 deletions tests/callbacks/test_write_log_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,56 @@ def test_header_data_all_available_on_start(cb):
)

mock_file.assert_called_with(result, "a", newline="", encoding="utf-8")
writelines_call_args = mock_file().writelines.call_args[0][0]
# time should have been renamed to start_time and converted to human readable
mock_file().write.assert_any_call("start_time: 2024-10-04 14:43:43\n")
mock_file().write.assert_any_call(f"uid: {uid}\n")
assert "start_time: 2024-10-04 14:43:43\n" in writelines_call_args
assert f"uid: {uid}\n" in writelines_call_args


def test_no_rb_number_folder(cb):
time = 1728049423.5860472
uid = "test123"
scan_id = 1234
run_start = RunStart(time=time, uid=uid, scan_id=scan_id)
with patch("ibex_bluesky_core.callbacks.file_logger.open", mock_open()) as mock_file:

with (
patch("ibex_bluesky_core.callbacks.file_logger.open", mock_open()) as mock_file,
patch.object(Path, "exists") as mock_exists,
patch.object(Path, "mkdir") as mock_mkdir,
):
mock_exists.return_value = False
cb.start(run_start)
result = save_path / "Unknown RB" / f"{node()}_block_dae_2024-10-04_14-43-43Z.txt"
assert mock_mkdir.called

mock_file.assert_called_with(result, "a", newline="", encoding="utf-8")
# time should have been renamed to start_time and converted to human readable
writelines_call_args = mock_file().writelines.call_args[0][0]
assert "start_time: 2024-10-04 14:43:43\n" in writelines_call_args
assert f"uid: {uid}\n" in writelines_call_args


def test_rb_number_folder_exists(cb):
time = 1728049424.5860472
uid = "test432"
scan_id = 4321
run_start = RunStart(time=time, uid=uid, scan_id=scan_id)

with (
patch("ibex_bluesky_core.callbacks.file_logger.open", mock_open()) as mock_file,
patch.object(Path, "exists") as mock_exists,
patch.object(Path, "mkdir") as mock_mkdir,
):
mock_exists.return_value = True
cb.start(run_start)
result = save_path / "Unknown RB" / f"{node()}_block_dae_2024-10-04_14-43-44Z.txt"
assert mock_mkdir.called

mock_file.assert_called_with(result, "a", newline="", encoding="utf-8")
# time should have been renamed to start_time and converted to human readable
mock_file().write.assert_any_call("start_time: 2024-10-04 14:43:43\n")
mock_file().write.assert_any_call(f"uid: {uid}\n")
writelines_call_args = mock_file().writelines.call_args[0][0]
assert "start_time: 2024-10-04 14:43:44\n" in writelines_call_args
assert f"uid: {uid}\n" in writelines_call_args


def test_descriptor_data_does_nothing_if_doc_not_called_primary(cb):
Expand Down
Loading