From f1afa3b43608723fdfecc9fd468d381868bdc846 Mon Sep 17 00:00:00 2001 From: thomas girod Date: Wed, 4 Sep 2024 16:21:42 +0200 Subject: [PATCH] fix memberships ending today --- club/models.py | 2 +- club/tests.py | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/club/models.py b/club/models.py index 24e75f34..3c31aad1 100644 --- a/club/models.py +++ b/club/models.py @@ -269,7 +269,7 @@ class Club(models.Model): class MembershipQuerySet(models.QuerySet): def ongoing(self) -> Self: """Filter all memberships which are not finished yet.""" - return self.filter(Q(end_date=None) | Q(end_date__gte=timezone.now())) + return self.filter(Q(end_date=None) | Q(end_date__gt=timezone.now().date())) def board(self) -> Self: """Filter all memberships where the user is/was in the board. diff --git a/club/tests.py b/club/tests.py index 31b9562a..6450d32d 100644 --- a/club/tests.py +++ b/club/tests.py @@ -24,6 +24,7 @@ from django.utils.translation import gettext as _ from club.forms import MailingForm from club.models import Club, Mailing, Membership +from core.baker_recipes import subscriber_user from core.models import AnonymousUser, User from sith.settings import SITH_BAR_MANAGER, SITH_MAIN_CLUB_ID @@ -106,6 +107,18 @@ class TestMembershipQuerySet(TestClub): expected.sort(key=lambda i: i.id) assert current_members == expected + def test_ongoing_with_membership_ending_today(self): + """Test that a membership ending the present day is considered as ended.""" + today = timezone.now().date() + self.richard.memberships.filter(club=self.club).update(end_date=today) + current_members = list(self.club.members.ongoing().order_by("id")) + expected = [ + self.skia.memberships.get(club=self.club), + self.comptable.memberships.get(club=self.club), + ] + expected.sort(key=lambda i: i.id) + assert current_members == expected + def test_board(self): """Test that the board queryset method returns the memberships of user in the club board. @@ -422,11 +435,11 @@ class TestClubModel(TestClub): of anyone. """ # make subscriber a board member - self.subscriber.memberships.all().delete() - Membership.objects.create(club=self.ae, user=self.subscriber, role=3) + subscriber = subscriber_user.make() + Membership.objects.create(club=self.ae, user=subscriber, role=3) - nb_memberships = self.club.members.count() - self.client.force_login(self.subscriber) + nb_memberships = self.club.members.ongoing().count() + self.client.force_login(subscriber) response = self.client.post( self.members_url, {"users_old": self.comptable.id}, @@ -437,7 +450,7 @@ class TestClubModel(TestClub): def test_end_membership_as_root(self): """Test that root users can end the membership of anyone.""" - nb_memberships = self.club.members.count() + nb_memberships = self.club.members.ongoing().count() self.client.force_login(self.root) response = self.client.post( self.members_url, @@ -446,7 +459,6 @@ class TestClubModel(TestClub): self.assertRedirects(response, self.members_url) self.assert_membership_ended_today(self.comptable) assert self.club.members.ongoing().count() == nb_memberships - 1 - assert self.club.members.count() == nb_memberships def test_end_membership_as_foreigner(self): """Test that users who are not in this club cannot end its memberships."""