Skip to content

Commit

Permalink
Added basic POST tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastdriven committed Apr 16, 2011
1 parent 0473143 commit 9f5516a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions polls/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,37 @@ def test_results(self):
# Ensure that non-existent polls throw a 404.
resp = self.client.get('/polls/2/results/')
self.assertEqual(resp.status_code, 404)

def test_good_vote(self):
poll_1 = Poll.objects.get(pk=1)
self.assertEqual(poll_1.choice_set.get(pk=1).votes, 1)

resp = self.client.post('/polls/1/vote/', {'choice': 1})
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp['Location'], 'http://testserver/polls/1/results/')

self.assertEqual(poll_1.choice_set.get(pk=1).votes, 2)

def test_bad_votes(self):
# Ensure a non-existant PK throws a Not Found.
resp = self.client.post('/polls/1000000/vote/')
self.assertEqual(resp.status_code, 404)

# Sanity check.
poll_1 = Poll.objects.get(pk=1)
self.assertEqual(poll_1.choice_set.get(pk=1).votes, 1)

# Send no POST data.
resp = self.client.post('/polls/1/vote/')
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.context['error_message'], "You didn't select a choice.")

# Send junk POST data.
resp = self.client.post('/polls/1/vote/', {'foo': 'bar'})
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.context['error_message'], "You didn't select a choice.")

# Send a non-existant Choice PK.
resp = self.client.post('/polls/1/vote/', {'choice': 300})
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.context['error_message'], "You didn't select a choice.")

0 comments on commit 9f5516a

Please sign in to comment.