def test_values_for_quantlib(self): assert timedelta_to_string(timedelta(days=1), for_quantlib=True) == "1D" timedelta_string = timedelta_to_string(timedelta(days=1.5 * DAYS_IN_A_YEAR), for_quantlib=True) assert timedelta_string == "1Y6M" timedelta_string = timedelta_to_string(timedelta(days=18 * DAYS_IN_A_MONTH), for_quantlib=True) assert timedelta_string == "1Y6M"
def test_returns_proper_label(self): assert timedelta_to_string(timedelta(days=0)) == "0" assert timedelta_to_string(timedelta(days=1)) == "O/N" assert timedelta_to_string(timedelta(days=2 * DAYS_IN_A_WEEK)) == "2W" assert timedelta_to_string(timedelta(days=6 * DAYS_IN_A_MONTH)) == "6M" assert timedelta_to_string(timedelta(days=1 * DAYS_IN_A_YEAR)) == "1Y" assert timedelta_to_string(timedelta(days=99 * DAYS_IN_A_YEAR)) == "99Y" assert timedelta_to_string(timedelta(days=54 * DAYS_IN_A_MONTH)) == "4.5Y"
def test_non_initial_units_is_correct(self): tenors_to_test = { timedelta(days=0): "0", timedelta(days=1): "Overnight", timedelta(days=1 * DAYS_IN_A_WEEK): "1 Week", timedelta(days=2 * DAYS_IN_A_WEEK): "2 Weeks", timedelta(days=1 * DAYS_IN_A_MONTH): "1 Month", timedelta(days=10 * DAYS_IN_A_MONTH): "10 Months", timedelta(days=1 * DAYS_IN_A_YEAR): "1 Year", timedelta(days=10 * DAYS_IN_A_YEAR): "10 Years", } for tenor, label in tenors_to_test.items(): assert timedelta_to_string(tenor, only_initial=False) == label
def test_works_with_tenors(self): tenor = choice(list(TENORS)) assert timedelta_to_string(tenor) == tenor.label
def test_can_round_to_n_digits(self): t = string_to_timedelta("10.234Y") timedelta_string = timedelta_to_string(t, round_ndigits=3, only_quarter_years=False) assert timedelta_string == "10.234Y"
def test_returns_none_unchanged(self): assert timedelta_to_string(None) is None
def test_returns_string_unchanged(self): assert timedelta_to_string("1 day") == "1 day"
def test_returns_months_for_time_between_1Y_to_18M_unless_quarter_year( self): assert timedelta_to_string(timedelta(days=13 * DAYS_IN_A_MONTH)) == "13M" assert timedelta_to_string(timedelta(days=14 * DAYS_IN_A_MONTH)) == "14M" assert timedelta_to_string(timedelta(days=15 * DAYS_IN_A_MONTH)) == "1.25Y" assert timedelta_to_string(timedelta(days=16 * DAYS_IN_A_MONTH)) == "16M" assert timedelta_to_string(timedelta(days=17 * DAYS_IN_A_MONTH)) == "17M" assert timedelta_to_string(timedelta(days=18 * DAYS_IN_A_MONTH)) == "1.5Y" assert timedelta_to_string(timedelta(days=19 * DAYS_IN_A_MONTH)) == "19M" assert timedelta_to_string(timedelta(days=20 * DAYS_IN_A_MONTH)) == "20M" assert timedelta_to_string(timedelta(days=21 * DAYS_IN_A_MONTH)) == "1.75Y" assert timedelta_to_string(timedelta(days=22 * DAYS_IN_A_MONTH)) == "22M" assert timedelta_to_string(timedelta(days=23 * DAYS_IN_A_MONTH)) == "23M" assert timedelta_to_string(timedelta(days=24 * DAYS_IN_A_MONTH)) == "2Y"