diff --git a/imap_processing/tests/test_utils.py b/imap_processing/tests/test_utils.py index 787289a6d..36c0588c1 100644 --- a/imap_processing/tests/test_utils.py +++ b/imap_processing/tests/test_utils.py @@ -97,10 +97,26 @@ def test_packet_file_to_datasets(use_derived_value, expected_mode): np.testing.assert_array_equal(data["mode"], [expected_mode] * len(data["mode"])) -def test__create_minimum_dtype_array(): +@pytest.mark.parametrize( + ("arr", "dtype", "expected_dtype"), + [ + # Expected basic case + ([1, 2, 3], "uint8", "uint8"), + # We shouldn't go lower than requested either + ([1, 2, 3], "uint16", "uint16"), + # Can't cast negative, fallback to default + ([-1, 2, 3], "uint8", int), + # Small signed ints should be good + ([-1, 2, 3], "int8", "int8"), + # Can't cast strings to ints, fallback to default + (["a", "b", "c"], "uint8", " npt.NDArray: array : np.array The array of values. """ + # Create an initial array and then try to safely cast it to the desired dtype + x = np.asarray(values) try: - return np.array(values, dtype=dtype) + # ValueError: when trying to cast strings (enum states) to ints + y = x.astype(dtype, copy=False) + # We need to compare the arrays to see if we trimmed any values by + # casting to a smaller datatype (e.g. float64 to uint8, 2.1 to 2) + if np.array_equal(x, y): + return y except ValueError: - return np.array(values) + pass + return x def packet_file_to_datasets(