diff --git a/fincal/utils.py b/fincal/utils.py index dc2584c..7014535 100644 --- a/fincal/utils.py +++ b/fincal/utils.py @@ -116,7 +116,7 @@ def _find_closest_date( raise ValueError(f"Invalid argument for if_not_found: {if_not_found}") -def _interval_to_years(interval_type: Literal["years", "months", "day"], interval_value: int) -> int: +def _interval_to_years(interval_type: Literal["years", "months", "day"], interval_value: int) -> float: """Converts any time period to years for use with compounding functions""" year_conversion_factor = {"years": 1, "months": 12, "days": 365} diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..ed575f5 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,26 @@ +import datetime + +import pytest +from fincal.utils import _interval_to_years, _parse_date + + +class TestParseDate: + def test_parsing(self): + dt = datetime.datetime(2020, 1, 1) + assert _parse_date(dt) == dt + assert _parse_date(dt.strftime("%Y-%m-%d")) == dt + assert _parse_date(datetime.date(2020, 1, 1)) == dt + assert _parse_date("01-01-2020", date_format="%d-%m-%Y") == dt + assert _parse_date("01-01-2020", date_format="%m-%d-%Y") == dt + + def test_errors(self): + with pytest.raises(ValueError): + _parse_date("01-01-2020") + + with pytest.raises(ValueError): + _parse_date("abcdefg") + + +class TestIntervalToYears: + def test_months(self): + assert _interval_to_years("months", 6) == 0.5