|
|
from pathlib import Path |
|
|
from unittest.mock import MagicMock, patch |
|
|
|
|
|
from stroke_deepisles_demo.pipeline import run_pipeline_on_case |
|
|
|
|
|
|
|
|
def test_pipeline_cleanup_default(temp_dir: Path) -> None: |
|
|
"""Test that pipeline cleans up staging directory by default.""" |
|
|
|
|
|
dwi_file = temp_dir / "dwi.nii.gz" |
|
|
dwi_file.write_bytes(b"fake dwi") |
|
|
adc_file = temp_dir / "adc.nii.gz" |
|
|
adc_file.write_bytes(b"fake adc") |
|
|
|
|
|
|
|
|
with ( |
|
|
patch("stroke_deepisles_demo.pipeline.load_isles_dataset") as mock_load, |
|
|
patch("stroke_deepisles_demo.pipeline.stage_case_for_deepisles") as mock_stage, |
|
|
patch("stroke_deepisles_demo.pipeline.run_deepisles_on_folder") as mock_run, |
|
|
patch("stroke_deepisles_demo.pipeline.metrics.compute_dice"), |
|
|
patch("stroke_deepisles_demo.pipeline.shutil.rmtree") as mock_rmtree, |
|
|
): |
|
|
|
|
|
mock_dataset = MagicMock() |
|
|
mock_dataset.list_case_ids.return_value = ["case1"] |
|
|
|
|
|
mock_dataset.get_case.return_value = {"dwi": dwi_file, "adc": adc_file} |
|
|
|
|
|
|
|
|
mock_load.return_value.__enter__ = MagicMock(return_value=mock_dataset) |
|
|
mock_load.return_value.__exit__ = MagicMock(return_value=None) |
|
|
|
|
|
mock_staged = MagicMock() |
|
|
mock_staged.input_dir = Path("/tmp/mock_staging") |
|
|
mock_stage.return_value = mock_staged |
|
|
|
|
|
mock_result = MagicMock() |
|
|
mock_result.prediction_mask = Path("/tmp/results/pred.nii.gz") |
|
|
mock_run.return_value = mock_result |
|
|
|
|
|
|
|
|
run_pipeline_on_case("case1") |
|
|
|
|
|
|
|
|
assert mock_rmtree.called |
|
|
|
|
|
|
|
|
|
|
|
args, _ = mock_stage.call_args |
|
|
staging_root_passed = args[1] |
|
|
|
|
|
|
|
|
|
|
|
mock_rmtree.assert_called_with(staging_root_passed) |
|
|
|