8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-20 10:23:12 +00:00
re2o/cotisations/test_models.py

154 lines
4.7 KiB
Python
Raw Normal View History

2019-11-03 20:46:18 +00:00
from django.test import TestCase
import datetime
from django.utils import timezone
2019-11-03 23:25:29 +00:00
from dateutil.relativedelta import relativedelta
2019-11-03 20:46:18 +00:00
from users.models import User
from .models import Vente, Facture, Cotisation, Paiement
2019-11-03 20:46:18 +00:00
class VenteModelTests(TestCase):
def setUp(self):
2019-12-15 15:23:53 +00:00
self.user = User.objects.create(pseudo="testUserPlop", email="test@example.org")
self.paiement = Paiement.objects.create(moyen="test payment")
2019-11-03 20:46:18 +00:00
self.f = Facture.objects.create(
user=self.user, paiement=self.paiement, valid=True
2019-11-03 20:46:18 +00:00
)
def test_one_day_cotisation(self):
"""
It should be possible to have one day membership.
"""
date = timezone.now()
purchase = Vente.objects.create(
facture=self.f,
number=1,
name="Test purchase",
duration=0,
duration_days=1,
type_cotisation="All",
prix=0,
)
2019-12-15 15:23:53 +00:00
self.f.reorder_purchases()
2019-11-03 20:46:18 +00:00
self.assertAlmostEqual(
self.user.end_connexion() - date,
datetime.timedelta(days=1),
delta=datetime.timedelta(seconds=1),
2019-11-03 20:46:18 +00:00
)
2019-11-03 23:25:29 +00:00
def test_one_month_cotisation(self):
"""
It should be possible to have one day membership.
"""
date = timezone.now()
2019-12-15 15:23:53 +00:00
Vente.objects.create(
2019-11-03 23:25:29 +00:00
facture=self.f,
number=1,
name="Test purchase",
duration=1,
duration_days=0,
type_cotisation="All",
prix=0,
)
2019-12-15 15:23:53 +00:00
self.f.reorder_purchases()
end = self.user.end_connexion()
expected_end = date + relativedelta(months=1)
self.assertEqual(end.day, expected_end.day)
self.assertEqual(end.month, expected_end.month)
self.assertEqual(end.year, expected_end.year)
2019-11-03 23:25:29 +00:00
def test_one_month_and_one_week_cotisation(self):
"""
It should be possible to have one day membership.
"""
date = timezone.now()
2019-12-15 15:23:53 +00:00
Vente.objects.create(
2019-11-03 23:25:29 +00:00
facture=self.f,
number=1,
name="Test purchase",
duration=1,
duration_days=7,
type_cotisation="All",
prix=0,
)
2019-12-15 15:23:53 +00:00
self.f.reorder_purchases()
end = self.user.end_connexion()
expected_end = date + relativedelta(months=1, days=7)
self.assertEqual(end.day, expected_end.day)
self.assertEqual(end.month, expected_end.month)
self.assertEqual(end.year, expected_end.year)
2019-11-03 23:25:29 +00:00
2019-12-20 07:43:35 +00:00
def test_date_start_cotisation(self):
"""
It should be possible to add a cotisation with a specific start date
"""
v = Vente(
facture=self.f,
number=1,
name="Test purchase",
duration=0,
duration_days=1,
type_cotisation = 'All',
prix=0
)
v.create_cotis(date_start=timezone.make_aware(datetime.datetime(1998, 10, 16)))
v.save()
self.assertEqual(v.cotisation.date_end, timezone.make_aware(datetime.datetime(1998, 10, 17)))
2019-11-03 20:46:18 +00:00
def tearDown(self):
self.f.delete()
self.user.delete()
self.paiement.delete()
2019-12-15 15:23:53 +00:00
class FactureModelTests(TestCase):
def setUp(self):
self.user = User.objects.create(pseudo="testUserPlop", email="test@example.org")
self.paiement = Paiement.objects.create(moyen="test payment")
def tearDown(self):
self.user.delete()
self.paiement.delete()
def test_cotisations_prolongation(self):
"""When user already have one valid cotisation, the new one should be
added at the end of the existing one."""
date = timezone.now()
invoice1 = Facture.objects.create(
user=self.user, paiement=self.paiement, valid=True
)
Vente.objects.create(
facture=invoice1,
number=1,
name="Test purchase",
duration=1,
duration_days=0,
type_cotisation="All",
prix=0,
)
invoice1.reorder_purchases()
invoice2 = Facture.objects.create(
user=self.user, paiement=self.paiement, valid=True
)
Vente.objects.create(
facture=invoice2,
number=1,
name="Test purchase",
duration=1,
duration_days=0,
type_cotisation="All",
prix=0,
)
invoice1.reorder_purchases()
delta = relativedelta(self.user.end_connexion(), date)
delta.microseconds = 0
try:
self.assertEqual(delta, relativedelta(months=2))
except Exception as e:
invoice1.delete()
invoice2.delete()
raise e
invoice1.delete()
invoice2.delete()