from aarecommon.errors.codes import ( AareErrorCode, AuthErrorCode, code_for_exception_class, error_code_help, export_error_codes, export_error_codes_grouped, ) def test_error_code_help_returns_string(): help_text = error_code_help(AuthErrorCode.AUTHENTICATION_FAILED) assert isinstance(help_text, str) assert len(help_text) > 0 def test_error_code_help_unknown_code(): help_text = error_code_help("UNKNOWN_CODE") assert help_text is None def test_export_error_codes_contains_known_codes(): exported = export_error_codes() assert AuthErrorCode.AUTHENTICATION_FAILED.name in exported def test_code_for_exception_class_basic(): assert code_for_exception_class("TellCommunicationError") == "TELL_COMMUNICATION_ERROR" assert code_for_exception_class("MountingFailed") == "MOUNTING_FAILED" assert code_for_exception_class("LoopCenteringFailed") == "LOOP_CENTERING_FAILED" def test_code_for_exception_class_acronyms(): assert code_for_exception_class("AXCFailed") == "AXC_FAILED" assert code_for_exception_class("AareDBCommunicationError") == "AARE_DB_COMMUNICATION_ERROR" assert code_for_exception_class("JFJochCommunicationError") == "JF_JOCH_COMMUNICATION_ERROR" def test_code_for_each_concrete_exception_is_in_aare_error_code_enum(): """Every code we'd produce from the rebuilt hierarchy must exist in the AareErrorCode enum -- otherwise clients have no symbol to branch on.""" from aarecommon.errors.exception_handler import ( AareDBCommunicationError, AerotechCommunicationError, AuthenticationException, AutoRasterSampleSkipped, AXCFailed, BeamlineBusyException, BeamlineBusyTimeoutException, BECCommunicationError, CriticalTellException, DataCollectionException, JFJochCommunicationError, LoopCenteringFailed, MagnetPositionSensorErorr, MaintenanceStateException, ManualMountException, MountingFailed, RasterScanException, SampleException, SmargonCommunicationError, SmartMagnetFaultException, StateTransitionFailed, TellCommandWhileBusyException, TellCommunicationError, TellConnectionException, TransformationInvalidException, UnmountingFailed, UserRightsException, WarningTellException, ) valid = {c.value for c in AareErrorCode} classes = [ TellCommunicationError, TellConnectionException, CriticalTellException, WarningTellException, TellCommandWhileBusyException, MountingFailed, UnmountingFailed, SmargonCommunicationError, AerotechCommunicationError, JFJochCommunicationError, BECCommunicationError, AareDBCommunicationError, StateTransitionFailed, MaintenanceStateException, BeamlineBusyException, BeamlineBusyTimeoutException, DataCollectionException, RasterScanException, LoopCenteringFailed, AXCFailed, AutoRasterSampleSkipped, TransformationInvalidException, MagnetPositionSensorErorr, SmartMagnetFaultException, ManualMountException, SampleException, AuthenticationException, UserRightsException, ] missing = [] for cls in classes: code = code_for_exception_class(cls.__name__) if code not in valid: missing.append((cls.__name__, code)) assert not missing, f"Codes missing from AareErrorCode: {missing}" def test_export_error_codes_grouped_includes_aare_error_code(): exported = export_error_codes_grouped() assert "AareErrorCode" in exported assert "AuthErrorCode" in exported assert "TELL_COMMUNICATION_ERROR" in exported["AareErrorCode"]