Skip to content

Commit

Permalink
Add tests for lines missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Nov 7, 2024
1 parent 25ba4ad commit 540bd10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/undate/undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ def _comparison_type(self, other: object) -> "Undate":
def __eq__(self, other: object) -> bool:
# Note: assumes label differences don't matter for comparing dates

other = self._comparison_type(other)

# only a day-precision fully known undate can be equal to a datetime.date
if isinstance(other, datetime.date):
return self.earliest == other and self.latest == other

other = self._comparison_type(other)
if other is NotImplemented:
return NotImplemented

# check for apparent equality
looks_equal = (
self.earliest == other.earliest
Expand Down Expand Up @@ -346,6 +348,7 @@ def day(self) -> Optional[str]:
if day:
return f"{day:>02}"
# if value is unset but date precision is day, return unknown day
# (may not be possible to have day precision with day part set in normal use)
elif self.precision == DatePrecision.DAY:
return self.MISSING_DIGIT * 2
return None
Expand Down
19 changes: 18 additions & 1 deletion tests/test_undate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import date

import pytest
from undate.date import Timedelta
from undate.date import DatePrecision, Timedelta
from undate.undate import Undate, UndateInterval


Expand Down Expand Up @@ -143,6 +143,11 @@ def test_year_property(self):
# unset year
assert Undate(month=12, day=31).year == "XXXX"

# force method to hit conditional for date precision
some_century = Undate()
some_century.precision = DatePrecision.CENTURY
assert some_century.year is None

def test_month_property(self):
# one, two digit month
assert Undate(2023, 1).month == "01"
Expand Down Expand Up @@ -172,12 +177,20 @@ def test_day_property(self):
# Day without year or month
assert Undate(day=15).day == "15"

# force str based on date precision without day part set
someday = Undate(2023)
someday.precision = DatePrecision.DAY
assert someday.day == "XX"

def test_eq(self):
assert Undate(2022) == Undate(2022)
assert Undate(2022, 10) == Undate(2022, 10)
assert Undate(2022, 10, 1) == Undate(2022, 10, 1)
assert Undate(month=2, day=7) == Undate(month=2, day=7)

# something we can't convert for comparison should return NotImplemented
assert Undate(2022).__eq__("not a date") == NotImplemented

def test_eq_datetime_date(self):
# support comparisons with datetime objects for full day-precision
assert Undate(2022, 10, 1) == date(2022, 10, 1)
Expand Down Expand Up @@ -535,3 +548,7 @@ def test_duration(self):

# duration is not supported for open-ended intervals
assert UndateInterval(Undate(2000), None).duration() == NotImplemented

# one year set and the other not currently raises not implemented error
with pytest.raises(NotImplementedError):
UndateInterval(Undate(2000), Undate()).duration()

0 comments on commit 540bd10

Please sign in to comment.