annotate polls/tests/test_views.py @ 917:0365fdbb4d78

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