Source code for ewoksxrdct.tests.test_integrate
from unittest.mock import Mock, patch
import pytest
from ..tasks.integrate import Integrate, IntegrateInputModel, IntegrateOutputModel
[docs]
class FakeDataset:
"""Mock dataset with a fname attribute."""
fname: str
[docs]
def test_output_model_valid(tmp_path):
"""Output model should accept valid fields."""
output_dir = tmp_path / "output"
model = IntegrateOutputModel(
processed_datasets=["dataset1", "dataset2"],
integration_output_path=[
str(output_dir / "dataset1.h5"),
str(output_dir / "dataset2.h5"),
],
)
assert model.processed_datasets == ["dataset1", "dataset2"]
assert model.integration_output_path == [
str(output_dir / "dataset1.h5"),
str(output_dir / "dataset2.h5"),
]
[docs]
def test_output_model_missing_fields():
"""Output model should raise an error for missing required fields."""
with pytest.raises(Exception):
IntegrateOutputModel()
with pytest.raises(Exception):
IntegrateOutputModel(integration_output_path=["output1"])
[docs]
@patch("ewoksxrdct.tasks.integrate.get_distributed_integrator_params")
def test_run_raises_on_empty_datasets(mock_params, tmp_path):
"""No datasets found → raises RuntimeError."""
mock_params.return_value = (None, None, None, [], [])
task = Integrate(inputs={"config_path": str(tmp_path / "config.conf")})
with pytest.raises(RuntimeError, match="No datasets found"):
task.run()
[docs]
@patch("ewoksxrdct.tasks.integrate.get_distributed_integrator_params")
def test_run_raises_on_parse_error(mock_params, tmp_path):
"""Config parse error → raises RuntimeError."""
mock_params.side_effect = ValueError("bad config")
task = Integrate(inputs={"config_path": str(tmp_path / "config.conf")})
with pytest.raises(RuntimeError, match="Failed to parse"):
task.run()
[docs]
@patch("ewoksxrdct.tasks.integrate.Path.exists")
@patch("ewoksxrdct.tasks.integrate.integrate_mp_cli")
@patch("ewoksxrdct.tasks.integrate.get_distributed_integrator_params")
def test_run_skips_integration_when_datasets_previously_processed(
mock_params, mock_cli, mock_exists, tmp_path
):
"""All datasets have already been processed → integrator not called."""
mock_dataset = Mock(spec=FakeDataset)
mock_dataset.fname = "dataset1"
mock_params.return_value = (None, None, None, [mock_dataset], ["output.h5"])
mock_exists.return_value = True # processed datasets exist
task = Integrate(inputs={"config_path": str(tmp_path / "config.conf")})
task.run()
mock_cli.assert_not_called()
assert task.outputs.processed_datasets == ["dataset1"]
assert task.outputs.integration_output_path == ["output.h5"]
[docs]
@patch("ewoksxrdct.tasks.integrate.Path.exists")
@patch("ewoksxrdct.tasks.integrate.integrate_mp_cli")
@patch("ewoksxrdct.tasks.integrate.get_distributed_integrator_params")
def test_run_integrates_when_datasets_not_yet_processed(
mock_params, mock_cli, mock_exists, tmp_path
):
"""Datasets not yet processed → integrator called and outputs set."""
mock_dataset = Mock(spec=FakeDataset)
mock_dataset.fname = "dataset1"
mock_params.return_value = (None, None, None, [mock_dataset], ["output.h5"])
mock_exists.return_value = False # processed datasets are missing
task = Integrate(inputs={"config_path": str(tmp_path / "config.conf")})
task.run()
mock_cli.assert_called_once()
assert task.outputs.processed_datasets == ["dataset1"]
assert task.outputs.integration_output_path == ["output.h5"]