From 9ee5c387c5a0b2c4edf7ac6bdef27c7b0c458d1f Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 29 Aug 2017 15:06:52 +0200 Subject: [PATCH] subscription: add tests Signed-off-by: Skia --- subscription/models.py | 4 ++- subscription/tests.py | 64 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/subscription/models.py b/subscription/models.py index ccab2502..15b7842d 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -137,7 +137,7 @@ class Subscription(models.Model): return 'No user - ' + str(self.pk) @staticmethod - def compute_start(d=date.today(), duration=1): + def compute_start(d=None, duration=1): """ This function computes the start date of the subscription with respect to the given date (default is today), and the start date given in settings.SITH_START_DATE. @@ -147,6 +147,8 @@ class Subscription(models.Model): 2015-03-17 -> 2015-02-15 2015-01-11 -> 2014-08-15 """ + if not d: + d = date.today() if duration <= 2: # Sliding subscriptions for 1 or 2 semesters return d return get_start_of_semester(d) diff --git a/subscription/tests.py b/subscription/tests.py index b8fbbe1e..4c27c242 100644 --- a/subscription/tests.py +++ b/subscription/tests.py @@ -21,7 +21,69 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # +from datetime import date +from unittest import mock from django.test import TestCase +from subscription.models import Subscription + +class FakeDate(date): + """A fake replacement for date that can be mocked for testing.""" + def __new__(cls, *args, **kwargs): + return date.__new__(date, *args, **kwargs) + +def date_mock_today(year, month, day): + FakeDate.today = classmethod(lambda cls: date(year, month, day)) + +class SubscribtionTest(TestCase): + + @mock.patch('subscription.models.date', FakeDate) + def test_start_dates_sliding_without_start(self): + date_mock_today(2015, 9, 18) + d = Subscription.compute_start(duration=1) + self.assertTrue(d == date(2015, 9, 18)) + self.assertTrue(Subscription.compute_start(duration=2) == date(2015, 9, 18)) + + def test_start_dates_sliding_with_start(self): + self.assertTrue(Subscription.compute_start(date(2015, 5, 17), 1) == + date(2015, 5, 17)) + self.assertTrue(Subscription.compute_start(date(2015, 5, 17), 2) == + date(2015, 5, 17)) + + @mock.patch('subscription.models.date', FakeDate) + def test_start_dates_not_sliding_without_start(self): + date_mock_today(2015, 5, 17) + self.assertTrue(Subscription.compute_start(duration=3) == date(2015, 2, 15)) + date_mock_today(2016, 1, 18) + self.assertTrue(Subscription.compute_start(duration=4) == date(2015, 8, 15)) + date_mock_today(2015, 9, 18) + self.assertTrue(Subscription.compute_start(duration=4) == date(2015, 8, 15)) + + def test_start_dates_not_sliding_with_start(self): + self.assertTrue(Subscription.compute_start(date(2015, 5, 17), 3) == + date(2015, 2, 15)) + self.assertTrue(Subscription.compute_start(date(2015, 1, 11), 3) == + date(2014, 8, 15)) + + @mock.patch('subscription.models.date', FakeDate) + def test_end_dates_sliding(self): + date_mock_today(2015, 9, 18) + d = Subscription.compute_end(2) + self.assertTrue(d == date(2016, 9, 18)) + d = Subscription.compute_end(1) + self.assertTrue(d == date(2016, 3, 18)) + + @mock.patch('subscription.models.date', FakeDate) + def test_end_dates_not_sliding_without_start(self): + date_mock_today(2015, 9, 18) + d = Subscription.compute_end(duration=3) + self.assertTrue(d == date(2017, 2, 15)) + d = Subscription.compute_end(duration=4) + self.assertTrue(d == date(2017, 8, 15)) + + def test_end_dates_not_sliding_with_start(self): + d = Subscription.compute_end(duration=3, start=date(2015, 9, 18)) + self.assertTrue(d == date(2017, 3, 18)) + d = Subscription.compute_end(duration=4, start=date(2015, 9, 18)) + self.assertTrue(d == date(2017, 9, 18)) -# Create your tests here.