From f6aa89231500417fb84f53ed0203e806f15462ce Mon Sep 17 00:00:00 2001 From: Eric Butcher <107886303+Eric-Butcher@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:55:36 -0400 Subject: [PATCH] Tests for the expires_in property of the Allocation model. Signed-off-by: Eric Butcher <107886303+Eric-Butcher@users.noreply.github.com> --- .../core/allocation/tests/test_models.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/coldfront/core/allocation/tests/test_models.py b/coldfront/core/allocation/tests/test_models.py index 87849db10..756d9ba65 100644 --- a/coldfront/core/allocation/tests/test_models.py +++ b/coldfront/core/allocation/tests/test_models.py @@ -5,6 +5,7 @@ """Unit tests for the allocation models""" import datetime +from unittest.mock import patch from django.core.exceptions import ValidationError from django.test import TestCase @@ -140,3 +141,62 @@ def test_status_is_active_and_start_date_equals_end_date_no_error(self): status=self.active_status, start_date=start_and_end_date, end_date=start_and_end_date, project=self.project ) actual_allocation.full_clean() + + +class AllocationModelExpiresInTests(TestCase): + mocked_today = datetime.date(2025, 1, 1) + three_years_after_mocked_today = datetime.date(2028, 1, 1) + four_years_after_mocked_today = datetime.date(2029, 1, 1) + + def test_end_date_is_today_returns_zero(self): + """Test that the expires_in method returns 0 when the end date is today.""" + allocation: Allocation = AllocationFactory(end_date=timezone.now().date()) + self.assertEqual(allocation.expires_in, 0) + + def test_end_date_tomorrow_returns_one(self): + """Test that the expires_in method returns 1 when the end date is tomorrow.""" + tomorrow: datetime.date = (timezone.now() + datetime.timedelta(days=1)).date() + allocation: Allocation = AllocationFactory(end_date=tomorrow) + self.assertEqual(allocation.expires_in, 1) + + def test_end_date_yesterday_returns_negative_one(self): + """Test that the expires_in method returns -1 when the end date is yesterday.""" + yesterday: datetime.date = (timezone.now() - datetime.timedelta(days=1)).date() + allocation: Allocation = AllocationFactory(end_date=yesterday) + self.assertEqual(allocation.expires_in, -1) + + def test_end_date_one_week_ago_returns_negative_seven(self): + """Test that the expires_in method returns -7 when the end date is one week ago.""" + days_in_a_week: int = 7 + one_week_ago: datetime.date = (timezone.now() - datetime.timedelta(days=days_in_a_week)).date() + allocation: Allocation = AllocationFactory(end_date=one_week_ago) + self.assertEqual(allocation.expires_in, -days_in_a_week) + + def test_end_date_in_one_week_returns_seven(self): + """Test that the expires_in method returns 7 when the end date is in one week.""" + days_in_a_week: int = 7 + one_week_from_now: datetime.date = (timezone.now() + datetime.timedelta(days=days_in_a_week)).date() + allocation: Allocation = AllocationFactory(end_date=one_week_from_now) + self.assertEqual(allocation.expires_in, days_in_a_week) + + def test_end_date_in_three_years_without_leap_day_returns_days_including_no_leap_day(self): + """Test that the expires_in method returns the correct number of days in three years when those years did not have a leap year.""" + days_in_three_years_excluding_leap_year = 365 * 3 + + with patch("coldfront.core.allocation.models.datetime") as mock_datetime: + mock_datetime.date.today.return_value = self.mocked_today + + allocation: Allocation = AllocationFactory(end_date=self.three_years_after_mocked_today) + + self.assertEqual(allocation.expires_in, days_in_three_years_excluding_leap_year) + + def test_end_date_in_four_years_returns_days_including_leap_day(self): + """Test that the expires_in method accounts for the extra day of a leap year.""" + days_in_four_years_including_leap_year = (365 * 4) + 1 + + with patch("coldfront.core.allocation.models.datetime") as mock_datetime: + mock_datetime.date.today.return_value = self.mocked_today + + allocation: Allocation = AllocationFactory(end_date=self.four_years_after_mocked_today) + + self.assertEqual(allocation.expires_in, days_in_four_years_including_leap_year)