mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
New subscription type for jewels
New condition to consider floats in subscriptions
This commit is contained in:
@ -104,7 +104,7 @@ class Subscription(models.Model):
|
||||
'sbarro/esta': 9,
|
||||
'cursus-alternant': 10,
|
||||
'welcome-semestre': 11,
|
||||
'un-mois': 12,
|
||||
'deux-mois-essai': 12,
|
||||
}
|
||||
PAYMENT = {
|
||||
"CHECK": 1,
|
||||
@ -169,11 +169,11 @@ class Subscription(models.Model):
|
||||
start = Subscription.compute_start(duration=duration)
|
||||
# This can certainly be simplified, but it works like this
|
||||
try:
|
||||
return start.replace(month=int((start.month - 1 + 6 * duration) % 12 + 1),
|
||||
year=int(start.year + int(duration / 2) + (1 if start.month > 6 and duration % 2 == 1 else 0)))
|
||||
return start.replace(month=int(round((start.month - 1 + 6 * duration) % 12 + 1, 0)),
|
||||
year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0)))
|
||||
except ValueError as e:
|
||||
return start.replace(day=1, month=int((start.month + 6 * duration) % 12 + 1),
|
||||
year=int(start.year + int(duration / 2) + (1 if start.month > 6 and duration % 2 == 1 else 0)))
|
||||
return start.replace(day=1, month=int(round((start.month + 6 * duration) % 12 + 1, 0)),
|
||||
year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0)))
|
||||
|
||||
def can_be_edited_by(self, user):
|
||||
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root
|
||||
|
@ -26,16 +26,23 @@ from unittest import mock
|
||||
|
||||
from django.test import TestCase
|
||||
from subscription.models import Subscription
|
||||
from core.models import User
|
||||
from django.conf import settings
|
||||
from datetime import datetime
|
||||
from django.core.management import call_command
|
||||
|
||||
|
||||
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):
|
||||
|
||||
class SubscriptionTest(TestCase):
|
||||
|
||||
@mock.patch('subscription.models.date', FakeDate)
|
||||
def test_start_dates_sliding_without_start(self):
|
||||
@ -46,9 +53,9 @@ class SubscribtionTest(TestCase):
|
||||
|
||||
def test_start_dates_sliding_with_start(self):
|
||||
self.assertTrue(Subscription.compute_start(date(2015, 5, 17), 1) ==
|
||||
date(2015, 5, 17))
|
||||
date(2015, 5, 17))
|
||||
self.assertTrue(Subscription.compute_start(date(2015, 5, 17), 2) ==
|
||||
date(2015, 5, 17))
|
||||
date(2015, 5, 17))
|
||||
|
||||
@mock.patch('subscription.models.date', FakeDate)
|
||||
def test_start_dates_not_sliding_without_start(self):
|
||||
@ -60,10 +67,8 @@ class SubscribtionTest(TestCase):
|
||||
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))
|
||||
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):
|
||||
@ -81,9 +86,54 @@ class SubscribtionTest(TestCase):
|
||||
d = Subscription.compute_end(duration=4)
|
||||
self.assertTrue(d == date(2017, 8, 15))
|
||||
|
||||
@mock.patch('subscription.models.date', FakeDate)
|
||||
def test_end_dates_with_float(self):
|
||||
date_mock_today(2015, 9, 18)
|
||||
d = Subscription.compute_end(duration=0.33)
|
||||
self.assertTrue(d == date(2015, 11, 18))
|
||||
d = Subscription.compute_end(duration=0.67)
|
||||
self.assertTrue(d == date(2016, 1, 18))
|
||||
d = Subscription.compute_end(duration=0.5)
|
||||
self.assertTrue(d == date(2015, 12, 18))
|
||||
|
||||
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))
|
||||
|
||||
|
||||
class DecimalDurationTest(TestCase):
|
||||
def setUp(self):
|
||||
call_command("populate")
|
||||
self.user = User.objects.filter(username="public").first()
|
||||
|
||||
def test_duration_two_months(self):
|
||||
|
||||
s = Subscription(member=User.objects.filter(pk=self.user.pk).first(), subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
||||
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
|
||||
s.subscription_start = date(2017, 8, 29)
|
||||
s.subscription_end = s.compute_end(duration=0.33,
|
||||
start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 10, 29))
|
||||
|
||||
def test_duration_three_months(self):
|
||||
|
||||
s = Subscription(member=User.objects.filter(pk=self.user.pk).first(), subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
||||
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
|
||||
s.subscription_start = date(2017, 8, 29)
|
||||
s.subscription_end = s.compute_end(duration=0.5,
|
||||
start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 11, 29))
|
||||
|
||||
def test_duration_four_months(self):
|
||||
|
||||
s = Subscription(member=User.objects.filter(pk=self.user.pk).first(), subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
||||
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0])
|
||||
s.subscription_start = date(2017, 8, 29)
|
||||
s.subscription_end = s.compute_end(duration=0.67,
|
||||
start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 12, 29))
|
||||
|
Reference in New Issue
Block a user