Skip to content

Commit

Permalink
Merge pull request #63 from ACCESS-NRI/55/refactor-fix_latlon_coord
Browse files Browse the repository at this point in the history
55/refactor fix_latlon_coord
  • Loading branch information
blimlim authored Sep 17, 2024
2 parents 7ed3b7d + 37049c7 commit 9acda2f
Show file tree
Hide file tree
Showing 4 changed files with 600 additions and 78 deletions.
32 changes: 16 additions & 16 deletions test/test_conversion_driver_esm1p5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from pathlib import Path
import unittest.mock as mock
import umpost.um2netcdf as um2nc


def test_get_esm1p5_fields_file_pattern():
Expand Down Expand Up @@ -98,11 +99,11 @@ def mock_process(base_mock_process):

@pytest.fixture
def mock_process_with_exception(mock_process):
# Add a generic exception with chosen message to mock_process.
# Yield function so that tests of different exception messages
# Add a specified exception with chosen message to mock_process.
# Yield function so that tests of different exceptions and messages
# can make use of the same fixture.
def _mock_process_with_exception(error_message):
mock_process.side_effect = Exception(error_message)
def _mock_process_with_exception(error_type, error_message):
mock_process.side_effect = error_type(error_message)

yield _mock_process_with_exception

Expand Down Expand Up @@ -130,29 +131,28 @@ def test_convert_fields_file_list_success(mock_process,


def test_convert_fields_file_list_fail_excepted(mock_process_with_exception):
# Hopefully this test will be unnecessary with um2nc standalone.
# Test that the "Variable can not be processed" error arising from time
# series inputs is excepted.
allowed_error_message = esm1p5_convert.ALLOWED_UM2NC_EXCEPTION_MESSAGES[
"TIMESERIES_ERROR"
]
mock_process_with_exception(allowed_error_message)
# Test that UnsupportedTimeSeriesErrors are excepted.

# TODO: This test will not catch changes to the exceptions in um2nc. E.g.
# if decided to instead raise a RuntimeError in um2nc when timeseries
# are encountered, the conversion of ESM1.5 outputs would undesirably
# crash, but this test would say everything is fine, since we're
# prescribing the error that is raised.
mock_process_with_exception(um2nc.UnsupportedTimeSeriesError,
"timeseries error")
fake_file_path = Path("fake_file")

_, failed = esm1p5_convert.convert_fields_file_list(
[fake_file_path], "fake_nc_write_dir")

assert failed[0][0] == fake_file_path

# TODO: Testing the exception part of the reported failures will be easier
# once um2nc specific exceptions are added.


def test_convert_fields_file_list_fail_critical(mock_process_with_exception):
# Test that critical exceptions which are not allowed by ALLOWED_UM2NC_EXCEPTION_MESSAGES
# Test that critical unexpected exceptions
# are raised, and hence lead to the conversion crashing.
generic_error_message = "Test error"
mock_process_with_exception(generic_error_message)
mock_process_with_exception(Exception, generic_error_message)
with pytest.raises(Exception) as exc_info:
esm1p5_convert.convert_fields_file_list(
["fake_file"], "fake_nc_write_dir")
Expand Down
Loading

0 comments on commit 9acda2f

Please sign in to comment.