From 8b3bac9f9330241af9776b5c277812319023df64 Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 17 Apr 2026 18:13:13 +0200 Subject: [PATCH] add tests --- .../0015_clubrole_alter_membership_role.py | 3 ++ club/models.py | 20 ++++-------- club/tests/test_club_controller.py | 2 +- club/tests/test_clubrole.py | 32 +++++++++++++++---- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/club/migrations/0015_clubrole_alter_membership_role.py b/club/migrations/0015_clubrole_alter_membership_role.py index 6a1b58ee..9c3e5a8e 100644 --- a/club/migrations/0015_clubrole_alter_membership_role.py +++ b/club/migrations/0015_clubrole_alter_membership_role.py @@ -148,6 +148,9 @@ class Migration(migrations.Migration): ("is_presidency", False), ("is_board", True), _connector="OR" ), name="clubrole_presidency_implies_board", + violation_error_message=( + "A role cannot be in the presidency while not being in the board" + ), ), ), migrations.RunPython(migrate_roles, migrations.RunPython.noop), diff --git a/club/models.py b/club/models.py index 2c5b5ff0..aedc6993 100644 --- a/club/models.py +++ b/club/models.py @@ -242,7 +242,9 @@ class Club(models.Model): """Return True if the given user can edit the roles of this club""" return user.is_authenticated and ( user.has_perm("club.change_clubrole") - or self.members.ongoing().filter(user=user, role__is_presidency=True).exists() + or self.members.ongoing() + .filter(user=user, role__is_presidency=True) + .exists() ) @cached_property @@ -292,6 +294,9 @@ class ClubRole(OrderedModel): models.CheckConstraint( condition=Q(is_presidency=False) | Q(is_board=True), name="clubrole_presidency_implies_board", + violation_error_message=_( + "A role cannot be in the presidency while not being in the board" + ), ) ] @@ -301,21 +306,8 @@ class ClubRole(OrderedModel): def get_display_name(self): return f"{self.name} - {self.club.name}" - def get_absolute_url(self): - return reverse("club:club_roles", kwargs={"club_id": self.club_id}) - def clean(self): errors = [] - if self.is_presidency and not self.is_board: - errors.append( - ValidationError( - _( - "Role %(name)s was declared as a presidency role " - "without being a board role" - ) - % {"name": self.name} - ) - ) roles = list(self.club.roles.all()) if ( self.is_board diff --git a/club/tests/test_club_controller.py b/club/tests/test_club_controller.py index 3bf0b75f..b6248e01 100644 --- a/club/tests/test_club_controller.py +++ b/club/tests/test_club_controller.py @@ -9,7 +9,7 @@ from model_bakery import baker from model_bakery.recipe import Recipe from pytest_django.asserts import assertNumQueries -from club.models import Club, ClubRole, Membership, ClubRole +from club.models import Club, ClubRole, Membership from core.baker_recipes import subscriber_user from core.models import Group, Page, User diff --git a/club/tests/test_clubrole.py b/club/tests/test_clubrole.py index e090f7e6..4628c292 100644 --- a/club/tests/test_clubrole.py +++ b/club/tests/test_clubrole.py @@ -1,6 +1,12 @@ +from collections.abc import Callable + import pytest +from django.contrib.auth.models import Permission +from django.test import Client, TestCase +from django.urls import reverse from model_bakery import baker, seq from model_bakery.recipe import Recipe +from pytest_django.asserts import assertRedirects from club.forms import ClubRoleFormSet from club.models import Club, ClubRole, Membership @@ -8,21 +14,35 @@ from core.baker_recipes import subscriber_user from core.models import AnonymousUser, User -@pytest.mark.django_db -def test_order_auto(): - """Test that newly created roles are put in the right place.""" +def make_club(): + # unittest-style tests cannot use fixture, so we create a function + # that will be callable either by a pytest fixture or inside + # a TestCase.setUpTestData method. club = baker.make(Club) recipe = Recipe(ClubRole, club=club, name=seq("role ")) - # bulk create initial roles (1 presidency, 1 board, 1 member) - roles = recipe.make( + recipe.make( is_board=iter([True, True, False]), is_presidency=iter([True, False, False]), order=iter([1, 2, 3]), _quantity=3, _bulk_create=True, ) - # then create the remaining roles one by one (like they will be in prod) + return club + + +@pytest.fixture +def club(db): + """A club with a presidency role, a board role and a member role""" + return make_club() + + +@pytest.mark.django_db +def test_order_auto(club): + """Test that newly created roles are put in the right place.""" + roles = list(club.roles.all()) + # create new roles one by one (like they will be in prod) # each new role should be placed at the end of its category + recipe = Recipe(ClubRole, club=club, name=seq("new role ")) role_a = recipe.make(is_board=True, is_presidency=True, order=None) role_b = recipe.make(is_board=True, is_presidency=False, order=None) role_c = recipe.make(is_board=False, is_presidency=False, order=None)