From b8219236474745576d4ba4abff96ec996d8e95f2 Mon Sep 17 00:00:00 2001 From: rlskoeser Date: Fri, 8 Nov 2024 17:50:26 -0500 Subject: [PATCH] Improve test coverage and error handling for EDTF --- src/undate/converters/edtf/converter.py | 2 +- tests/test_converters/test_edtf.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/undate/converters/edtf/converter.py b/src/undate/converters/edtf/converter.py index 13411fd..86d83b5 100644 --- a/src/undate/converters/edtf/converter.py +++ b/src/undate/converters/edtf/converter.py @@ -103,4 +103,4 @@ def _undate_to_string(self, undate: Undate) -> str: return "-".join(parts) # how can we have an empty string? probably shouldn't get here - return "" + raise ValueError("Failed to generate an EDTF string from %r", undate) diff --git a/tests/test_converters/test_edtf.py b/tests/test_converters/test_edtf.py index 24fad38..78b3be5 100644 --- a/tests/test_converters/test_edtf.py +++ b/tests/test_converters/test_edtf.py @@ -1,5 +1,6 @@ import pytest from undate.converters.edtf import EDTFDateConverter +from undate.date import DatePrecision from undate.undate import Undate, UndateInterval @@ -52,5 +53,12 @@ def test_to_string(self): assert EDTFDateConverter().to_string(Undate(1991, "0X")) == "1991-0X" assert EDTFDateConverter().to_string(Undate(1991, None, 3)) == "1991-XX-03" + assert EDTFDateConverter().to_string(Undate(-1984)) == "-1984" + # if converter can't generate a string for the date, + # it should return a value error + empty_undate = Undate() + empty_undate.precision = DatePrecision.DECADE + with pytest.raises(ValueError): + EDTFDateConverter().to_string(empty_undate) # TODO: override missing digit and confirm replacement