From 8c5b39b077db83574d2722a5ef68e9403a719b15 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Mon, 11 Dec 2023 16:50:02 +0100 Subject: [PATCH 1/3] Add failing tests --- tests/unit/test_input4mips.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_input4mips.py b/tests/unit/test_input4mips.py index b9f49b1..508f48e 100644 --- a/tests/unit/test_input4mips.py +++ b/tests/unit/test_input4mips.py @@ -3,6 +3,7 @@ """ import re +import cftime import pytest import xarray as xr @@ -173,9 +174,20 @@ def test_from_metadata_autoadd_bounds_to_dimensions_multivar(): ) -def test_format_date_not_implemented(): - with pytest.raises(NotImplementedError, match="yr"): - format_date("mock", ds_frequency="yr") +@pytest.mark.parametrize( + "date, freq, exp", + ( + (cftime.DatetimeGregorian(2012, 1, 3), "mon", "201201"), + (cftime.DatetimeGregorian(2012, 4, 1), "mon", "201204"), + (cftime.DatetimeGregorian(2022, 1, 3), "yr", "2022"), + (cftime.DatetimeGregorian(2022, 11, 13), "yr", "2022"), + (cftime.DatetimeGregorian(1, 11, 13), "mon", "000111"), + (cftime.DatetimeGregorian(1, 11, 13), "yr", "0001"), + ), +) +def test_format_date(date, freq, exp): + res = format_date(date, ds_frequency=freq) + assert res == exp def test_add_time_bounds_name_clash(): From 641807648eb7b24f79618f89a98e14039b4aa049 Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Mon, 11 Dec 2023 16:50:25 +0100 Subject: [PATCH 2/3] Pass failing tests --- src/carpet_concentrations/input4MIPs/dataset.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/carpet_concentrations/input4MIPs/dataset.py b/src/carpet_concentrations/input4MIPs/dataset.py index 226aba7..c1af5b9 100644 --- a/src/carpet_concentrations/input4MIPs/dataset.py +++ b/src/carpet_concentrations/input4MIPs/dataset.py @@ -467,12 +467,13 @@ def format_date( ------- Formatted date """ - if ds_frequency.endswith("mon"): - datestring = date.strftime("%Y%m") - else: - raise NotImplementedError(ds_frequency) + if ds_frequency.startswith("mon"): + return date.strftime("%Y%m") + + if ds_frequency.startswith("yr"): + return date.strftime("%Y") - return datestring + raise NotImplementedError(ds_frequency) def get_version(creation_date: str) -> str: From d14e20bfbdcd7425430dfe3bb0118208324228fc Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Mon, 11 Dec 2023 16:54:21 +0100 Subject: [PATCH 3/3] Put fallback test back in --- tests/unit/test_input4mips.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/test_input4mips.py b/tests/unit/test_input4mips.py index 508f48e..b00dc44 100644 --- a/tests/unit/test_input4mips.py +++ b/tests/unit/test_input4mips.py @@ -190,6 +190,11 @@ def test_format_date(date, freq, exp): assert res == exp +def test_format_date_not_implemented(): + with pytest.raises(NotImplementedError, match="junk"): + format_date("mock", ds_frequency="junk") + + def test_add_time_bounds_name_clash(): inp = xr.Dataset( {