From 6e2023cefa07e08ec16895cea72f5a490d933071 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Wed, 22 Apr 2026 16:59:30 +0200 Subject: [PATCH] tests: added more tests, pytest ini options in pyporject toml --- pyproject.toml | 8 ++++++++ .../integration/daq/test_database_connection.py | 5 +++++ .../common/test_data_collection_parameters.py | 17 ++++++++++++++++- tests/unit/common/test_mlbox_model.py | 4 ++++ .../common/test_zoom_model_camera_settings.py | 12 ++++++++++++ tests/unit/daq/test_mlbox_logic.py | 15 ++++++++++++++- tests/unit/gui/test_prediction_subscriber.py | 9 --------- 7 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 tests/integration/daq/test_database_connection.py diff --git a/pyproject.toml b/pyproject.toml index e4529c7e..be180e69 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,14 @@ aaredb = { index = "psi"} name = "psi" url = "https://gitea.psi.ch/api/packages/mx/pypi/simple" +[tool.pytest.ini_options] +testpaths = ["tests"] +python_files = ["test_*.py"] +norecursedirs = ["scripts", ".venv", "logs", "docs"] +markers = [ + "integration: tests that require external systems or secrets", +] + [build-system] requires = ["setuptools>=75.6.0"] build-backend = "setuptools.build_meta" diff --git a/tests/integration/daq/test_database_connection.py b/tests/integration/daq/test_database_connection.py new file mode 100644 index 00000000..5920e66f --- /dev/null +++ b/tests/integration/daq/test_database_connection.py @@ -0,0 +1,5 @@ +import pytest + +@pytest.mark.integration +def test_real_db_connection(): + pass \ No newline at end of file diff --git a/tests/unit/common/test_data_collection_parameters.py b/tests/unit/common/test_data_collection_parameters.py index 80cc8f7c..9702e21f 100644 --- a/tests/unit/common/test_data_collection_parameters.py +++ b/tests/unit/common/test_data_collection_parameters.py @@ -6,7 +6,8 @@ from aare.common.models import DataCollectionParameters def test_directory_defaults_when_missing(): params = DataCollectionParameters() - assert params.directory == "{date}/{prefix}" + assert params.directory == None + #TODO What should the defaults be? Do we want it to be "{date}/{prefix}"? def test_directory_spaces_are_replaced(): @@ -27,3 +28,17 @@ def test_exposure_must_be_between_0_and_1(): def test_cloud_blank_defaults_to_true(): params = DataCollectionParameters(cloud="") assert params.cloud is True + +def test_directory_accepts_valid_macros(): + params = DataCollectionParameters(directory="{date}/{prefix}/run") + assert params.directory == "{date}/{prefix}/run" + + +def test_aperture_accepts_float_string(): + params = DataCollectionParameters(aperture="2.0") + assert params.aperture == 2 + + +def test_processingpipeline_rejects_unknown_value(): + with pytest.raises(ValidationError): + DataCollectionParameters(processingpipeline="xia2") \ No newline at end of file diff --git a/tests/unit/common/test_mlbox_model.py b/tests/unit/common/test_mlbox_model.py index 03788792..3db87cc6 100644 --- a/tests/unit/common/test_mlbox_model.py +++ b/tests/unit/common/test_mlbox_model.py @@ -19,3 +19,7 @@ def test_get_best_for_class_returns_highest_confidence(): assert best is not None assert best.conf == 0.7 + +def test_get_best_for_class_returns_none_when_missing(): + model = MLOutputModel() + assert model.get_best_for_class(MLBoxType.Crystal) is None \ No newline at end of file diff --git a/tests/unit/common/test_zoom_model_camera_settings.py b/tests/unit/common/test_zoom_model_camera_settings.py index 67eba498..b02f9fdd 100644 --- a/tests/unit/common/test_zoom_model_camera_settings.py +++ b/tests/unit/common/test_zoom_model_camera_settings.py @@ -33,3 +33,15 @@ def test_get_camera_settings_raises_when_empty(): model = ZoomModel(z={}) with pytest.raises(ValueError): model.get_camera_settings(100) + +def test_get_camera_settings_below_minimum_returns_first(): + model = ZoomModel( + z={ + 100: SampleCameraSettings(gain=1.0, exposure=0.1), + 200: SampleCameraSettings(gain=2.0, exposure=0.2), + } + ) + + settings = model.get_camera_settings(50) + assert settings.gain == 1.0 + assert settings.exposure == 0.1 \ No newline at end of file diff --git a/tests/unit/daq/test_mlbox_logic.py b/tests/unit/daq/test_mlbox_logic.py index f0cd0450..6f2c23c7 100644 --- a/tests/unit/daq/test_mlbox_logic.py +++ b/tests/unit/daq/test_mlbox_logic.py @@ -24,4 +24,17 @@ def test_prediction_score_uses_count_and_max_confidence(): model.add_box(MLBoxType.Pin, (0, 0, 10, 10), 0.4) model.add_box(MLBoxType.Crystal, (1, 1, 9, 9), 0.8) - assert MlBox._prediction_score(model) == (2, 0.8) \ No newline at end of file + assert MlBox._prediction_score(model) == (2, 0.8) + +def test_best_by_class_keeps_highest_confidence_per_class(): + results = [ + {"class": 1, "confidence": 0.4, "box": {"x1": 0, "y1": 0, "x2": 10, "y2": 10}}, + {"class": 1, "confidence": 0.8, "box": {"x1": 1, "y1": 1, "x2": 11, "y2": 11}}, + {"class": 2, "confidence": 0.6, "box": {"x1": 2, "y1": 2, "x2": 12, "y2": 12}}, + ] + + out = MlBox._best_by_class(results) + + assert out is not None + assert out.get_best_for_class(MLBoxType.Pin).conf == 0.8 + assert out.get_best_for_class(MLBoxType.Crystal).conf == 0.6 diff --git a/tests/unit/gui/test_prediction_subscriber.py b/tests/unit/gui/test_prediction_subscriber.py index 39bd879b..2e135470 100644 --- a/tests/unit/gui/test_prediction_subscriber.py +++ b/tests/unit/gui/test_prediction_subscriber.py @@ -1,11 +1,2 @@ import sys import pytest -from PySide6.QtWidgets import QApplication - - -@pytest.fixture(scope="session") -def qapp(): - app = QApplication.instance() - if app is None: - app = QApplication(sys.argv) - return app