From 864f195c710edfa12ec801c65289253bedf66726 Mon Sep 17 00:00:00 2001 From: appleb_m Date: Fri, 24 Apr 2026 11:01:39 +0200 Subject: [PATCH] tests: added tests for coordinate and models --- CHANGELOG.md | 27 +++++- tests/unit/common/test_coordinate.py | 126 +++++++++++++++++++++++++ tests/unit/common/test_models_extra.py | 100 ++++++++++++++++++++ 3 files changed, 251 insertions(+), 2 deletions(-) create mode 100644 tests/unit/common/test_coordinate.py create mode 100644 tests/unit/common/test_models_extra.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a5cbdc8..1e0f700e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog -## 0.3.0 +## 0.3.0 (2026-04-24) - Merge three repos into one, remove Ultralytics as dependency +- **Testing & CI/CD**: + - Implemented comprehensive `pytest` infrastructure for unit and integration tests. + - Integrated `pytest-cov` and `pytest-mock` into the testing workflow with 100% coverage on core modules like `coordinate` and `exception_handler`. + - Added automated testing stage to Gitea workflow (`.gitea/workflows/action.yaml`) ensuring tests pass before build and documentation updates. + - Added `pytest-qt` support for GUI testing. - **DAQ/Server**: - Added lifespan function for one-time initialization of core objects (devices, daq, config) on startup, reducing conflicting Redis and device connections. - Big Aerotech upgrade: transitioned to Aerotech API with ported coordinate functions. @@ -16,6 +21,12 @@ - Improved database message limits for omega (to 2 decimal places). - Added screenshot to database from GUI; face detection sequence and beamline recovery updates. - Throttled `UserRightsException` logging to reduce log noise. + - Implemented persistent automation progress tracking using Redis across multiple workers. + - Added automation progress panel to the GUI for real-time monitoring. + - Improved raster and rotation scan error handling and centralized database message handling. + - Updated `server_exception_handler.py` and `error_codes.py` to cover all custom exceptions. + - Added `auto_exposure` endpoint and improved `autofocus` logic. + - Fixed multiple bugs in raster scan positioning and state transitions. - **Baton & Sessions**: - Major rework of the baton exchange procedure and session management. - Added `NOT_BATON_HOLDER` error code and specialized handling for baton requests. @@ -34,7 +45,12 @@ - New screenshot button and controls in sample camera GUI. - Added SSE support for face detection panel updates during automation. - Integrated sample position resync in recovery panel to reduce Tell polling. - - Fixed matplotlib warnings in face detection panel and improved layout persistence. + - Fixed matplotlib warnings in face detection panel and improved layout persistence. + - Added `beamline_state_panel` with station widgets for improved state management and visibility. + - Performance optimizations: throttled raster grid drawing to 25Hz and improved dense heat map rendering. + - Added automation progress panel and wired it to the persistent Redis backend. + - Improved prediction subscriber performance and target stability visualization. + - Added shortcuts and improved user feedback in the status bar using `BeamlineStateEnum` display names. - **TELL & Infrastructure**: - Added mTLS certificate authentication for `aaredb`, `tellupdater`, and `spreadsheetupdater`. - Improved SSE client resilience in `tellupdater` with retry mechanisms and enhanced error logging. @@ -43,6 +59,9 @@ - Added error handling and proxy support for connection failures. - Fixed event names for mounting, and added missing events for gripper detection and motion sync. - Refactored SSE connection handling for simplicity and robustness. + - Added control for pin blower and annealing (in/out) to `TellClient`. + - Improved TELL HTTP communication handling to prevent swallowed errors. + - Added magnet position sensor state checks and exception handling. - **Common & Devices**: - Added BEC worker and Aerotech controller initialization in `devices.py`. - Added magnet position sensor functions and centralized transmission settings. @@ -52,6 +71,10 @@ - Improved Smargon connection exception handling. - Updated ABR_POS_HOME configuration and added comments for cryo positions. - Added image upload comments and improved authentication error messaging. + - Centralized custom exceptions in `aare.common.exception_handler`. + - Added `display_name` to `BeamlineStateEnum` for consistent UI strings. + - Refactored `SampleShortInfo` handling across DAQ and database modules. + - Multiple bug fixes in ML box prediction logic and coordinate transformations. - **Infrastructure**: - Updated dependencies (added `pshell`) and improved documentation autodeployment workflows. - Refactored package structure and imports. diff --git a/tests/unit/common/test_coordinate.py b/tests/unit/common/test_coordinate.py new file mode 100644 index 00000000..4f8062aa --- /dev/null +++ b/tests/unit/common/test_coordinate.py @@ -0,0 +1,126 @@ +import pytest +import numpy as np +from aare.common.coordinate import Coordinate, SmargonCoordinate, AerotechCoordinate, positive_coords + +def test_coordinate_addition(): + c1 = Coordinate(x=1, y=2, z=3) + c2 = Coordinate(x=4, y=5, z=6) + c3 = c1 + c2 + assert c3.x == 5 + assert c3.y == 7 + assert c3.z == 9 + +def test_coordinate_subtraction(): + c1 = Coordinate(x=5, y=7, z=9) + c2 = Coordinate(x=1, y=2, z=3) + c3 = c1 - c2 + assert c3.x == 4 + assert c3.y == 5 + assert c3.z == 6 + +def test_coordinate_multiplication_scalar(): + c1 = Coordinate(x=1, y=2, z=3) + c2 = c1 * 2.0 + assert c2.x == 2.0 + assert c2.y == 4.0 + assert c2.z == 6.0 + +def test_coordinate_dot_product(): + c1 = Coordinate(x=1, y=2, z=3) + c2 = Coordinate(x=4, y=5, z=6) + dot = c1 * c2 + assert dot == (1*4 + 2*5 + 3*6) + +def test_coordinate_division(): + c1 = Coordinate(x=2, y=4, z=6) + c2 = c1 / 2.0 + assert c2.x == 1.0 + assert c2.y == 2.0 + assert c2.z == 3.0 + +def test_coordinate_division_by_zero(): + c1 = Coordinate(x=2, y=4, z=6) + with pytest.raises(ValueError, match="Cannot divide by zero"): + _ = c1 / 0.0 + +def test_coordinate_normalize(): + c1 = Coordinate(x=3, y=0, z=4) + c2 = c1.normalize() + assert c2.x == 0.6 + assert c2.y == 0.0 + assert c2.z == 0.8 + +def test_coordinate_normalize_zero(): + c1 = Coordinate(x=0, y=0, z=0) + with pytest.raises(ValueError, match="Cannot normalize a zero-magnitude vector."): + c1.normalize() + +def test_coordinate_rotate_x(): + c1 = Coordinate(x=1, y=1, z=0) + # Rotate 90 degrees around X. (1, 1, 0) -> (1, 0, 1) + c2 = c1.rotate(90, 'x') + assert np.isclose(c2.x, 1) + assert np.isclose(c2.y, 0) + assert np.isclose(c2.z, 1) + +def test_coordinate_rotate_y(): + c1 = Coordinate(x=1, y=0, z=1) + # Rotate 90 degrees around Y. (1, 0, 1) -> (1, 0, -1) + c2 = c1.rotate(90, 'y') + assert np.isclose(c2.x, 1) + assert np.isclose(c2.y, 0) + assert np.isclose(c2.z, -1) + +def test_coordinate_rotate_z(): + c1 = Coordinate(x=1, y=0, z=0) + # Rotate 90 degrees around Z. (1, 0, 0) -> (0, 1, 0) + c2 = c1.rotate(90, 'z') + assert np.isclose(c2.x, 0) + assert np.isclose(c2.y, 1) + assert np.isclose(c2.z, 0) + +def test_coordinate_rotate_invalid_axis(): + c1 = Coordinate(x=1, y=1, z=1) + with pytest.raises(ValueError, match="Invalid axis"): + c1.rotate(90, 'w') + +def test_smargon_coordinate_equality(): + s1 = SmargonCoordinate(sh_mm=Coordinate(x=1, y=1, z=1), phi_deg=10, chi_deg=20) + s2 = SmargonCoordinate(sh_mm=Coordinate(x=1.05, y=0.95, z=1.01), phi_deg=10.05, chi_deg=19.95) + assert s1 == s2 + + s3 = SmargonCoordinate(sh_mm=Coordinate(x=2, y=1, z=1), phi_deg=10, chi_deg=20) + assert s1 != s3 + +def test_aerotech_coordinate_equality(): + a1 = AerotechCoordinate(at_mm=Coordinate(x=1, y=1, z=1), omega_deg=10) + a2 = AerotechCoordinate(at_mm=Coordinate(x=1.005, y=0.995, z=1.001), omega_deg=10.005) + assert a1 == a2 + + a3 = AerotechCoordinate(at_mm=Coordinate(x=1.1, y=1, z=1), omega_deg=10) + assert a1 != a3 + +def test_positive_coords(): + c1 = Coordinate(x=1, y=1, z=1) + assert positive_coords(c1) == c1 + + with pytest.raises(ValueError, match="Coordinates must be positive"): + positive_coords(Coordinate(x=-1, y=1, z=1)) + + with pytest.raises(ValueError, match="Coordinates must be positive"): + positive_coords(Coordinate(x=1, y=-1, z=1)) + +def test_coordinate_unsupported_ops(): + c1 = Coordinate(x=1, y=1, z=1) + assert c1.__add__(1) == NotImplemented + assert c1.__sub__(1) == NotImplemented + assert c1.__mul__("string") == NotImplemented + assert c1.__truediv__("string") == NotImplemented + +def test_smargon_coordinate_eq_not_implemented(): + s1 = SmargonCoordinate(sh_mm=Coordinate(x=1, y=1, z=1), phi_deg=10, chi_deg=20) + assert s1.__eq__(1) == NotImplemented + +def test_aerotech_coordinate_eq_not_implemented(): + a1 = AerotechCoordinate(at_mm=Coordinate(x=1, y=1, z=1), omega_deg=10) + assert a1.__eq__(1) == NotImplemented diff --git a/tests/unit/common/test_models_extra.py b/tests/unit/common/test_models_extra.py new file mode 100644 index 00000000..e2ace151 --- /dev/null +++ b/tests/unit/common/test_models_extra.py @@ -0,0 +1,100 @@ +import pytest +from aare.common.models import SampleShortInfo, DewarAddress, BeamMarkCoeffModel, MLOutputModel, MLBoxType, BeamlineStateEnum + +def test_sample_short_info_methods(): + info = SampleShortInfo( + db_id=1, + puck_name="puck1", + dewar_name="dew1", + sample_name="sample1", + run_number=1, + user="group1", + pin=3, + location=DewarAddress(segment="A", pos=2), + ) + + # Test tell_address (line 380) + addr = info.tell_address() + assert addr.puck.segment == "A" + assert addr.puck.pos == 2 + assert addr.pin == 3 + + # Test loc_str (lines 383-386) + assert info.loc_str() == "A2-3" + + # Test loc_str_sort (lines 389-392) + assert info.loc_str_sort() == "A2-03" + + info_no_loc = info.model_copy(update={"location": None}) + assert info_no_loc.loc_str() == "-" + assert info_no_loc.loc_str_sort() == "" + + # Test from_dict (line 396) + data = { + "db_id": 2, + "puck_name": "puck2", + "dewar_name": "dew2", + "sample_name": "sample2", + "run_number": 2, + "user": "group2", + "pin": 4, + "location": {"segment": "B", "pos": 5} + } + info2 = SampleShortInfo.from_dict(data) + assert info2.db_id == 2 + assert info2.location.segment == "B" + +def test_beam_mark_coeff_model_apply(): + # Test apply (line 414) + model = BeamMarkCoeffModel( + coeff_x=(1.0, 2.0, 5.0), + coeff_y=(3.0, 4.0, 6.0) + ) + # zoom = 10 + # x = 1.0 * 100 + 2.0 * 10 + 5.0 = 125.0 + # y = 3.0 * 100 + 4.0 * 10 + 6.0 = 346.0 + res = model.apply(10.0) + assert res.x == 125.0 + assert res.y == 346.0 + +def test_ml_output_model_extra_methods(): + model = MLOutputModel() + key = model.add_box(MLBoxType.Crystal, (1, 2, 3, 4), 0.8) + + # Test get_box_model (line 499) + box_model = model.get_box_model(key) + assert box_model.conf == 0.8 + + # Test get_box_tuple (lines 502-505) + assert model.get_box_tuple(key) == (1, 2, 3, 4) + assert model.get_box_tuple("NonExistent") is None + + # Test get_box_tuple_with_conf (lines 508-512) + assert model.get_box_tuple_with_conf(key) == (1, 2, 3, 4, 0.8) + assert model.get_box_tuple_with_conf("NonExistent") is None + + # Test get_tuples_for_class (lines 523-527) + tuples = model.get_tuples_for_class(MLBoxType.Crystal) + assert len(tuples) == 1 + assert tuples[0] == (1, 2, 3, 4) + + # Test get_tuples_with_conf_for_class (lines 530-534) + tuples_conf = model.get_tuples_with_conf_for_class(MLBoxType.Crystal) + assert len(tuples_conf) == 1 + assert tuples_conf[0] == (1, 2, 3, 4, 0.8) + + # Test get_class_str (lines 470, 475-481) + assert MLOutputModel.get_class_str(MLBoxType.Loop_all) == "Loop_all" + assert MLOutputModel.get_class_str(MLBoxType.Pin) == "Pin" + assert MLOutputModel.get_class_str(MLBoxType.Crystal) == "Crystal" + assert MLOutputModel.get_class_str(MLBoxType.Loop_face) == "Loop_face" + assert MLOutputModel.get_class_str(MLBoxType.Ice) == "Ice" + assert MLOutputModel.get_class_str(MLBoxType.Needle) == "Needle" + assert MLOutputModel.get_class_str(100) == "Unknown" + +def test_beamline_state_enum_display_name(): + # Test display_name (line 556) + assert BeamlineStateEnum.SampleExchange.display_name() == "Sample exchange" + assert BeamlineStateEnum.Moving.display_name() == "Moving" + # Using value for potentially unknown + assert BeamlineStateEnum.display_name(None) == "-"