Mercurial > public > sg101
comparison polls/tests/test_views.py @ 744:8789299c75b1
Django 1.6: test discovery as per unittest.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 29 Dec 2013 14:45:26 -0600 |
parents | polls/tests/view_tests.py@ee87ea74d46b |
children |
comparison
equal
deleted
inserted
replaced
743:66d46d31d543 | 744:8789299c75b1 |
---|---|
1 """ | |
2 Tests for the views in the polls application. | |
3 | |
4 """ | |
5 import datetime | |
6 | |
7 from django.contrib.auth.models import User | |
8 from django.test import TestCase | |
9 from django.core.urlresolvers import reverse | |
10 from polls.models import Poll, Choice | |
11 | |
12 | |
13 class PollHomeTest(TestCase): | |
14 | |
15 def setUp(self): | |
16 now = datetime.datetime.now() | |
17 Poll.objects.all().delete() | |
18 | |
19 past = now - datetime.timedelta(7) | |
20 future = now + datetime.timedelta(7) | |
21 | |
22 Poll.objects.create(start_date=past, | |
23 end_date=past, | |
24 is_enabled=True, | |
25 question='Old Poll') | |
26 | |
27 active = Poll.objects.create(start_date=past, | |
28 end_date=future, | |
29 is_enabled=True, | |
30 question='Active Poll') | |
31 | |
32 Poll.objects.create(start_date=future, | |
33 end_date=future, | |
34 is_enabled=True, | |
35 question='Future Poll') | |
36 | |
37 Poll.objects.create(start_date=past, | |
38 end_date=future, | |
39 is_enabled=False, | |
40 question='Disabled Poll') | |
41 | |
42 choice1 = Choice.objects.create(poll=active, choice="Yes") | |
43 choice2 = Choice.objects.create(poll=active, choice="Yes") | |
44 | |
45 self.username = 'test_user' | |
46 self.pw = 'password' | |
47 self.user = User.objects.create_user(self.username, '', self.pw) | |
48 self.user.save() | |
49 self.assertTrue(self.client.login(username=self.username, | |
50 password=self.pw)) | |
51 | |
52 def tearDown(self): | |
53 self.client.logout() | |
54 | |
55 def testHome(self): | |
56 response = self.client.get(reverse('polls-main')) | |
57 | |
58 old = response.context['old_polls'] | |
59 self.assertEqual(len(old), 1) | |
60 if old: | |
61 self.assertEqual(old[0].question, 'Old Poll') | |
62 | |
63 current = response.context['current_polls'] | |
64 self.assertEqual(len(current), 1) | |
65 if current: | |
66 self.assertEqual(current[0].question, 'Active Poll') | |
67 | |
68 self.assertNotContains(response, 'Future Poll') | |
69 self.assertNotContains(response, 'Disabled Poll') | |
70 | |
71 def testFuture(self): | |
72 poll = Poll.objects.get(question='Future Poll') | |
73 response = self.client.get(reverse('polls-detail', kwargs={'poll_id': | |
74 poll.id})) | |
75 self.assertEqual(response.status_code, 404) | |
76 | |
77 def testDisabled(self): | |
78 poll = Poll.objects.get(question='Disabled Poll') | |
79 response = self.client.get(reverse('polls-detail', kwargs={'poll_id': | |
80 poll.id})) | |
81 self.assertEqual(response.status_code, 404) | |
82 | |
83 def testOld(self): | |
84 poll = Poll.objects.get(question='Old Poll') | |
85 response = self.client.get(reverse('polls-detail', kwargs={'poll_id': | |
86 poll.id})) | |
87 self.assertEqual(response.status_code, 200) | |
88 | |
89 def testActive(self): | |
90 poll = Poll.objects.get(question='Active Poll') | |
91 response = self.client.get(reverse('polls-detail', kwargs={'poll_id': | |
92 poll.id})) | |
93 self.assertEqual(response.status_code, 200) | |
94 | |
95 def testVoteOld(self): | |
96 poll = Poll.objects.get(question='Old Poll') | |
97 response = self.client.get(reverse('polls-vote', kwargs={'poll_id': | |
98 poll.id}), follow=True) | |
99 | |
100 self.assertEqual(len(response.redirect_chain), 1) | |
101 if response.redirect_chain: | |
102 self.assertEqual(response.redirect_chain[0][0], | |
103 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) | |
104 self.assertEqual(response.redirect_chain[0][1], 302) | |
105 | |
106 self.assertEqual(response.status_code, 200) | |
107 | |
108 def testVoteActive(self): | |
109 poll = Poll.objects.get(question='Active Poll') | |
110 response = self.client.get(reverse('polls-vote', kwargs={'poll_id': | |
111 poll.id}), follow=True) | |
112 | |
113 self.assertEqual(len(response.redirect_chain), 0) | |
114 self.assertEqual(response.status_code, 200) | |
115 | |
116 def testVoteFuture(self): | |
117 poll = Poll.objects.get(question='Future Poll') | |
118 response = self.client.get(reverse('polls-vote', kwargs={'poll_id': | |
119 poll.id}), follow=True) | |
120 | |
121 self.assertEqual(len(response.redirect_chain), 1) | |
122 if response.redirect_chain: | |
123 self.assertEqual(response.redirect_chain[0][0], | |
124 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) | |
125 self.assertEqual(response.redirect_chain[0][1], 302) | |
126 | |
127 self.assertEqual(response.status_code, 404) | |
128 | |
129 def testVoteDisabled(self): | |
130 poll = Poll.objects.get(question='Disabled Poll') | |
131 response = self.client.get(reverse('polls-vote', kwargs={'poll_id': | |
132 poll.id}), follow=True) | |
133 | |
134 self.assertEqual(response.status_code, 404) | |
135 | |
136 def testVoteActivePost(self): | |
137 self._voteTest() | |
138 | |
139 def testDeleteVote(self): | |
140 poll = self._voteTest() | |
141 | |
142 response = self.client.post( | |
143 reverse('polls-delete_vote'), | |
144 {'poll_id': poll.id}, | |
145 follow=True) | |
146 | |
147 self.assertEqual(len(response.redirect_chain), 1) | |
148 if response.redirect_chain: | |
149 self.assertEqual(response.redirect_chain[0][0], | |
150 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) | |
151 self.assertEqual(response.redirect_chain[0][1], 302) | |
152 | |
153 self.assertEqual(response.status_code, 200) | |
154 | |
155 choice = Choice.objects.get(pk=1) | |
156 self.assertEqual(choice.votes, 0) | |
157 self.assertTrue(self.user not in choice.voters.all()) | |
158 | |
159 def _voteTest(self): | |
160 poll = Poll.objects.get(question='Active Poll') | |
161 response = self.client.post( | |
162 reverse('polls-vote', kwargs={'poll_id': poll.id}), | |
163 {'choices': 1}, | |
164 follow=True) | |
165 | |
166 self.assertEqual(len(response.redirect_chain), 1) | |
167 if response.redirect_chain: | |
168 self.assertEqual(response.redirect_chain[0][0], | |
169 'http://testserver' + reverse('polls-detail', kwargs={'poll_id': poll.id})) | |
170 self.assertEqual(response.redirect_chain[0][1], 302) | |
171 | |
172 self.assertEqual(response.status_code, 200) | |
173 | |
174 choice = Choice.objects.get(pk=1) | |
175 self.assertEqual(choice.votes, 1) | |
176 self.assertTrue(self.user in choice.voters.all()) | |
177 | |
178 return poll |