From fb8da93c68efd84ec9d6b88ff1976e5fa5f7db82 Mon Sep 17 00:00:00 2001 From: TitouanDor Date: Tue, 20 Jan 2026 21:01:23 +0100 Subject: [PATCH 1/4] add test_election_form --- election/tests.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/election/tests.py b/election/tests.py index 45ac3ea7..96a5aa67 100644 --- a/election/tests.py +++ b/election/tests.py @@ -115,3 +115,72 @@ def test_election_results(): "total vote": 100, }, } + + +@pytest.mark.django_db +def test_election_form(client : Client): + election = baker.make( + Election, + end_date = now() + timedelta(days=1), + ) + group = baker.make(Group) + election.vote_groups.add(group) + lists = baker.make(ElectionList, election=election, _quantity=2, _bulk_create=True) + roles = baker.make(Role, election=election, _quantity=2, _bulk_create=True) + users = baker.make(User, _quantity=4, _bulk_create=True) + cand = [ + baker.make(Candidature, role=roles[0], user=users[0], election_list=lists[0]), + baker.make(Candidature, role=roles[0], user=users[1], election_list=lists[1]), + baker.make(Candidature, role=roles[1], user=users[2], election_list=lists[0]), + baker.make(Candidature, role=roles[1], user=users[3], election_list=lists[1]), + ] + url = reverse("election:vote", kwargs={"election_id": election.id}) + + votes = [ + { + roles[0].title : "", + roles[1].title : str(cand[2].id), + }, + + { + roles[0].title : "", + roles[1].title : "", + }, + + { + roles[0].title : str(cand[0].id), + roles[1].title : str(cand[2].id), + }, + + { + roles[0].title : str(cand[0].id), + roles[1].title : str(cand[3].id), + }, + ] + + NB_VOTER = len(votes) + voters = [subscriber_user.make() for _ in range(NB_VOTER)] + + for i in range(NB_VOTER): + voter = voters[i] + voter.groups.add(group) + if not election.can_vote(voter): + assert False + client.force_login(voter) + reponse = client.post(url, data = votes[i]) + assert reponse.status_code == 302 + + assert election.results == { + roles[0].title: { + cand[0].user.username: {"percent": 50.0, "vote": 2}, + cand[1].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 50.0, "vote": 2}, + "total vote": 4, + }, + roles[1].title: { + cand[2].user.username: {"percent": 50.0, "vote": 2}, + cand[3].user.username: {"percent": 25.0, "vote": 1}, + "blank vote": {"percent": 25.0, "vote": 1}, + "total vote": 4, + }, + } \ No newline at end of file From c154b311c34c1ce4ad57626171fbbe2d70845d36 Mon Sep 17 00:00:00 2001 From: TitouanDor Date: Thu, 22 Jan 2026 13:30:08 +0100 Subject: [PATCH 2/4] add test with wrong data form --- election/tests.py | 95 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/election/tests.py b/election/tests.py index 96a5aa67..59cba5ba 100644 --- a/election/tests.py +++ b/election/tests.py @@ -1,6 +1,7 @@ from datetime import timedelta import pytest +from pytest_django.asserts import assertRedirects from django.conf import settings from django.test import Client, TestCase from django.urls import reverse @@ -118,13 +119,14 @@ def test_election_results(): @pytest.mark.django_db -def test_election_form(client : Client): +def test_election_good_form(client : Client): election = baker.make( Election, end_date = now() + timedelta(days=1), ) group = baker.make(Group) election.vote_groups.add(group) + election.edit_groups.add(group) lists = baker.make(ElectionList, election=election, _quantity=2, _bulk_create=True) roles = baker.make(Role, election=election, _quantity=2, _bulk_create=True) users = baker.make(User, _quantity=4, _bulk_create=True) @@ -158,18 +160,19 @@ def test_election_form(client : Client): }, ] - NB_VOTER = len(votes) - voters = [subscriber_user.make() for _ in range(NB_VOTER)] + voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) + group.users.set(voters) - for i in range(NB_VOTER): - voter = voters[i] - voter.groups.add(group) - if not election.can_vote(voter): - assert False + for voter, vote in zip(voters, votes): + assert election.can_vote(voter) client.force_login(voter) - reponse = client.post(url, data = votes[i]) - assert reponse.status_code == 302 - + response = client.post(url, data = vote) + assertRedirects( + response, + reverse("election:detail", kwargs={"election_id": election.id}) + ) + + assert set(election.voters.all()) == set(voters) assert election.results == { roles[0].title: { cand[0].user.username: {"percent": 50.0, "vote": 2}, @@ -183,4 +186,72 @@ def test_election_form(client : Client): "blank vote": {"percent": 25.0, "vote": 1}, "total vote": 4, }, - } \ No newline at end of file + } + + +@pytest.mark.django_db +def test_election_bad_form(client : Client): + election = baker.make( + Election, + end_date = now() + timedelta(days=1), + ) + group = baker.make(Group) + election.vote_groups.add(group) + election.edit_groups.add(group) + lists = baker.make(ElectionList, election=election, _quantity=2, _bulk_create=True) + roles = baker.make(Role, election=election, _quantity=2, _bulk_create=True) + users = baker.make(User, _quantity=5, _bulk_create=True) + cand = [ + baker.make(Candidature, role=roles[0], user=users[0], election_list=lists[0]), + baker.make(Candidature, role=roles[0], user=users[1], election_list=lists[1]), + baker.make(Candidature, role=roles[1], user=users[2], election_list=lists[0]), + baker.make(Candidature, role=roles[1], user=users[3], election_list=lists[1]), + ] + url = reverse("election:vote", kwargs={"election_id": election.id}) + + votes = [ + { + roles[0].title : "", + roles[1].title : str(cand[0].id), #wrong candidate + }, + + { + roles[0].title : "", + }, + + { + roles[0].title : "0123456789", #unkwon users + roles[1].title : str(users[4].id), #not a candidate + }, + + { + }, + ] + + voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) + group.users.set(voters) + + for voter, vote in zip(voters, votes): + assert election.can_vote(voter) + client.force_login(voter) + response = client.post(url, data = vote) + assertRedirects( + response, + reverse("election:detail", kwargs={"election_id": election.id}) + ) + + assert election.results == { + roles[0].title: { + cand[0].user.username: {"percent": 0.0, "vote": 0}, + cand[1].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 100.0, "vote": 2}, + "total vote": 2, + }, + roles[1].title: { + cand[2].user.username: {"percent": 0.0, "vote": 0}, + cand[3].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 100.0, "vote": 2}, + "total vote": 2, + }, + } + \ No newline at end of file From 33d4a99a2c920daf29933ff446e49aef056c1a2f Mon Sep 17 00:00:00 2001 From: TitouanDor Date: Sun, 8 Feb 2026 18:46:03 +0100 Subject: [PATCH 3/4] move form test into a class TestElectionForm --- election/tests.py | 271 ++++++++++++++++++++++------------------------ 1 file changed, 131 insertions(+), 140 deletions(-) diff --git a/election/tests.py b/election/tests.py index 59cba5ba..6ee12ec9 100644 --- a/election/tests.py +++ b/election/tests.py @@ -52,7 +52,137 @@ class TestElectionUpdateView(TestElection): ) assert response.status_code == 403 +class TestElectionForm(TestCase): + @classmethod + def setUpTestData(cls): + cls.election = baker.make( + Election, + end_date = now() + timedelta(days=1), + ) + cls.group = baker.make(Group) + cls.election.vote_groups.add(cls.group) + cls.election.edit_groups.add(cls.group) + lists = baker.make(ElectionList, election=cls.election, _quantity=2, _bulk_create=True) + cls.roles = baker.make(Role, election=cls.election, _quantity=2, _bulk_create=True) + users = baker.make(User, _quantity=4, _bulk_create=True) + cls.cand = [ + baker.make(Candidature, role=cls.roles[0], user=users[0], election_list=lists[0]), + baker.make(Candidature, role=cls.roles[0], user=users[1], election_list=lists[1]), + baker.make(Candidature, role=cls.roles[1], user=users[2], election_list=lists[0]), + baker.make(Candidature, role=cls.roles[1], user=users[3], election_list=lists[1]), + ] + cls.url = reverse("election:vote", kwargs={"election_id": cls.election.id}) + def test_election_good_form(self): + election = self.election + group = self.group + roles = self.roles + cand = self.cand + + votes = [ + { + roles[0].title : "", + roles[1].title : str(cand[2].id), + }, + + { + roles[0].title : "", + roles[1].title : "", + }, + + { + roles[0].title : str(cand[0].id), + roles[1].title : str(cand[2].id), + }, + + { + roles[0].title : str(cand[0].id), + roles[1].title : str(cand[3].id), + }, + ] + + voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) + group.users.set(voters) + + for voter, vote in zip(voters, votes): + assert election.can_vote(voter) + self.client.force_login(voter) + response = self.client.post(self.url, data = vote) + assertRedirects( + response, + reverse("election:detail", kwargs={"election_id": election.id}) + ) + + assert set(election.voters.all()) == set(voters) + assert election.results == { + roles[0].title: { + cand[0].user.username: {"percent": 50.0, "vote": 2}, + cand[1].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 50.0, "vote": 2}, + "total vote": 4, + }, + roles[1].title: { + cand[2].user.username: {"percent": 50.0, "vote": 2}, + cand[3].user.username: {"percent": 25.0, "vote": 1}, + "blank vote": {"percent": 25.0, "vote": 1}, + "total vote": 4, + }, + } + + def test_election_bad_form(self): + election = self.election + group = self.group + roles = self.roles + cand = self.cand + unknow_user = baker.make(User, _quantity=1, _bulk_create=True) + + votes = [ + { + roles[0].title : "", + roles[1].title : str(cand[0].id), #wrong candidate + }, + + { + roles[0].title : "", + }, + + { + roles[0].title : "0123456789", #unknow users + roles[1].title : str(unknow_user[0].id), #not a candidate + }, + + { + }, + ] + + voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) + group.users.set(voters) + + for voter, vote in zip(voters, votes): + assert election.can_vote(voter) + self.client.force_login(voter) + response = self.client.post(self.url, data = vote) + assertRedirects( + response, + reverse("election:detail", kwargs={"election_id": election.id}) + ) + + assert election.results == { + roles[0].title: { + cand[0].user.username: {"percent": 0.0, "vote": 0}, + cand[1].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 100.0, "vote": 2}, + "total vote": 2, + }, + roles[1].title: { + cand[2].user.username: {"percent": 0.0, "vote": 0}, + cand[3].user.username: {"percent": 0.0, "vote": 0}, + "blank vote": {"percent": 100.0, "vote": 2}, + "total vote": 2, + }, + } + + @pytest.mark.django_db def test_election_create_list_permission(client: Client): election = baker.make(Election, end_candidature=now() + timedelta(hours=1)) @@ -115,143 +245,4 @@ def test_election_results(): "blank vote": {"percent": 25.0, "vote": 25}, "total vote": 100, }, - } - - -@pytest.mark.django_db -def test_election_good_form(client : Client): - election = baker.make( - Election, - end_date = now() + timedelta(days=1), - ) - group = baker.make(Group) - election.vote_groups.add(group) - election.edit_groups.add(group) - lists = baker.make(ElectionList, election=election, _quantity=2, _bulk_create=True) - roles = baker.make(Role, election=election, _quantity=2, _bulk_create=True) - users = baker.make(User, _quantity=4, _bulk_create=True) - cand = [ - baker.make(Candidature, role=roles[0], user=users[0], election_list=lists[0]), - baker.make(Candidature, role=roles[0], user=users[1], election_list=lists[1]), - baker.make(Candidature, role=roles[1], user=users[2], election_list=lists[0]), - baker.make(Candidature, role=roles[1], user=users[3], election_list=lists[1]), - ] - url = reverse("election:vote", kwargs={"election_id": election.id}) - - votes = [ - { - roles[0].title : "", - roles[1].title : str(cand[2].id), - }, - - { - roles[0].title : "", - roles[1].title : "", - }, - - { - roles[0].title : str(cand[0].id), - roles[1].title : str(cand[2].id), - }, - - { - roles[0].title : str(cand[0].id), - roles[1].title : str(cand[3].id), - }, - ] - - voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) - group.users.set(voters) - - for voter, vote in zip(voters, votes): - assert election.can_vote(voter) - client.force_login(voter) - response = client.post(url, data = vote) - assertRedirects( - response, - reverse("election:detail", kwargs={"election_id": election.id}) - ) - - assert set(election.voters.all()) == set(voters) - assert election.results == { - roles[0].title: { - cand[0].user.username: {"percent": 50.0, "vote": 2}, - cand[1].user.username: {"percent": 0.0, "vote": 0}, - "blank vote": {"percent": 50.0, "vote": 2}, - "total vote": 4, - }, - roles[1].title: { - cand[2].user.username: {"percent": 50.0, "vote": 2}, - cand[3].user.username: {"percent": 25.0, "vote": 1}, - "blank vote": {"percent": 25.0, "vote": 1}, - "total vote": 4, - }, - } - - -@pytest.mark.django_db -def test_election_bad_form(client : Client): - election = baker.make( - Election, - end_date = now() + timedelta(days=1), - ) - group = baker.make(Group) - election.vote_groups.add(group) - election.edit_groups.add(group) - lists = baker.make(ElectionList, election=election, _quantity=2, _bulk_create=True) - roles = baker.make(Role, election=election, _quantity=2, _bulk_create=True) - users = baker.make(User, _quantity=5, _bulk_create=True) - cand = [ - baker.make(Candidature, role=roles[0], user=users[0], election_list=lists[0]), - baker.make(Candidature, role=roles[0], user=users[1], election_list=lists[1]), - baker.make(Candidature, role=roles[1], user=users[2], election_list=lists[0]), - baker.make(Candidature, role=roles[1], user=users[3], election_list=lists[1]), - ] - url = reverse("election:vote", kwargs={"election_id": election.id}) - - votes = [ - { - roles[0].title : "", - roles[1].title : str(cand[0].id), #wrong candidate - }, - - { - roles[0].title : "", - }, - - { - roles[0].title : "0123456789", #unkwon users - roles[1].title : str(users[4].id), #not a candidate - }, - - { - }, - ] - - voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) - group.users.set(voters) - - for voter, vote in zip(voters, votes): - assert election.can_vote(voter) - client.force_login(voter) - response = client.post(url, data = vote) - assertRedirects( - response, - reverse("election:detail", kwargs={"election_id": election.id}) - ) - - assert election.results == { - roles[0].title: { - cand[0].user.username: {"percent": 0.0, "vote": 0}, - cand[1].user.username: {"percent": 0.0, "vote": 0}, - "blank vote": {"percent": 100.0, "vote": 2}, - "total vote": 2, - }, - roles[1].title: { - cand[2].user.username: {"percent": 0.0, "vote": 0}, - cand[3].user.username: {"percent": 0.0, "vote": 0}, - "blank vote": {"percent": 100.0, "vote": 2}, - "total vote": 2, - }, - } - \ No newline at end of file + } \ No newline at end of file From b74b1ac691a84d129c97df9a948bbe8245215366 Mon Sep 17 00:00:00 2001 From: imperosol Date: Mon, 9 Feb 2026 15:23:50 +0100 Subject: [PATCH 4/4] refactor TestElectionForm --- election/tests.py | 155 ++++++++++++++++++---------------------------- 1 file changed, 61 insertions(+), 94 deletions(-) diff --git a/election/tests.py b/election/tests.py index 6ee12ec9..b4f78ff8 100644 --- a/election/tests.py +++ b/election/tests.py @@ -1,12 +1,13 @@ from datetime import timedelta import pytest -from pytest_django.asserts import assertRedirects from django.conf import settings from django.test import Client, TestCase from django.urls import reverse from django.utils.timezone import now from model_bakery import baker +from model_bakery.recipe import Recipe +from pytest_django.asserts import assertRedirects from core.baker_recipes import subscriber_user from core.models import Group, User @@ -52,137 +53,103 @@ class TestElectionUpdateView(TestElection): ) assert response.status_code == 403 + class TestElectionForm(TestCase): @classmethod def setUpTestData(cls): - cls.election = baker.make( - Election, - end_date = now() + timedelta(days=1), - ) + cls.election = baker.make(Election, end_date=now() + timedelta(days=1)) cls.group = baker.make(Group) cls.election.vote_groups.add(cls.group) cls.election.edit_groups.add(cls.group) - lists = baker.make(ElectionList, election=cls.election, _quantity=2, _bulk_create=True) - cls.roles = baker.make(Role, election=cls.election, _quantity=2, _bulk_create=True) + lists = baker.make( + ElectionList, election=cls.election, _quantity=2, _bulk_create=True + ) + cls.roles = baker.make( + Role, election=cls.election, _quantity=2, _bulk_create=True + ) users = baker.make(User, _quantity=4, _bulk_create=True) + recipe = Recipe(Candidature) cls.cand = [ - baker.make(Candidature, role=cls.roles[0], user=users[0], election_list=lists[0]), - baker.make(Candidature, role=cls.roles[0], user=users[1], election_list=lists[1]), - baker.make(Candidature, role=cls.roles[1], user=users[2], election_list=lists[0]), - baker.make(Candidature, role=cls.roles[1], user=users[3], election_list=lists[1]), + recipe.prepare(role=cls.roles[0], user=users[0], election_list=lists[0]), + recipe.prepare(role=cls.roles[0], user=users[1], election_list=lists[1]), + recipe.prepare(role=cls.roles[1], user=users[2], election_list=lists[0]), + recipe.prepare(role=cls.roles[1], user=users[3], election_list=lists[1]), ] - cls.url = reverse("election:vote", kwargs={"election_id": cls.election.id}) + Candidature.objects.bulk_create(cls.cand) + cls.vote_url = reverse("election:vote", kwargs={"election_id": cls.election.id}) + cls.detail_url = reverse( + "election:detail", kwargs={"election_id": cls.election.id} + ) def test_election_good_form(self): - election = self.election - group = self.group - roles = self.roles - cand = self.cand - + postes = (self.roles[0].title, self.roles[1].title) votes = [ - { - roles[0].title : "", - roles[1].title : str(cand[2].id), - }, - - { - roles[0].title : "", - roles[1].title : "", - }, - - { - roles[0].title : str(cand[0].id), - roles[1].title : str(cand[2].id), - }, - - { - roles[0].title : str(cand[0].id), - roles[1].title : str(cand[3].id), - }, + {postes[0]: "", postes[1]: str(self.cand[2].id)}, + {postes[0]: "", postes[1]: ""}, + {postes[0]: str(self.cand[0].id), postes[1]: str(self.cand[2].id)}, + {postes[0]: str(self.cand[0].id), postes[1]: str(self.cand[3].id)}, ] - voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) - group.users.set(voters) - - for voter, vote in zip(voters, votes): - assert election.can_vote(voter) + self.group.users.set(voters) + + for voter, vote in zip(voters, votes, strict=True): + assert self.election.can_vote(voter) self.client.force_login(voter) - response = self.client.post(self.url, data = vote) - assertRedirects( - response, - reverse("election:detail", kwargs={"election_id": election.id}) - ) - - assert set(election.voters.all()) == set(voters) - assert election.results == { - roles[0].title: { - cand[0].user.username: {"percent": 50.0, "vote": 2}, - cand[1].user.username: {"percent": 0.0, "vote": 0}, + response = self.client.post(self.vote_url, data=vote) + assertRedirects(response, self.detail_url) + + assert set(self.election.voters.all()) == set(voters) + assert self.election.results == { + postes[0]: { + self.cand[0].user.username: {"percent": 50.0, "vote": 2}, + self.cand[1].user.username: {"percent": 0.0, "vote": 0}, "blank vote": {"percent": 50.0, "vote": 2}, "total vote": 4, }, - roles[1].title: { - cand[2].user.username: {"percent": 50.0, "vote": 2}, - cand[3].user.username: {"percent": 25.0, "vote": 1}, + postes[1]: { + self.cand[2].user.username: {"percent": 50.0, "vote": 2}, + self.cand[3].user.username: {"percent": 25.0, "vote": 1}, "blank vote": {"percent": 25.0, "vote": 1}, "total vote": 4, }, } def test_election_bad_form(self): - election = self.election - group = self.group - roles = self.roles - cand = self.cand - unknow_user = baker.make(User, _quantity=1, _bulk_create=True) + postes = (self.roles[0].title, self.roles[1].title) votes = [ + {postes[0]: "", postes[1]: str(self.cand[0].id)}, # wrong candidate + {postes[0]: ""}, { - roles[0].title : "", - roles[1].title : str(cand[0].id), #wrong candidate - }, - - { - roles[0].title : "", - }, - - { - roles[0].title : "0123456789", #unknow users - roles[1].title : str(unknow_user[0].id), #not a candidate - }, - - { - }, + postes[0]: "0123456789", # unknow users + postes[1]: str(subscriber_user.make().id), # not a candidate + }, + {}, ] - voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True) - group.users.set(voters) + self.group.users.set(voters) - for voter, vote in zip(voters, votes): - assert election.can_vote(voter) + for voter, vote in zip(voters, votes, strict=True): self.client.force_login(voter) - response = self.client.post(self.url, data = vote) - assertRedirects( - response, - reverse("election:detail", kwargs={"election_id": election.id}) - ) + response = self.client.post(self.vote_url, data=vote) + assertRedirects(response, self.detail_url) - assert election.results == { - roles[0].title: { - cand[0].user.username: {"percent": 0.0, "vote": 0}, - cand[1].user.username: {"percent": 0.0, "vote": 0}, + assert self.election.results == { + postes[0]: { + self.cand[0].user.username: {"percent": 0.0, "vote": 0}, + self.cand[1].user.username: {"percent": 0.0, "vote": 0}, "blank vote": {"percent": 100.0, "vote": 2}, "total vote": 2, }, - roles[1].title: { - cand[2].user.username: {"percent": 0.0, "vote": 0}, - cand[3].user.username: {"percent": 0.0, "vote": 0}, + postes[1]: { + self.cand[2].user.username: {"percent": 0.0, "vote": 0}, + self.cand[3].user.username: {"percent": 0.0, "vote": 0}, "blank vote": {"percent": 100.0, "vote": 2}, "total vote": 2, }, } - - + + @pytest.mark.django_db def test_election_create_list_permission(client: Client): election = baker.make(Election, end_candidature=now() + timedelta(hours=1)) @@ -245,4 +212,4 @@ def test_election_results(): "blank vote": {"percent": 25.0, "vote": 25}, "total vote": 100, }, - } \ No newline at end of file + }