Add Vote View Test and Recode it a bit + Seperate test

Change Model a bit and migrate back
Update Login, Signup UI a bit
This commit is contained in:
sosokker 2023-09-14 21:47:12 +07:00
parent aa0810fca8
commit b730333de3
16 changed files with 422 additions and 259 deletions

View File

@ -0,0 +1,17 @@
# Generated by Django 4.2.4 on 2023-09-14 12:47
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('polls', '0010_sentimentvote'),
]
operations = [
migrations.RemoveField(
model_name='vote',
name='question',
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 4.2.4 on 2023-09-14 13:15
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('polls', '0011_remove_vote_question'),
]
operations = [
migrations.AddField(
model_name='vote',
name='question',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='polls.question'),
),
]

View File

@ -0,0 +1,19 @@
# Generated by Django 4.2.4 on 2023-09-14 13:15
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('polls', '0012_vote_question'),
]
operations = [
migrations.AlterField(
model_name='vote',
name='question',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question'),
),
]

View File

@ -172,8 +172,8 @@ class Question(models.Model):
vote.update(vote_types=True)
self.save()
else:
return 'already_upvoted'
return 'ok'
return False
return True
def downvote(self, user):
@ -187,8 +187,8 @@ class Question(models.Model):
vote.update(vote_types=False)
self.save()
else:
return 'already_downvoted'
return 'ok'
return False
return True
class Choice(models.Model):

View File

@ -6,10 +6,6 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<script
src="https://unpkg.com/htmx.org@1.9.5"
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="{% static 'polls/js/detail.js' %}"></script>
<script src="{% static 'polls/base.css' %}"></script>

View File

@ -3,8 +3,13 @@
<!-- Navbar -->
<nav class="bg-blue-500 p-4 shadow-xl border-b-2 border-solid border-neutral-700">
<div class="container mx-auto flex items-center justify-between">
<div class="text-2xl font-bold text-white">📝KU POLL</div>
<div class="text-2xl font-bold text-white">
{% if user.is_authenticated %}
📝KU POLL | 👋 Hi! {{ user.username }}
{% else %}
📝KU POLL
{% endif %}
</div>
<!-- Button -->
<div>
{% comment %}

View File

@ -7,7 +7,7 @@
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
<div class="bg-white p-8 shadow-md rounded-md w-96">
<div class="bg-white p-8 shadow-md rounded-md w-96 border-solid border-neutral-500 border-2">
<h2 class="text-3xl font-semibold text-center mb-6">Sign In</h2>
<form method="post">
{% csrf_token %}

View File

@ -7,7 +7,7 @@
</head>
<body class="bg-gray-100">
<div class="min-h-screen flex items-center justify-center">
<div class="bg-white p-8 shadow-md rounded-md w-96">
<div class="bg-white p-8 shadow-md rounded-md w-96 border-solid border-neutral-500 border-2">
<h2 class="text-2xl font-semibold mb-6">Sign Up</h2>
<form method="post">
{% csrf_token %}

View File

