diff --git a/fixedcal/core/date.py b/fixedcal/core/date.py index f4ea0ef..4269261 100644 --- a/fixedcal/core/date.py +++ b/fixedcal/core/date.py @@ -116,5 +116,37 @@ class FixedDate: return self._day_of_year > o.day_of_year return self._year > o.year - def __sub__(self, o: "FixedDate") -> timedelta: - return self.datetime - o.datetime + def __add__(self, o: timedelta) -> "FixedDate": + """Addition of FixedDate and timedelta. + Does not modify this instance, but creates new one. + + Args: + o (timedelta): The time delta that will be added. + + Returns: + FixedDate: New FixedDate instance that will hold the new date. + """ + new_date = self.datetime + o + return FixedDate(date=new_date) + + def __sub__(self, o): + """Subtraction of FixedDate and some other value. + Does not modify either one of the values. + + Args: + o (Union[FixedDate, timedelta]): The value that will be added. + + Raises: + ValueError: Given argument was not FixedDate nor timedelta. + + Returns: + Union[FixedDate, timedelta]: With FixedDate as argument, + timedelta will be returned representing the difference of given fixed dates. + With timedelta as argument, new FixedDate will be returned. + """ + if isinstance(o, FixedDate): + return self.datetime - o.datetime + elif isinstance(o, timedelta): + new_date = self.datetime - o + return FixedDate(date=new_date) + raise ValueError("Invalid subtractor type, expected FixedDate or timedelta") diff --git a/tests/operations_test.py b/tests/operations_test.py index 02e70e2..bcdfdd6 100644 --- a/tests/operations_test.py +++ b/tests/operations_test.py @@ -25,11 +25,34 @@ class TestOperations(unittest.TestCase): def test_less_than_with_true_expected(self): self.assertTrue(self.fixed1 < self.fixed2) - def test_subtration_with_two_different(self): + def test_subtration_of_two_dates(self): self.assertEqual(self.fixed2-self.fixed1, timedelta(1)) - def test_subtration_with_smaller_first(self): + def test_subtration_of_two_dates_with_smaller_first(self): self.assertEqual(self.fixed1-self.fixed2, timedelta(-1)) - def test_subtration_with_two_same(self): + def test_subtration_of_two_same_dates(self): self.assertEqual(self.fixed1-self.fixed1, timedelta(0)) + + def test_subtraction_of_timedelta(self): + result = self.fixed1 - timedelta(7) + self.assertEqual(result, FixedDate(date=datetime(2022, 11, 28))) + + def test_subtraction_of_negative_timedelta(self): + result = self.fixed1 - timedelta(-2) + self.assertEqual(result, FixedDate(date=datetime(2022, 12, 7))) + + def test_subtraction_of_invalid_type(self): + self.assertRaises(ValueError, lambda : self.fixed1 - 3) + + def test_addition_of_timedelta(self): + result = self.fixed1 + timedelta(3) + self.assertEqual(result, FixedDate(date=datetime(2022, 12, 8))) + + def test_addition_of_negative_timedelta(self): + result = self.fixed1 + timedelta(-3) + self.assertEqual(result, FixedDate(date=datetime(2022, 12, 2))) + + def test_addition_does_not_modify(self): + _ = self.fixed1 + timedelta(2) + self.assertEqual(self.fixed1.datetime, datetime(2022, 12, 5))