|
| 1 | +""" |
| 2 | +This module contains unit tests for the `model_output_parser` module. |
| 3 | +""" |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from util.model_output_parser import get_ds |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(name="mock_ds_grib") |
| 13 | +def fixture_mock_ds_grib(): |
| 14 | + """ |
| 15 | + Fixture that creates a mock GRIB object to simulate different cases of data |
| 16 | + selection and exceptions that `get_ds` needs to handle. |
| 17 | + """ |
| 18 | + ds_grib = MagicMock() |
| 19 | + |
| 20 | + # Simulating the successful selection and conversion to xarray |
| 21 | + ds_grib.sel.return_value.to_xarray.return_value = "valid_xarray" |
| 22 | + |
| 23 | + # Simulating the metadata retrieval (unique values) |
| 24 | + ds_grib.sel.return_value.metadata.side_effect = [ |
| 25 | + ["forecast"], # stepType |
| 26 | + [100], # numberOfPoints |
| 27 | + ["hours"], # stepUnits |
| 28 | + ["analysis"], # dataType |
| 29 | + ["regular_ll"], # gridType |
| 30 | + ] |
| 31 | + |
| 32 | + return ds_grib |
| 33 | + |
| 34 | + |
| 35 | +def test_get_ds_success(mock_ds_grib): |
| 36 | + """ |
| 37 | + Test case where get_ds successfully retrieves the dataset on the first attempt. |
| 38 | + """ |
| 39 | + pid = 1 |
| 40 | + lev = "surface" |
| 41 | + |
| 42 | + result = get_ds(mock_ds_grib, pid, lev) |
| 43 | + |
| 44 | + # Ensure the dataset is selected once |
| 45 | + mock_ds_grib.sel.assert_called_once_with(paramId=pid, typeOfLevel=lev) |
| 46 | + |
| 47 | + # The result should contain the mocked xarray dataset |
| 48 | + assert result == ["valid_xarray"] |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "to_xarray_return_value", |
| 53 | + [ |
| 54 | + (KeyError(), "valid_xarray"), |
| 55 | + (KeyError(), KeyError(), "valid_xarray"), |
| 56 | + (KeyError(), KeyError(), KeyError(), "valid_xarray"), |
| 57 | + (KeyError(), KeyError(), KeyError(), KeyError(), "valid_xarray"), |
| 58 | + (KeyError(), KeyError(), KeyError(), KeyError(), KeyError(), "valid_xarray"), |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_get_ds_recursive_selection(mock_ds_grib, to_xarray_return_value): |
| 62 | + """ |
| 63 | + Test case where get_ds recursively selects the dataset by metadata fields. |
| 64 | + """ |
| 65 | + pid = 1 |
| 66 | + lev = "surface" |
| 67 | + |
| 68 | + mock_ds_grib.sel.return_value.to_xarray.side_effect = to_xarray_return_value |
| 69 | + |
| 70 | + result = get_ds(mock_ds_grib, pid, lev) |
| 71 | + |
| 72 | + # Ensure the recursive logic is triggered by calling sel multiple times |
| 73 | + assert mock_ds_grib.sel.call_count >= len(to_xarray_return_value) |
| 74 | + |
| 75 | + # The result should contain the mocked xarray dataset |
| 76 | + assert result == ["valid_xarray"] |
| 77 | + |
| 78 | + |
| 79 | +def test_get_ds_keyerror_handling(caplog, mock_ds_grib): |
| 80 | + """ |
| 81 | + Test case where get_ds fails to retrieve data and handles multiple KeyErrors. |
| 82 | + """ |
| 83 | + # # Create a mock GRIB object |
| 84 | + # mock_ds_grib = MagicMock() |
| 85 | + |
| 86 | + pid = 1 |
| 87 | + lev = "surface" |
| 88 | + |
| 89 | + # Simulate KeyErrors for all attempts to select datasets |
| 90 | + mock_ds_grib.sel.return_value.to_xarray.side_effect = KeyError() |
| 91 | + |
| 92 | + result = get_ds(mock_ds_grib, pid, lev) |
| 93 | + |
| 94 | + # Assert that the warning was logged |
| 95 | + assert "GRIB file of level surface and paramId 1 cannot be read." in caplog.text |
| 96 | + assert not result |
0 commit comments