Skip to content

Commit

Permalink
Add poll tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Oct 15, 2023
1 parent 13a803c commit 7a4d48c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
17 changes: 12 additions & 5 deletions myhpi/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,25 @@ def serve(self, request, *args, **kwargs):
elif request.method == "POST" and self.can_vote(request.user):
choices = request.POST.getlist("choice")
if len(choices) == 0:
messages.error(request, "You must at least select one choice.")
messages.error(request, "You must select at least one choice.")
elif len(choices) > self.max_allowed_answers:
messages.error(
request,
"You can only select up to {} options.".format(self.max_allowed_answers),
)
else:
confirmed_choices = 0
for choice_id in choices:
choice = self.choices.filter(id=choice_id)
choice.update(votes=F("votes") + 1)
self.participants.add(request.user)
messages.success(request, "Your vote has been counted.")
choice = self.choices.filter(id=choice_id).first()
if choice and choice.page == self:
choice.votes += 1
choice.save()
confirmed_choices += 1
else:
messages.error(request, "Invalid choice.")
if confirmed_choices > 0:
self.participants.add(request.user)
messages.success(request, "Your vote has been counted.")
return redirect(self.relative_url(self.get_site()))

return super().serve(request, *args, **kwargs)
Expand Down
92 changes: 92 additions & 0 deletions myhpi/tests/core/test_polls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from datetime import datetime, timedelta

from myhpi.polls.models import Poll, PollChoice, PollList
from myhpi.tests.core.utils import MyHPIPageTestCase


class PollTests(MyHPIPageTestCase):
def setUp(self):
super().setUp()
self.poll_list = PollList(
title="Polls",
slug="polls",
path="0001000200010005",
depth=4,
is_public=True,
)
self.information_menu.add_child(instance=self.poll_list)

self.poll = Poll(
title="How are you?",
slug="how-are-you",
question="How are you?",
description="This is a poll to check how you are.",
start_date=datetime.now() - timedelta(days=1),
end_date=datetime.now() + timedelta(days=1),
max_allowed_answers=1,
results_visible=False,
is_public=True,
)

self.poll_list.add_child(instance=self.poll)

self.choice_good = PollChoice(
text="Good",
page=self.poll,
votes=0,
)
self.choice_good.save()
self.choice_bad = PollChoice(
text="Bad",
page=self.poll,
votes=0,
)
self.choice_bad.save()

def test_can_vote_once(self):
self.sign_in_as_student()
self.assertTrue(self.poll.can_vote(self.student))
self.poll.participants.add(self.student)
self.assertFalse(self.poll.can_vote(self.student))

def test_post_vote(self):
self.sign_in_as_student()
self.assertTrue(self.poll.can_vote(self.student))
self.client.post(
self.poll.url,
data={"choice": [self.choice_good.id]},
)
self.choice_good.refresh_from_db()
self.assertEqual(self.choice_good.votes, 1)
self.assertFalse(self.poll.can_vote(self.student))

def test_post_vote_invalid_choice(self):
self.sign_in_as_student()
self.assertTrue(self.poll.can_vote(self.student))
self.client.post(
self.poll.url,
data={"choice": [self.choice_good.id + 9999]},
)
self.choice_good.refresh_from_db()
self.assertEqual(self.choice_good.votes, 0)
self.assertTrue(self.poll.can_vote(self.student))

def test_post_vote_no_choice(self):
self.sign_in_as_student()
self.assertTrue(self.poll.can_vote(self.student))
response = self.client.post(
self.poll.url,
data={"choice": []},
)
self.assertContains(response, "You must select at least one choice.")
self.assertTrue(self.poll.can_vote(self.student))

def test_post_vote_too_many_choices(self):
self.sign_in_as_student()
self.assertTrue(self.poll.can_vote(self.student))
response = self.client.post(
self.poll.url,
data={"choice": [self.choice_good.id, self.choice_bad.id]},
)
self.assertContains(response, "You can only select up to 1 options.", 1)
self.assertTrue(self.poll.can_vote(self.student))

0 comments on commit 7a4d48c

Please sign in to comment.