@ -1,224 +0,0 @@
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
def test_was_published_recently_with_old_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
old_question = Question(pub_date=time)
self.assertIs(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self):
"""
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
self.assertIs(recent_question.was_published_recently(), True)
def test_is_published_with_future_question(self):
"""
is_published() should return False for questions whos pub_date is in the
future.
"""
future_date = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=future_date)
self.assertIs(future_question.is_published(), False)
def test_default_pub_date(self):
"""
Questions with the default pub_date (now) are displayed on the index page.
"""
question = Question.objects.create(question_text="Default pub date question.")
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_is_published_with_past_question(self):
"""
is_published() should return True for questions whose pub_date is in the
past.
"""
past_date = timezone.now() - datetime.timedelta(days=1)
past_question = Question(pub_date=past_date)
self.assertIs(past_question.is_published(), True)
def test_can_vote_with_question_not_ended(self):
"""
can_vote() should return True for questions that are published and have not
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ended(self):
"""
can_vote() should return False for questions that are published but have
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=2)
end_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), False)
def test_can_vote_with_question_no_end_date(self):
"""
can_vote() should return True for questions that are published and have no
specified end date.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=None)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ending_in_future(self):
"""
can_vote() should return True for questions that are published and
the current time is within the allowed voting period.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=2)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), True)
def create_question(self, question_text, days, pub_date=None):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + timezone.timedelta(days=days)
if pub_date is not None:
time = pub_date
return Question.objects.create(question_text=question_text, pub_date=time)
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse("polls:index"))
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
question = Question.objects.create(question_text="Past question.")
question.pub_date = timezone.now() - timezone.timedelta(days=30)
question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
past_question = Question.objects.create(question_text="Past question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=30)
past_question.save()
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[past_question],
)
def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = Question.objects.create(question_text="Past question 1.")
question1.pub_date = timezone.now() - timezone.timedelta(days=30)
question1.save()
question2 = Question.objects.create(question_text="Past question 2.")
question2.pub_date = timezone.now() - timezone.timedelta(days=5)
question2.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question2, question1],
)
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future
returns a 404 not found.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=5)
future_question.save()
user = User.objects.create_user(username="testcase", password="123test123")
self.client.login(username="testcase", password="123test123")
url = reverse("polls:detail", args=(future_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_past_question(self):
"""
The detail view of a question with a pub_date in the past
displays the question's text.
"""
past_question = Question.objects.create(question_text="Past Question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=5)
past_question.save()
user = User.objects.create_user(username="testcase", password="123test123")
self.client.login(username="testcase", password="123test123")
url = reverse("polls:detail", args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)

0
polls/tests/__init__.py Normal file
View File

14
polls/tests/base.py Normal file
View File

@ -0,0 +1,14 @@
from django.utils import timezone
from django.contrib.auth.models import User
from ..models import Question, Vote, Choice
def create_question(question_text, day=0):
"""
Create a question with the given `question_text` and published the
given number of `days` offset to now (negative for questions published
in the past, positive for questions that have yet to be published).
"""
time = timezone.now() + timezone.timedelta(days=day)
return Question.objects.create(question_text=question_text, pub_date=time)

View File

@ -0,0 +1,43 @@
from django.test import TestCase, Client
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
from .base import create_question
class QuestionDetailViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa")
cls.client = Client()
def test_future_question(self):
"""
The detail view of a question with a pub_date in the future
returns a 404 not found.
"""
future_question = create_question(question_text="Future question.", day=10)
future_question.save()
self.client.login(username=self.user.username, password="aaa123321aaa")
url = reverse("polls:detail", args=(future_question.id,))
respone = self.client.get(url)
self.assertEqual(respone.status_code, 404)
def test_past_question(self):
"""
The detail view of a question with a pub_date in the past
displays the question's text.
"""
past_question = create_question(question_text="Past Question.", day=-10)
past_question.save()
self.client.login(username=self.user.username, password='aaa123321aaa')
url = reverse("polls:detail", args=(past_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, past_question.question_text)

View File

@ -0,0 +1,77 @@
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from ..models import Question
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
"""
If no questions exist, an appropriate message is displayed.
"""
response = self.client.get(reverse("polls:index"))
self.assertEqual(response.status_code, 200)
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_past_question(self):
"""
Questions with a pub_date in the past are displayed on the
index page.
"""
question = Question.objects.create(question_text="Past question.")
question.pub_date = timezone.now() - timezone.timedelta(days=30)
question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_future_question(self):
"""
Questions with a pub_date in the future aren't displayed on
the index page.
"""
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(response.context["latest_question_list"], [])
def test_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
are displayed.
"""
past_question = Question.objects.create(question_text="Past question.")
past_question.pub_date = timezone.now() - timezone.timedelta(days=30)
past_question.save()
future_question = Question.objects.create(question_text="Future question.")
future_question.pub_date = timezone.now() + timezone.timedelta(days=30)
future_question.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[past_question],
)
def test_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = Question.objects.create(question_text="Past question 1.")
question1.pub_date = timezone.now() - timezone.timedelta(days=30)
question1.save()
question2 = Question.objects.create(question_text="Past question 2.")
question2.pub_date = timezone.now() - timezone.timedelta(days=5)
question2.save()
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question2, question1],
)

View File

@ -0,0 +1,105 @@
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from ..models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
def test_was_published_recently_with_old_question(self):
"""
was_published_recently() returns False for questions whose pub_date
is older than 1 day.
"""
time = timezone.now() - datetime.timedelta(days=1, seconds=1)
old_question = Question(pub_date=time)
self.assertIs(old_question.was_published_recently(), False)
def test_was_published_recently_with_recent_question(self):
"""
was_published_recently() returns True for questions whose pub_date
is within the last day.
"""
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
recent_question = Question(pub_date=time)
self.assertIs(recent_question.was_published_recently(), True)
def test_is_published_with_future_question(self):
"""
is_published() should return False for questions whos pub_date is in the
future.
"""
future_date = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=future_date)
self.assertIs(future_question.is_published(), False)
def test_default_pub_date(self):
"""
Questions with the default pub_date (now) are displayed on the index page.
"""
question = Question.objects.create(question_text="Default pub date question.")
response = self.client.get(reverse("polls:index"))
self.assertQuerySetEqual(
response.context["latest_question_list"],
[question],
)
def test_is_published_with_past_question(self):
"""
is_published() should return True for questions whose pub_date is in the
past.
"""
past_date = timezone.now() - datetime.timedelta(days=1)
past_question = Question(pub_date=past_date)
self.assertIs(past_question.is_published(), True)
def test_can_vote_with_question_not_ended(self):
"""
can_vote() should return True for questions that are published and have not
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ended(self):
"""
can_vote() should return False for questions that are published but have
ended.
"""
pub_date = timezone.now() - datetime.timedelta(hours=2)
end_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), False)
def test_can_vote_with_question_no_end_date(self):
"""
can_vote() should return True for questions that are published and have no
specified end date.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
question = Question(pub_date=pub_date, end_date=None)
self.assertIs(question.can_vote(), True)
def test_can_vote_with_question_ending_in_future(self):
"""
can_vote() should return True for questions that are published and
the current time is within the allowed voting period.
"""
pub_date = timezone.now() - datetime.timedelta(hours=1)
end_date = timezone.now() + datetime.timedelta(hours=2)
question = Question(pub_date=pub_date, end_date=end_date)
self.assertIs(question.can_vote(), True)

