class VotingFormTesting(TestCase): def setUp(self): self.poll = PollFactory() def tearDown(self): """Delete the created Poll instance.""" self.poll.delete() def test_voting_form_has_good_choices(self): """The choices in the VotingForm object are correct.""" # Create a couple of Choices to check if they are in the form's queryset. c1 = ChoiceFactory(poll=self.poll) c2 = ChoiceFactory(poll=self.poll) form = forms.VoteForm(poll=self.poll) self.assertQuerysetEqual( form.fields['choice'].queryset, [c1.id, c2.id], transform=lambda c:c.id )
class PollsIndexViewsTestCase(TestCase): def setUp(self): self.poll = PollFactory() def tearDown(self): self.poll.delete() def test_index(self): request = request_factory.get(reverse('polls:index')) response = views.PollsIndex.as_view()(request) self.assertEqual(response.status_code, 200) def test_archive_index(self): """The listchoices template is used in the archive view""" request = request_factory.get(reverse('polls:archive')) response = views.PollsArchiveView.as_view()(request) self.assertTemplateUsed(response, "polls/listchoices.html") def test_archive_index_noPOST(self): """The archive view doesn't accept POST method""" request = request_factory.post(reverse('polls:archive')) response = views.PollsArchiveView.as_view()(request) self.assertEqual(response.status_code, 405)
class PollsModelTesting(TestCase): def setUp(self): self.poll = PollFactory() def tearDown(self): self.poll.delete() def test_get_max_votes_no_voted(self): """A poll with no votes, get_max_votes returns 0.""" self.assertEqual(self.poll.get_max_votes(), 0) def test_get_max_votes_nominal(self): """get_max_votes returns the number of votes of the most voted choice.""" c_max = ChoiceFactory(poll = self.poll, choice = "A winner choice") c_max.vote_me() c_max.vote_me() c_max.vote_me() c_med = ChoiceFactory(poll = self.poll, choice = "A choice") c_med.vote_me() c_med.vote_me() c_min = ChoiceFactory(poll = self.poll, choice = "A looser choice") c_min.vote_me() self.assertEqual(self.poll.get_max_votes(), 3) def test_has_winners_returns_none_if_no_choice(self): """If the poll has not choices, has_winners resturns [].""" self.assertItemsEqual(self.poll.has_winners(), []) def test_has_winners_only_one_winning_choice(self): """If the poll has a single winner, has_winners returns it.""" c_max = ChoiceFactory(poll = self.poll, choice = "A winner choice") c_max.vote_me() c_max.vote_me() c_med = ChoiceFactory(poll = self.poll, choice = "A choice") c_med.vote_me() self.assertItemsEqual(self.poll.has_winners(), [c_max]) def test_has_winners_multiple_winning_choices(self): """If the poll has many winners, has_winners returns all of them.""" winner_1 = ChoiceFactory(poll = self.poll) winner_1.vote_me() winner_1.vote_me() winner_2 = ChoiceFactory(poll = self.poll) winner_2.vote_me() winner_2.vote_me() looser = ChoiceFactory(poll = self.poll) looser.vote_me() self.assertItemsEqual(self.poll.has_winners(), [winner_2, winner_1])
def setUp(self): self.poll = PollFactory() self.c1 = ChoiceFactory(poll=self.poll) self.c2 = ChoiceFactory(poll=self.poll)
class PollVoteTesting(TestCase): def setUp(self): self.poll = PollFactory() self.c1 = ChoiceFactory(poll=self.poll) self.c2 = ChoiceFactory(poll=self.poll) def tearDown(self): """Delete the created Poll instance.""" self.poll.delete() def test_vote_calls_choice_vote_me(self): """polls:emit_vote calls the right Choice vote_me method.""" request = request_factory.post( reverse('polls:emit_vote', kwargs={'poll_id':self.poll.id}), data = {'choice':self.c1.id} ) with patch.object(Choice, 'vote_me', autospec=True) as mock_vote_me: mock_vote_me.return_value = None response = views.PollVote.as_view()(request, poll_id = self.poll.id) args, kwargs = mock_vote_me.call_args_list[0] target_choice = args[0] self.assertEqual(target_choice.id, self.c1.id) def test_vote_redirects_ok(self): """Succesful voting redirects to results page.""" data = {'choice':self.c1.id} response = self.client.post( reverse('polls:emit_vote', kwargs={'poll_id':self.poll.id}), data = data ) self.assertRedirects( response, reverse('polls:results', kwargs={'poll_id':self.poll.id}), ) def test_vote_nonExistant(self): """Voting for a non-existing poll responds a 404.""" inexisten_id = 10000 data = {'choice':self.c1.id} request = request_factory.post( reverse('polls:emit_vote', kwargs={'poll_id':inexisten_id}), data = data, ) with self.assertRaises(Http404): response = views.PollVote.as_view()(request, poll_id = inexisten_id) self.assertEqual(response.status_code, 404) def test_vote_without_data(self): """Voting with empty form-data responds a form with errors.""" data = {} request = request_factory.post( reverse('polls:emit_vote', kwargs={'poll_id':self.poll.id}), data = data, ) response = views.PollVote.as_view()(request, poll_id = self.poll.id) self.assertContains(response, u"You must select a choice to vote.") def test_vote_invalid_choice(self): """Voting for a wrong choice responds a form with errors.""" wrong_choice = 505050 data = {'choice':wrong_choice} request = request_factory.post( reverse('polls:emit_vote', kwargs={'poll_id':self.poll.id}), data = data, ) response = views.PollVote.as_view()(request, poll_id = self.poll.id) print response self.assertContains(response, u"Select a valid choice.")
def setUp(self): self.poll = PollFactory()