- add some testing to validate that voting can't be done multiple times
- add some testing to validate that results are shown when voting has already occurred
main
Guy Davis 7 months ago
parent ee7a8335b7
commit 45050dc4b5

1
.gitignore vendored

@ -1,2 +1,3 @@
.idea/ .idea/
db.sqlite3 db.sqlite3
staticfiles/

@ -3,6 +3,7 @@
{{ poll.question }} {{ poll.question }}
</div> </div>
<div> <div>
Options!
{% for option in options %} {% for option in options %}
<div <div
hx-post="{% url 'cast-vote' poll.pk option.pk %}" hx-post="{% url 'cast-vote' poll.pk option.pk %}"

@ -3,6 +3,7 @@
{{ poll.question }} {{ poll.question }}
</div> </div>
<div> <div>
Results!
{% for result in results %} {% for result in results %}
<div> <div>
{{ result.option.text }} - {{ result.count }} {{ result.option.text }} - {{ result.count }}

@ -1,3 +1,148 @@
from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from django.urls import reverse
# Create your tests here. from polls.models import Vote, Poll, Option
class PollDetailsTests(
TestCase,
):
def fresh_user_logged_in(
self,
):
test_user = User.objects.create_user(
"test",
password="test",
)
self.client.force_login(
test_user,
)
return test_user
def create_poll_with_questions(
self,
questions=3,
):
poll = Poll.objects.create(
question="Testing Question",
)
for i in range(questions):
Option.objects.create(
poll=poll,
text=f"Option #{i}",
)
return poll
def test_shows_options_if_not_voted(
self,
):
"""
If test user has not yet voted, options should be shown
"""
self.fresh_user_logged_in()
poll = self.create_poll_with_questions()
response = self.client.get(
reverse(
"poll-details",
kwargs={
"poll_id": poll.pk,
}
),
)
self.assertEqual(
response.status_code,
200,
)
self.assertContains(
response,
"Options!",
)
options = Option.objects.filter(
poll=poll,
)
self.assertQuerySetEqual(
response.context["options"],
options,
)
def test_shows_results_if_voted(
self,
):
"""
If test user has not yet voted, options should be shown
"""
test_user = self.fresh_user_logged_in()
poll = self.create_poll_with_questions()
options = Option.objects.filter(
poll=poll,
)
Vote.objects.create(
poll=poll,
option=options[0],
user=test_user,
)
response = self.client.get(
reverse(
"poll-details",
kwargs={
"poll_id": poll.pk,
}
),
)
self.assertEqual(
response.status_code,
200,
)
self.assertNotContains(
response,
"Options!",
)
self.assertContains(
response,
"Results!",
)
def test_re_voting_doesnt_change_votes(
self,
):
"""
If test user has not yet voted, options should be shown
"""
test_user = self.fresh_user_logged_in()
poll = self.create_poll_with_questions()
options = Option.objects.filter(
poll=poll,
)
Vote.objects.create(
poll=poll,
option=options[0],
user=test_user,
)
self.assertEqual(
Vote.objects.count(),
1,
)
response = self.client.post(
reverse(
"cast-vote",
kwargs={
"poll_id": poll.pk,
"option_id": options[1].pk,
}
),
)
self.assertEqual(
response.status_code,
200,
)
self.assertEqual(
Vote.objects.count(),
1,
)

@ -79,6 +79,8 @@ def poll_details(
context["poll"] = poll context["poll"] = poll
context["options"] = Option.objects.filter( context["options"] = Option.objects.filter(
poll=poll, poll=poll,
).order_by(
"pk",
) )
return render( return render(

Loading…
Cancel
Save

Powered by TurnKey Linux.