- 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 occurredmain
parent
ee7a8335b7
commit
45050dc4b5
@ -1,2 +1,3 @@
|
||||
.idea/
|
||||
db.sqlite3
|
||||
staticfiles/
|
@ -1,3 +1,148 @@
|
||||
from django.contrib.auth.models import User
|
||||
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,
|
||||
)
|
||||
|
Loading…
Reference in new issue