View File

@ -0,0 +1,86 @@
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth.models import User
from .base import create_question
from ..models import Vote, Choice
class VoteViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.question = create_question(question_text="Test Question", day=0)
cls.choice1 = Choice.objects.create(question=cls.question, choice_text="Test Choice 1")
cls.choice2 = Choice.objects.create(question=cls.question, choice_text="Test Choice 2")
cls.user = User.objects.create_user(username="test_user_123", password="aaa123321aaa")
cls.client = Client()
def test_vote_with_valid_choice(self):
"""
Test the vote view with a valid choice selection.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : self.choice1.id})
self.assertRedirects(response, reverse("polls:results", args=(self.question.id,)))
self.assertTrue(Vote.objects.filter(user=self.user, question=self.question).exists())
def test_vote_with_invalid_choice(self):
"""
Test the vote view with an invalid choice selection.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : 1000})
self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,)))
def test_vote_without_login(self):
"""
Test the vote view when the user is not logged in.
"""
response = self.client.post(reverse("polls:vote", args=(self.question.id,)),
{'choice' : self.choice1})
self.assertRedirects(response, "/accounts/login/?next=/polls/1/vote/")
def test_vote_voting_not_allowed(self):
"""
Test the vote view when voting is not allowed for the question.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
self.question_2 = create_question(question_text="Test not allow", day=10)
self.choice_2 = Choice.objects.create(question=self.question_2, choice_text="Test Choice 2_2")
response = self.client.post(reverse("polls:vote", args=(self.question_2.id,)),
{"choice" : self.choice_2.id})
self.assertRedirects(response, reverse('polls:index'))
def test_vote_with_no_post_data(self):
"""
Test the vote view when vote with no post data.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response = self.client.post(reverse("polls:vote", args=(self.question.id,)))
self.assertRedirects(response, reverse('polls:detail', args=(self.question.id,)))
def test_update_vote_when_vote_on_new_choice(self):
"""
Test the vote when same user vote on same question but change the choice.
"""
self.client.login(username=self.user.username, password="aaa123321aaa")
response_1 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice1.id})
self.assertRedirects(response_1, reverse('polls:results', args=(self.question.id,)))
response_2 = self.client.post(reverse("polls:vote", args=(self.question.id,)), {"choice": self.choice2.id})
self.assertRedirects(response_2, reverse('polls:results', args=(self.question.id,)))
self.assertFalse(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice1).exists())
self.assertTrue(Vote.objects.filter(user=self.user, question=self.question, choice=self.choice2).exists())

View File

@ -25,7 +25,7 @@ class IndexView(generic.ListView):
"""
now = timezone.now()
return Question.objects.filter(
Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None))
Q(pub_date__lte=now) & ((Q(end_date__gte=now) | Q(end_date=None)))
).order_by("-pub_date")
@ -42,7 +42,10 @@ class DetailView(LoginRequiredMixin, generic.DetailView):
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now())
now = timezone.now()
return Question.objects.filter(
Q(pub_date__lte=now) & (Q(end_date__gte=now) | Q(end_date=None))
).order_by("-pub_date")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@ -88,6 +91,7 @@ class SignUpView(generic.CreateView):
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
@login_required
def vote(request, question_id):
"""
@ -95,29 +99,31 @@ def vote(request, question_id):
in a specific question_id.
"""
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
messages.error(request, "You didn't select a choice.")
return render(request, "polls/detail.html", {"question": question})
else:
if request.method == "POST":
try:
selected_choice = question.choice_set.get(pk=request.POST["choice"])
except (KeyError, Choice.DoesNotExist):
messages.error(request, "You didn't select a choice.")
return redirect("polls:detail", question_id)
if question.can_vote():
if request.method == "POST" and "vote-button" in request.POST:
if Vote.objects.filter(user=request.user, question=question).exists():
old_vote = question.vote_set.get(user=request.user)
old_vote.choice = selected_choice
old_vote.save()
# ! Return 1. object element 2. boolean status of creation
vote, created = Vote.objects.update_or_create(
user=request.user,
question=question,
defaults={'choice' : selected_choice}
)
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
else:
Vote.objects.create(choice=selected_choice, user=request.user, question=question).save()
messages.success(request, "You vote successfully🥳")
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
if created:
messages.success(request, "You voted successfully🥳")
else:
messages.error(request, "You cannot vote by typing the URL.")
return render(request, "polls/detail.html", {"question": question})
messages.success(request, "You updated your vote🥳")
return redirect("polls:results", question_id)
else:
messages.error(request, "You can not vote on this question.")
return HttpResponseRedirect(reverse("polls:index"))
messages.error(request, "You cannot vote on this question.")
return redirect("polls:index")
else:
messages.error(request, "Invalid request method.")
return redirect("polls:index")