diff --git a/scos_actions/calibration/tests/test_sensor_calibration.py b/scos_actions/calibration/tests/test_sensor_calibration.py index 88de63f6..aa29ba81 100644 --- a/scos_actions/calibration/tests/test_sensor_calibration.py +++ b/scos_actions/calibration/tests/test_sensor_calibration.py @@ -276,11 +276,17 @@ def test_get_calibration_dict_within_range(self): clock_rate_lookup_by_sample_rate=[], sensor_uid="TESTING", ) + + lookup_fail_value = 250.0 with pytest.raises(CalibrationException) as e_info: - _ = cal.get_calibration_dict({"sample_rate": 100.0, "frequency": 250.0}) + _ = cal.get_calibration_dict( + {"sample_rate": 100.0, "frequency": lookup_fail_value} + ) assert e_info.value.args[0] == ( - f"Could not locate calibration data at 250.0" - + f"\nAttempted lookup using key '250.0'" + f"Could not locate calibration data at {lookup_fail_value}" + + "\nAttempted lookup using keys: " + + f"\n\tstr({lookup_fail_value}).lower() = {str(lookup_fail_value).lower()}" + + f"\n\tstr(int({lookup_fail_value})) = {int(lookup_fail_value)}" + f"\nUsing calibration data: {cal.calibration_data['100.0']}" ) diff --git a/scos_actions/calibration/tests/test_utils.py b/scos_actions/calibration/tests/test_utils.py index 15ab29cf..8b6eefcf 100644 --- a/scos_actions/calibration/tests/test_utils.py +++ b/scos_actions/calibration/tests/test_utils.py @@ -6,12 +6,17 @@ class TestCalibrationUtils: def test_filter_by_parameter_out_of_range(self): calibrations = {200.0: {"some_cal_data"}, 300.0: {"more cal data"}} + + # Also checks error output when missing value is an integer + test_value = 400 with pytest.raises(CalibrationException) as e_info: - _ = filter_by_parameter(calibrations, 400.0) + _ = filter_by_parameter(calibrations, test_value) assert ( e_info.value.args[0] - == f"Could not locate calibration data at 400.0" - + f"\nAttempted lookup using key '400.0' and 400.0" + == f"Could not locate calibration data at {test_value}" + + "\nAttempted lookup using keys: " + + f"\n\tstr({test_value}).lower() = {str(test_value).lower()}" + + f"\n\tstr(float({test_value})) = {float(test_value)}" + f"\nUsing calibration data: {calibrations}" ) @@ -20,11 +25,16 @@ def test_filter_by_parameter_in_range_requires_match(self): 200.0: {"Gain": "Gain at 200.0"}, 300.0: {"Gain": "Gain at 300.0"}, } + + # Check looking up a missing value with a float + test_value = 150.0 with pytest.raises(CalibrationException) as e_info: - _ = filter_by_parameter(calibrations, 150.0) + _ = filter_by_parameter(calibrations, test_value) assert e_info.value.args[0] == ( - f"Could not locate calibration data at 150.0" - + f"\nAttempted lookup using key '150.0' and 150.0" + f"Could not locate calibration data at {test_value}" + + "\nAttempted lookup using keys: " + + f"\n\tstr({test_value}).lower() = {str(test_value).lower()}" + + f"\n\tstr(int({test_value})) = {int(test_value)}" + f"\nUsing calibration data: {calibrations}" ) diff --git a/scos_actions/calibration/utils.py b/scos_actions/calibration/utils.py index a526e7f6..8e6a21dd 100644 --- a/scos_actions/calibration/utils.py +++ b/scos_actions/calibration/utils.py @@ -52,12 +52,15 @@ def filter_by_parameter(calibrations: dict, value: Union[float, int, bool]) -> d """ try: filtered_data = calibrations.get(str(value).lower(), None) + attempts = f"\n\tstr({value}).lower() = {str(value).lower()}" if filtered_data is None and isinstance(value, int): # Try equivalent float for ints, i.e., match "1.0" to 1 filtered_data = calibrations.get(str(float(value)), None) + attempts += f"\n\tstr(float({value})) = {str(float(value))}" if filtered_data is None and isinstance(value, float) and value.is_integer(): # Check for, e.g., key '25' if value is '25.0' filtered_data = calibrations.get(str(int(value)), None) + attempts += f"\n\tstr(int({value})) = {str(int(value))}" if filtered_data is None: raise KeyError else: @@ -70,9 +73,7 @@ def filter_by_parameter(calibrations: dict, value: Union[float, int, bool]) -> d except KeyError: msg = ( f"Could not locate calibration data at {value}" - + f"\nAttempted lookup using key '{str(value).lower()}'" - + f"{f' and {float(value)}' if isinstance(value, int) else ''}" - + f"{f' and {int(value)}' if isinstance(value, float) and value.is_integer() else ''}" + + f"\nAttempted lookup using keys: {attempts}" + f"\nUsing calibration data: {calibrations}" ) raise CalibrationEntryMissingException(msg)