-
Notifications
You must be signed in to change notification settings - Fork 12
/
tests.py
51 lines (34 loc) · 1.76 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from django.test import TestCase, Client
from django.urls import reverse
from django.utils import timezone
from .models import Question, Choice
class PollsTest(TestCase):
def setUp(self):
self.client = Client()
self.question = Question.objects.create(question_text="Test Question", pub_date=timezone.now())
self.choice = Choice.objects.create(question=self.question, choice_text="Test Choice", votes=0)
def test_index_view(self):
response = self.client.get(reverse('polls:index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Question")
def test_detail_view(self):
response = self.client.get(reverse('polls:detail', args=(self.question.id,)))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Question")
def test_results_view(self):
response = self.client.get(reverse('polls:results', args=(self.question.id,)))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Question")
def test_vote_view(self):
response = self.client.post(reverse('polls:vote', args=(self.question.id,)), {'choice': self.choice.id})
self.assertEqual(response.status_code, 302) # Check for redirect
self.choice.refresh_from_db()
self.assertEqual(self.choice.votes, 1)
def test_question_viewset(self):
response = self.client.get(reverse('question-list'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Question")
def test_choice_viewset(self):
response = self.client.get(reverse('choice-list'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Choice")