class NewPollPOSTTesting(TestCase): def setUp(self): self.usr = "******" self.a_user = UserFactory(username=self.usr) self.a_user.set_password(DEFAULT_PASSWORD) self.a_user.save() self.client.login(username=self.usr, password=DEFAULT_PASSWORD) self.a_question = 'a question?' self.a_choice = 'a choice' def tearDown(self): """Delete the created Poll instance.""" self.client.logout() self.a_user.delete() def test_valid_new_poll_inserts_only_one(self): """POST to edit_poll with valid data creates ONE new Poll.""" assert(len(Poll.objects.all()) == 0) valid_data = aux_initial_management_form(extra={ 'question': self.a_question, }) request = request_factory.post( reverse('polls:new_poll'), data = valid_data ) request.user = self.a_user response = views.edit_poll(request) self.assertEqual(Poll.objects.all().count(), 1) def test_valid_new_poll_redirects_to_voting(self): """POST to edit_poll with valid data. redirects to polls:voting.""" assert(len(Poll.objects.all()) == 0) valid_data = aux_initial_management_form(extra={'question': self.a_question}) response = self.client.post(reverse('polls:new_poll'), data = valid_data) self.assertRedirects( response, reverse('polls:voting', kwargs={'poll_id':Poll.objects.get(pk=1).pk}) ) def test_valid_new_poll_inserts_correct_question(self): """POST to edit_poll with valid data creates the correct Poll.""" valid_data = aux_initial_management_form(extra={ 'question': self.a_question, }) request = request_factory.post( reverse('polls:new_poll'), data = valid_data ) request.user = self.a_user response = views.edit_poll(request) self.assertEqual(Poll.objects.get(pk=1).question, self.a_question) def test_valid_new_poll_inserts_correct_choices(self): """POST to edit_poll with valid data creates the correct Choices.""" choices_values = ['a', 'b', 'c', 'd'] valid_data = aux_initial_management_form(total_forms=4, extra={ 'question': self.a_question, "choice_set-0-ORDER" : 1, "choice_set-0-choice" : choices_values[0], "choice_set-1-ORDER" : 2, "choice_set-1-choice" : choices_values[1], "choice_set-2-ORDER" : 3, "choice_set-2-choice" : choices_values[2], "choice_set-3-ORDER" : 4, "choice_set-3-choice" : choices_values[3], }) request = request_factory.post( reverse('polls:new_poll'), data = valid_data, ) request.user = self.a_user response = views.edit_poll(request) db_values = [d.choice for d in Choice.objects.all()] self.assertItemsEqual(choices_values, db_values) def test_new_poll_empty_question_fails(self): """POST to edit_poll with an empty question, results in an invalid form.""" assert(Poll.objects.all().count() == 0) # No extra data (such as the 'question' value) invalid_data = aux_initial_management_form() response = self.client.post(reverse('polls:new_poll'), data = invalid_data) self.assertFalse(response.context['poll_form'].is_valid()) def test_new_poll_empty_question_shows_specific_message(self): """POST to edit_poll with an empty question, renders a specific msg""" # No extra data (such as the 'question' value) invalid_data = aux_initial_management_form() request = request_factory.post( reverse('polls:new_poll'), data = invalid_data, ) request.user = self.a_user response = views.edit_poll(request) response.render() self.assertContains(response, html.escape(forms.EMPTY_QUESTION_MSG)) def test_new_poll_empty_question_dont_inserts_in_DB(self): """POST to edit_poll with an empty question, does not insert a new poll in the DB.""" assert(len(Poll.objects.all()) == 0) # No extra data (such as the 'question' value) invalid_data = aux_initial_management_form() response = self.client.post(reverse('polls:new_poll'), data = invalid_data) self.assertEqual(Poll.objects.all().count(), 0) def test_new_poll_duplicated_choices_fail(self): """POST to edit_poll with an empty question, renders a specific msg""" # No extra data (such as the 'question' value) choices_values = ['a', 'a'] invalid_data = aux_initial_management_form(total_forms=2, extra={ 'question': self.a_question, "choice_set-0-ORDER" : 1, "choice_set-0-choice" : choices_values[0], "choice_set-1-ORDER" : 2, "choice_set-1-choice" : choices_values[1], }) response = self.client.post(reverse('polls:new_poll'), data = invalid_data) self.assertFalse(response.context['choices_formset'].is_valid())