|
| 1 | +"""Tests for the nextsim normalizer""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +import unittest.mock as mock |
| 5 | +from datetime import datetime, timezone |
| 6 | + |
| 7 | +import metanorm.normalizers as normalizers |
| 8 | +from metanorm.errors import MetadataNormalizationError |
| 9 | + |
| 10 | + |
| 11 | +class DownscaledECMWFMetadataNormalizerTests(unittest.TestCase): |
| 12 | + """Tests for DownscaledECMWFMetadataNormalizer""" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.normalizer = normalizers.DownscaledECMWFMetadataNormalizer() |
| 16 | + |
| 17 | + def test_check(self): |
| 18 | + """Test the checking condition""" |
| 19 | + self.assertTrue(self.normalizer.check({'url': '/foo/bar/Seasonal_Nov23_SAT_n15.nc'})) |
| 20 | + self.assertTrue(self.normalizer.check({'url': '/foo/bar/Seasonal_Nov23_SDA_n15.nc'})) |
| 21 | + |
| 22 | + self.assertFalse(self.normalizer.check({})) |
| 23 | + self.assertFalse(self.normalizer.check({'url': ''})) |
| 24 | + self.assertFalse(self.normalizer.check({'url': '/foo/bar/baz.nc'})) |
| 25 | + |
| 26 | + def test_get_entry_title(self): |
| 27 | + """Test getting the title""" |
| 28 | + self.assertEqual(self.normalizer.get_entry_title({}), 'Downscaled ECMWF seasonal forecast') |
| 29 | + |
| 30 | + def test_get_entry_id(self): |
| 31 | + """Test getting the ID""" |
| 32 | + self.assertEqual( |
| 33 | + self.normalizer.get_entry_id({ |
| 34 | + 'url': '/foo/bar/Seasonal_Nov23_SDA_n15.nc' |
| 35 | + }), |
| 36 | + 'Seasonal_Nov23_SDA_n15') |
| 37 | + self.assertEqual( |
| 38 | + self.normalizer.get_entry_id({ |
| 39 | + 'url': '/foo/bar/Seasonal_Nov23_SAT_n15.nc' |
| 40 | + }), |
| 41 | + 'Seasonal_Nov23_SAT_n15') |
| 42 | + |
| 43 | + def test_entry_id_error(self): |
| 44 | + """A MetadataNormalizationError should be raised if the url |
| 45 | + attribute is missing or the ID is not found |
| 46 | + """ |
| 47 | + with self.assertRaises(MetadataNormalizationError): |
| 48 | + self.normalizer.get_entry_id({}) |
| 49 | + with self.assertRaises(MetadataNormalizationError): |
| 50 | + self.normalizer.get_entry_id({'url': 'foo'}) |
| 51 | + |
| 52 | + def test_summary(self): |
| 53 | + """Test getting the summary""" |
| 54 | + self.assertEqual( |
| 55 | + self.normalizer.get_summary({}), |
| 56 | + "Downscaled version of ECMWF's seasonal forecasts") |
| 57 | + |
| 58 | + def test_get_time_coverage_start(self): |
| 59 | + """Test getting the start of the time coverage""" |
| 60 | + self.assertEqual( |
| 61 | + self.normalizer.get_time_coverage_start({'date': '2023-11-14 13:21:08'}), |
| 62 | + datetime(year=2023, month=11, day=1, tzinfo=timezone.utc)) |
| 63 | + |
| 64 | + def test_missing_time_coverage_start(self): |
| 65 | + """A MetadataNormalizationError must be raised when the |
| 66 | + time_coverage_start raw attribute is missing |
| 67 | + """ |
| 68 | + with self.assertRaises(MetadataNormalizationError): |
| 69 | + self.normalizer.get_time_coverage_start({}) |
| 70 | + |
| 71 | + def test_get_time_coverage_end(self): |
| 72 | + """Test getting the end of the time coverage""" |
| 73 | + self.assertEqual( |
| 74 | + self.normalizer.get_time_coverage_end({'date': '2023-11-14 13:21:08'}), |
| 75 | + datetime(year=2024, month=5, day=1, tzinfo=timezone.utc)) |
| 76 | + |
| 77 | + def test_missing_time_coverage_end(self): |
| 78 | + """A MetadataNormalizationError must be raised when the |
| 79 | + time_coverage_end raw attribute is missing |
| 80 | + """ |
| 81 | + with self.assertRaises(MetadataNormalizationError): |
| 82 | + self.normalizer.get_time_coverage_end({}) |
| 83 | + |
| 84 | + def test_gcmd_platform(self): |
| 85 | + """Test getting the platform""" |
| 86 | + with mock.patch('metanorm.utils.get_gcmd_platform') as mock_get_gcmd_method: |
| 87 | + self.assertEqual( |
| 88 | + self.normalizer.get_platform({}), |
| 89 | + mock_get_gcmd_method.return_value) |
| 90 | + mock_get_gcmd_method.assert_called_with('OPERATIONAL MODELS') |
| 91 | + |
| 92 | + def test_gcmd_instrument(self): |
| 93 | + """Test getting the instrument""" |
| 94 | + with mock.patch('metanorm.utils.get_gcmd_instrument') as mock_get_gcmd_method: |
| 95 | + self.assertEqual( |
| 96 | + self.normalizer.get_instrument({}), |
| 97 | + mock_get_gcmd_method.return_value) |
| 98 | + mock_get_gcmd_method.assert_called_with('Computer') |
| 99 | + |
| 100 | + def test_gcmd_provider(self): |
| 101 | + """Test getting the provider""" |
| 102 | + with mock.patch('metanorm.utils.get_gcmd_provider') as mock_get_gcmd_method: |
| 103 | + self.assertEqual( |
| 104 | + self.normalizer.get_provider({}), |
| 105 | + mock_get_gcmd_method.return_value) |
| 106 | + mock_get_gcmd_method.assert_called_with(['NERSC']) |
| 107 | + |
| 108 | + def test_get_location_geometry(self): |
| 109 | + """get_location_geometry() should return the location |
| 110 | + of the dataset |
| 111 | + """ |
| 112 | + self.assertEqual(self.normalizer.get_location_geometry({}), '') |
0 commit comments