add tests

This commit is contained in:
imperosol
2026-04-17 18:13:13 +02:00
parent 25c797fff8
commit 8b3bac9f93
4 changed files with 36 additions and 21 deletions
@@ -148,6 +148,9 @@ class Migration(migrations.Migration):
("is_presidency", False), ("is_board", True), _connector="OR" ("is_presidency", False), ("is_board", True), _connector="OR"
), ),
name="clubrole_presidency_implies_board", 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), migrations.RunPython(migrate_roles, migrations.RunPython.noop),
+6 -14
View File
@@ -242,7 +242,9 @@ class Club(models.Model):
"""Return True if the given user can edit the roles of this club""" """Return True if the given user can edit the roles of this club"""
return user.is_authenticated and ( return user.is_authenticated and (
user.has_perm("club.change_clubrole") 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 @cached_property
@@ -292,6 +294,9 @@ class ClubRole(OrderedModel):
models.CheckConstraint( models.CheckConstraint(
condition=Q(is_presidency=False) | Q(is_board=True), condition=Q(is_presidency=False) | Q(is_board=True),
name="clubrole_presidency_implies_board", 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): def get_display_name(self):
return f"{self.name} - {self.club.name}" 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): def clean(self):
errors = [] 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()) roles = list(self.club.roles.all())
if ( if (
self.is_board self.is_board
+1 -1
View File
@@ -9,7 +9,7 @@ from model_bakery import baker
from model_bakery.recipe import Recipe from model_bakery.recipe import Recipe
from pytest_django.asserts import assertNumQueries 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.baker_recipes import subscriber_user
from core.models import Group, Page, User from core.models import Group, Page, User
+26 -6
View File
@@ -1,6 +1,12 @@
from collections.abc import Callable
import pytest 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 import baker, seq
from model_bakery.recipe import Recipe from model_bakery.recipe import Recipe
from pytest_django.asserts import assertRedirects
from club.forms import ClubRoleFormSet from club.forms import ClubRoleFormSet
from club.models import Club, ClubRole, Membership from club.models import Club, ClubRole, Membership
@@ -8,21 +14,35 @@ from core.baker_recipes import subscriber_user
from core.models import AnonymousUser, User from core.models import AnonymousUser, User
@pytest.mark.django_db def make_club():
def test_order_auto(): # unittest-style tests cannot use fixture, so we create a function
"""Test that newly created roles are put in the right place.""" # that will be callable either by a pytest fixture or inside
# a TestCase.setUpTestData method.
club = baker.make(Club) club = baker.make(Club)
recipe = Recipe(ClubRole, club=club, name=seq("role ")) recipe = Recipe(ClubRole, club=club, name=seq("role "))
# bulk create initial roles (1 presidency, 1 board, 1 member) recipe.make(
roles = recipe.make(
is_board=iter([True, True, False]), is_board=iter([True, True, False]),
is_presidency=iter([True, False, False]), is_presidency=iter([True, False, False]),
order=iter([1, 2, 3]), order=iter([1, 2, 3]),
_quantity=3, _quantity=3,
_bulk_create=True, _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 # 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_a = recipe.make(is_board=True, is_presidency=True, order=None)
role_b = recipe.make(is_board=True, is_presidency=False, 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) role_c = recipe.make(is_board=False, is_presidency=False, order=None)