add graceful error handling with return of status codes, closes #12

This commit is contained in:
2025-11-10 15:27:30 +01:00
parent 08f70ee672
commit 4a94815bfe
4 changed files with 2610 additions and 2853 deletions

View File

@@ -1,23 +1,61 @@
import shutil
from io import StringIO
from unittest.mock import patch
import pytest
from dopt_basics.result_pattern import wrap_result
from dopt_sensor_anomalies import _interface, constants, errors
from dopt_sensor_anomalies import _interface, constants
def test_print_error_state_WrongState():
from dopt_basics.result_pattern import STATUS_HANDLER
MSG = "to treat state as error"
with pytest.raises(RuntimeError, match=MSG):
_interface._print_error_state(STATUS_HANDLER.SUCCESS, out_stream=StringIO())
def test_print_error_state(tmp_path):
@wrap_result(100)
def error_func() -> None:
# do something
raise RuntimeError("Oops, error occurred")
err_state = error_func().status
output_file = tmp_path / "t_output.txt"
output_file.touch()
with open(output_file, "w") as stream:
_interface._print_error_state(err_state, stream)
lines: list[str]
with open(output_file, "r") as file:
lines = file.readlines()
assert "following exception" in lines[0]
assert "Type: RuntimeError" in lines[1]
assert "Description:" in lines[2]
assert "Message: Oops, error occurred" in lines[3]
@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib")
def test_sensor_anomalies_detection_FailImagePath(setup_temp_dir):
img_path = str(setup_temp_dir / "not-existing.bmp")
pixels_per_metric_X: float = 0.251
pixels_per_metric_Y: float = 0.251
MESSAGE = "The provided path seems not to exist"
with pytest.raises(FileNotFoundError, match=MESSAGE):
_interface.sensor_anomalies_detection(
with patch("sys.stderr", new_callable=StringIO) as mock_err:
ret = _interface.sensor_anomalies_detection(
img_path, pixels_per_metric_X, pixels_per_metric_Y
)
captured = mock_err.getvalue()
assert ret != 0
assert "FileNotFoundError" in captured
assert MESSAGE in captured
@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib")
@@ -28,10 +66,14 @@ def test_sensor_anomalies_detection_FailElectrodeCount(path_img_with_failure_Ele
MESSAGE = "Number of counted electrodes does not match the"
with pytest.raises(errors.InvalidElectrodeCount, match=MESSAGE):
_interface.sensor_anomalies_detection(
with patch("sys.stderr", new_callable=StringIO) as mock_err:
ret = _interface.sensor_anomalies_detection(
img_path, pixels_per_metric_X, pixels_per_metric_Y
)
captured = mock_err.getvalue()
assert ret != 0
assert "InvalidElectrodeCount" in captured
assert MESSAGE in captured
@patch("dopt_sensor_anomalies._find_paths.STOP_FOLDER_NAME", "lib")
@@ -52,8 +94,11 @@ def test_sensor_anomalies_detection_Success(
pixels_per_metric_X: float = 0.251
pixels_per_metric_Y: float = 0.251
_interface.sensor_anomalies_detection(img_path, pixels_per_metric_X, pixels_per_metric_Y)
ret = _interface.sensor_anomalies_detection(
img_path, pixels_per_metric_X, pixels_per_metric_Y
)
assert ret == 0
assert csv_file.exists()
assert heatmap_file.exists()
target_folder = results_folder / "csharp_interface"