mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Use pytest for tests (#681)
* use pytest for tests Eh ouais, il y a que la config qui change. Pytest est implémentable par étapes. Et ça c'est beau. * rework tests with pytest * remove unittest custom TestRunner * Edit doc and CI
This commit is contained in:
@ -14,8 +14,9 @@
|
||||
#
|
||||
#
|
||||
from datetime import date
|
||||
from unittest import mock
|
||||
|
||||
import freezegun
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
@ -23,113 +24,92 @@ from core.models import User
|
||||
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)
|
||||
@pytest.mark.parametrize(
|
||||
("today", "duration", "expected_start"),
|
||||
[
|
||||
(date(2020, 9, 18), 1, date(2020, 9, 18)),
|
||||
(date(2020, 9, 18), 2, date(2020, 9, 18)),
|
||||
(date(2020, 5, 17), 3, date(2020, 2, 15)),
|
||||
(date(2021, 1, 18), 4, date(2020, 8, 15)),
|
||||
(date(2020, 9, 18), 4, date(2020, 8, 15)),
|
||||
],
|
||||
)
|
||||
def test_subscription_compute_start_from_today(today, duration, expected_start):
|
||||
with freezegun.freeze_time(today):
|
||||
assert Subscription.compute_start(duration=duration) == expected_start
|
||||
|
||||
|
||||
def date_mock_today(year, month, day):
|
||||
FakeDate.today = classmethod(lambda cls: date(year, month, day))
|
||||
@pytest.mark.parametrize(
|
||||
("start_date", "duration", "expected_start"),
|
||||
[
|
||||
(date(2020, 5, 17), 1, date(2020, 5, 17)),
|
||||
(date(2020, 5, 17), 2, date(2020, 5, 17)),
|
||||
(date(2020, 5, 17), 3, date(2020, 2, 15)),
|
||||
(date(2020, 1, 11), 3, date(2019, 8, 15)),
|
||||
],
|
||||
)
|
||||
def test_subscription_compute_start_explicit(start_date, duration, expected_start):
|
||||
assert Subscription.compute_start(start_date, duration=duration) == expected_start
|
||||
|
||||
|
||||
class SubscriptionUnitTest(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))
|
||||
@pytest.mark.parametrize(
|
||||
("today", "duration", "expected_end"),
|
||||
[
|
||||
(date(2020, 9, 18), 1, date(2021, 3, 18)),
|
||||
(date(2020, 9, 18), 2, date(2021, 9, 18)),
|
||||
(date(2020, 9, 18), 3, date(2022, 2, 15)),
|
||||
(date(2020, 5, 17), 4, date(2022, 8, 15)),
|
||||
(date(2020, 9, 18), 0.33, date(2020, 11, 18)),
|
||||
(date(2020, 9, 18), 0.67, date(2021, 1, 19)),
|
||||
(date(2020, 9, 18), 0.5, date(2020, 12, 18)),
|
||||
],
|
||||
)
|
||||
def test_subscription_compute_end_from_today(today, duration, expected_end):
|
||||
with freezegun.freeze_time(today):
|
||||
assert Subscription.compute_end(duration=duration) == expected_end
|
||||
|
||||
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))
|
||||
|
||||
@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, 19))
|
||||
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))
|
||||
@pytest.mark.parametrize(
|
||||
("start_date", "duration", "expected_end"),
|
||||
[
|
||||
(date(2020, 9, 18), 3, date(2022, 3, 18)),
|
||||
(date(2020, 9, 18), 4, date(2022, 9, 18)),
|
||||
],
|
||||
)
|
||||
def test_subscription_compute_end_from_today(start_date, duration, expected_end):
|
||||
assert Subscription.compute_end(duration, start_date) == expected_end
|
||||
|
||||
|
||||
class SubscriptionIntegrationTest(TestCase):
|
||||
@classmethod
|
||||
def setUp(cls):
|
||||
cls.user = User.objects.filter(username="public").first()
|
||||
def setUpTestData(cls):
|
||||
cls.user = User.objects.get(username="public")
|
||||
|
||||
def test_duration_one_month(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
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.166, start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 9, 29))
|
||||
assert s.subscription_end == date(2017, 9, 29)
|
||||
|
||||
def test_duration_two_months(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
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.333, start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 10, 29))
|
||||
assert s.subscription_end == date(2017, 10, 29)
|
||||
|
||||
def test_duration_one_day(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
||||
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
||||
)
|
||||
@ -139,44 +119,43 @@ class SubscriptionIntegrationTest(TestCase):
|
||||
start=s.subscription_start,
|
||||
)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2017, 8, 30))
|
||||
assert s.subscription_end == date(2017, 8, 30)
|
||||
|
||||
def test_duration_three_months(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
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))
|
||||
assert s.subscription_end == date(2017, 11, 29)
|
||||
|
||||
def test_duration_four_months(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
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, 30))
|
||||
assert s.subscription_end == date(2017, 12, 30)
|
||||
|
||||
def test_duration_six_weeks(self):
|
||||
s = Subscription(
|
||||
member=User.objects.filter(pk=self.user.pk).first(),
|
||||
member=self.user,
|
||||
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
||||
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
||||
)
|
||||
s.subscription_start = date(2018, 9, 1)
|
||||
s.subscription_end = s.compute_end(duration=0.23, start=s.subscription_start)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2018, 10, 13))
|
||||
assert s.subscription_end == date(2018, 10, 13)
|
||||
|
||||
@mock.patch("subscription.models.date", FakeDate)
|
||||
def test_dates_sliding_with_subscribed_user(self):
|
||||
user = User.objects.filter(pk=self.user.pk).first()
|
||||
user = self.user
|
||||
s = Subscription(
|
||||
member=user,
|
||||
subscription_type="deux-semestres",
|
||||
@ -188,18 +167,16 @@ class SubscriptionIntegrationTest(TestCase):
|
||||
start=s.subscription_start,
|
||||
)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2016, 8, 29))
|
||||
date_mock_today(2016, 8, 25)
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
assert s.subscription_end == date(2016, 8, 29)
|
||||
with freezegun.freeze_time("2016-08-25"):
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
assert d == date(2017, 8, 29)
|
||||
|
||||
self.assertTrue(d == date(2017, 8, 29))
|
||||
|
||||
@mock.patch("subscription.models.date", FakeDate)
|
||||
def test_dates_renewal_sliding_during_two_free_monthes(self):
|
||||
user = User.objects.filter(pk=self.user.pk).first()
|
||||
user = self.user
|
||||
s = Subscription(
|
||||
member=user,
|
||||
subscription_type="deux-mois-essai",
|
||||
@ -211,17 +188,16 @@ class SubscriptionIntegrationTest(TestCase):
|
||||
start=s.subscription_start,
|
||||
)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2015, 10, 29))
|
||||
date_mock_today(2015, 9, 25)
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
self.assertTrue(d == date(2016, 10, 29))
|
||||
assert s.subscription_end == date(2015, 10, 29)
|
||||
with freezegun.freeze_time("2015-09-25"):
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
assert d == date(2016, 10, 29)
|
||||
|
||||
@mock.patch("subscription.models.date", FakeDate)
|
||||
def test_dates_renewal_sliding_after_two_free_monthes(self):
|
||||
user = User.objects.filter(pk=self.user.pk).first()
|
||||
user = self.user
|
||||
s = Subscription(
|
||||
member=user,
|
||||
subscription_type="deux-mois-essai",
|
||||
@ -233,10 +209,10 @@ class SubscriptionIntegrationTest(TestCase):
|
||||
start=s.subscription_start,
|
||||
)
|
||||
s.save()
|
||||
self.assertTrue(s.subscription_end == date(2015, 10, 29))
|
||||
date_mock_today(2015, 11, 5)
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
self.assertTrue(d == date(2016, 11, 5))
|
||||
assert s.subscription_end == date(2015, 10, 29)
|
||||
with freezegun.freeze_time("2015-11-05"):
|
||||
d = Subscription.compute_end(
|
||||
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
||||
user=user,
|
||||
)
|
||||
assert d == date(2016, 11, 5)
|
||||
|
Reference in New Issue
Block a user