Skip to content

Commit 5119ccd

Browse files
committed
Add unit tests for missing coverage
1 parent c5c1cc8 commit 5119ccd

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/undate/undate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ class Calendar(StrEnum):
3535
def get_converter(calendar) -> BaseCalendarConverter:
3636
# calendar converter must be available with a name matching
3737
# the title-case name of the calendar enum entry
38-
converter_cls = BaseDateConverter.available_converters()[calendar.value.title()]
38+
try:
39+
converter_cls = BaseDateConverter.available_converters()[
40+
calendar.value.title()
41+
]
42+
except KeyError:
43+
raise ValueError(f"Unknown calendar '{calendar}'")
3944
if not issubclass(converter_cls, BaseCalendarConverter):
4045
raise ValueError(
41-
f"Requested converter {converter_cls} is not a CalendarConverter"
46+
f"Requested converter '{calendar.value.title()}' is not a CalendarConverter"
4247
)
4348
return converter_cls()
4449

tests/test_converters/test_calendars/test_hebrew/test_hebrew_converter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def test_compare_across_calendars(self):
154154
expected_gregorian_years = [-3261, 33, 1056, 1350, 1655, 1995]
155155
assert [d.earliest.year for d in sorted_dates] == expected_gregorian_years
156156

157+
def test_days_in_year(self):
158+
converter = HebrewDateConverter()
159+
assert converter.days_in_year(4816) == 353
160+
assert converter.days_in_year(4817) == 355
161+
assert converter.days_in_year(4818) == 384
162+
assert converter.days_in_year(4819) == 355
163+
157164
def test_representative_years(self):
158165
converter = HebrewDateConverter()
159166
# single year is not filtered

tests/test_converters/test_calendars/test_seleucid.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def test_gregorian_earliest_latest(self):
5353
assert date.latest == Date(1146, 10, 15)
5454
assert date.label == f"{date_str} {SeleucidDateConverter.calendar_name}"
5555

56+
def test_days_in_year(self):
57+
converter = SeleucidDateConverter()
58+
assert converter.days_in_year(2350) == 354
59+
assert converter.days_in_year(2349) == 385
60+
assert converter.days_in_year(2351) == 355
61+
5662

5763
# TODO: update validation error to say seleucid instead of hebrew
5864

tests/test_undate.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from datetime import date, datetime
2+
from enum import auto
23

34
import pytest
45

56
from undate import Undate, UndateInterval, Calendar
6-
from undate.converters.base import BaseCalendarConverter
7+
from undate.undate import StrEnum # import whichever version is used there
8+
from undate.converters.base import BaseCalendarConverter, BaseDateConverter
79
from undate.date import Date, DatePrecision, Timedelta, UnDelta, UnInt
810

911

@@ -545,3 +547,25 @@ def test_calendar_get_converter():
545547
converter = Calendar.get_converter(cal)
546548
assert isinstance(converter, BaseCalendarConverter)
547549
assert converter.name.lower() == cal.name.lower()
550+
551+
class BogusCalendar(StrEnum):
552+
"""Unsupported calendars"""
553+
554+
FOOBAR = auto()
555+
DUMMY = auto()
556+
557+
# test error handling
558+
# ensure we raise a ValueError when an invalid calendar is requested
559+
with pytest.raises(ValueError, match="Unknown calendar"):
560+
Calendar.get_converter(BogusCalendar.FOOBAR)
561+
562+
class DummyFormatter(BaseDateConverter):
563+
name = "Dummy"
564+
565+
# also error if you request a converter that is not a calendar converter
566+
# NOTE: this fails because get_converter converts the enum to title case...
567+
# can't be tested with any of the existing non-calendar converters
568+
with pytest.raises(
569+
ValueError, match="Requested converter 'Dummy' is not a CalendarConverter"
570+
):
571+
Calendar.get_converter(BogusCalendar.DUMMY)

0 commit comments

Comments
 (0)