diff --git a/.gitignore b/.gitignore index 8ad47b1..d24328c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ -db.sqlite3 \ No newline at end of file +db.sqlite3 +staticfiles/ \ No newline at end of file diff --git a/polls/templates/polls/poll_details.html b/polls/templates/polls/poll_details.html index b962c32..da30adf 100644 --- a/polls/templates/polls/poll_details.html +++ b/polls/templates/polls/poll_details.html @@ -3,6 +3,7 @@ {{ poll.question }}
+ Options! {% for option in options %}
+ Results! {% for result in results %}
{{ result.option.text }} - {{ result.count }} diff --git a/polls/tests.py b/polls/tests.py index 7ce503c..e9cda6f 100644 --- a/polls/tests.py +++ b/polls/tests.py @@ -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, + ) diff --git a/polls/views.py b/polls/views.py index 83bf9b3..1e8e4f5 100644 --- a/polls/views.py +++ b/polls/views.py @@ -79,6 +79,8 @@ def poll_details( context["poll"] = poll context["options"] = Option.objects.filter( poll=poll, + ).order_by( + "pk", ) return render(