def test_choice_short_name_single_choice(self): # make choices choice1 = Choice(title='yes', short_name='y') # make a question question = Question( title='is this a test', allow_multiple_choice=True, show_results=False) self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.save_revision().publish() # make a vote client = Client() client.login(username='******', password='******') client.post(reverse('molo.polls:vote', kwargs={'question_id': question.id}), {'choice': choice1.id}) # should automatically create the poll vote # test poll vote response = download_as_csv(QuestionAdmin(Question, self.site), None, Question.objects.all()) date = str(datetime.datetime.now().date()) expected_output = ('Content-Type: text/csv\r\nContent-Disposition:' ' attachment;filename=questions-' + date + '.csv\r\n\r\n' 'title,date_submitted,user,answer' '\r\nis this a test,' + date + ',superuser,' 'y\r\n') self.assertEquals(str(response), expected_output)
def test_question_choices(self): choice1 = Choice(title='yes') choice2 = Choice(title='no') choice3 = Choice(title='maybe') choice4 = Choice(title='definitely') choice5 = Choice(title='idk') question = Question(title='is this a test', randomise_options=True) self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.add_child(instance=choice2) question.add_child(instance=choice3) question.add_child(instance=choice4) question.add_child(instance=choice5) choices = question.choices() self.assertEqual(choices.get(title='yes'), choice1) self.assertEqual(choices.get(title='no'), choice2) self.assertEqual(choices.get(title='maybe'), choice3) self.assertEqual(choices.get(title='definitely'), choice4) self.assertEqual(choices.get(title='idk'), choice5) question.randomise_options = False choices = question.choices() self.assertEqual(choices.all().first(), choice1) self.assertEqual(choices.all().last(), choice5)
def test_multiple_options(self): # make choices choice1 = Choice(title="yes") choice2 = Choice(title="no") # make a question question = Question(title="is this a test", allow_multiple_choice=True, show_results=False) self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.add_child(instance=choice2) question.save_revision().publish() # make a vote client = Client() client.login(username="******", password="******") client.post( reverse("molo.polls:vote", kwargs={"question_id": question.id}), {"choice": [choice1.id, choice2.id]} ) # should automatically create the poll vote # test poll vote vote_count1 = ChoiceVote.objects.all()[0].choice.all()[0].votes self.assertEquals(vote_count1, 1) vote_count2 = ChoiceVote.objects.all()[0].choice.all()[1].votes self.assertEquals(vote_count2, 1) response = client.get("/") self.assertContains(response, "You voted: yes, no")
def test_user_not_allow_to_vote_in_other_languages_once_voted(self): client = Client() client.login(username='******', password='******') choice1 = Choice(title='yes') question = Question(title='is this a test') self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.save_revision().publish() self.client.post(reverse( 'add_translation', args=[choice1.id, 'fr'])) page = Choice.objects.get( slug='french-translation-of-yes') page.save_revision().publish() client.post(reverse('molo.polls:vote', kwargs={'question_id': question.id}), {'choice': choice1.id}) response = self.client.get('/') self.assertContains(response, 'Show Results') response = self.client.get('/locale/fr/') response = self.client.get('/') self.assertContains(response, 'Show Results')
def test_voting_once_only(self): # make choices choice1 = Choice(title="yes") # make a question question = Question(title="is this a test") self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.save_revision().publish() # make a vote client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") response = client.post(reverse("molo.polls:vote", kwargs={"question_id": question.id})) self.assertContains(response, "select a choice") response = client.post(reverse("molo.polls:vote", kwargs={"question_id": question.id}), {"choice": choice1.id}) # should automatically create the poll vote # test poll vote vote_count = ChoiceVote.objects.all()[0].choice.all()[0].votes self.assertEquals(vote_count, 1) self.assertEquals(ChoiceVote.objects.all()[0].choice.all()[0].choice_votes.count(), 1) # vote again and test that it does not add to vote_count client.post(reverse("molo.polls:vote", kwargs={"question_id": question.id}), {"choice": choice1.id}) # should automatically create the poll vote # test poll vote vote_count = ChoiceVote.objects.all()[0].choice.all()[0].votes self.assertEquals(vote_count, 1) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertContains(response, "100%")
def test_poll_vote(self): # make choices choice1 = Choice(title='yes') # make a question question = Question(title='is this a test') self.polls_index.add_child(instance=question) question.add_child(instance=choice1) # make a vote client = Client() client.login(username='******', password='******') client.post(reverse('molo.polls:vote', kwargs={'question_id': question.id}), {'choice': choice1.id}) # should automatically create the poll vote # test poll vote vote_count = ChoiceVote.objects.all()[0].choice.all()[0].votes self.assertEquals(vote_count, 1)
def test_translated_question_exists(self): client = Client() client.login(username='******', password='******') question = Question(title='is this a test') self.polls_index.add_child(instance=question) question.save_revision().publish() self.client.post(reverse( 'add_translation', args=[question.id, 'fr'])) page = Question.objects.get( slug='french-translation-of-is-this-a-test') page.save_revision().publish() response = self.client.get(reverse( 'wagtailadmin_explore', args=[self.polls_index.id])) self.assertContains(response, '<a href="/admin/pages/%s/edit/"' % page.id)
def test_show_results(self): # make choices choice1 = Choice(title="yes") # make a question question = Question(title="is this a test", show_results=False) self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.save_revision().publish() # make a vote client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") client.post(reverse("molo.polls:vote", kwargs={"question_id": question.id}), {"choice": choice1.id}) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertContains(response, "Thank you for voting!") response = client.get("/") self.assertContains(response, "You voted")
def test_section_page_question(self): section = SectionPage( title='section', slug='section', extra_style_hints='purple') self.main.add_child(instance=section) section.save_revision().publish() question = Question(title='is this a test') section.add_child(instance=question) question.save_revision().publish() # make a vote client = Client() client.login(username='******', password='******') response = client.get('/') self.assertContains(response, 'section') response = self.client.get( '/section/') self.assertContains(response, "is this a test") self.assertEquals(section.get_effective_extra_style_hints(), 'purple') self.assertEquals(question.get_effective_extra_style_hints(), 'purple')
def test_votes_stored_against_main_language_question(self): client = Client() client.login(username='******', password='******') choice1 = Choice(title='yes') question = Question(title='is this a test') self.polls_index.add_child(instance=question) question.add_child(instance=choice1) question.save_revision().publish() self.client.post(reverse( 'add_translation', args=[choice1.id, 'fr'])) page = Choice.objects.get( slug='french-translation-of-yes') page.save_revision().publish() client.post(reverse('molo.polls:vote', kwargs={'question_id': question.id}), {'choice': choice1.id}) vote = ChoiceVote.objects.all().first() self.assertEqual(vote.choice.all().first().id, choice1.id)
def test_question_results_view(self): question = Question(title='is this a test') self.polls_index.add_child(instance=question) choice1 = Choice(title='yes') question.add_child(instance=choice1) choice2 = Choice(title='no') question.add_child(instance=choice2) question.save_revision().publish() choice_vote = ChoiceVote(user=self.superuser, question=question) choice_vote.save() choice_vote.choice.add(choice1) choice1.choice_votes.add(choice_vote) response = self.client.get( '/admin/polls/question/{0}/results/'.format(question.id) ) expected_headings_html = '<tr><th>Submission Date</th><th>Answer</th>'\ '<th>User</th></tr>' expected_data_html = '<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>'\ .format(datetime.today().strftime('%B %d, %Y'), choice1.title, self.superuser.username) self.assertContains(response, expected_headings_html, html=True) self.assertContains(response, expected_data_html, html=True) # test CSV download response = self.client.get( '/admin/polls/question/{0}/results/?action=download'.format( question.id) ) expected_output = ( 'Submission Date,Answer,User\r\n' '%s,yes,superuser\r\n' % datetime.today().strftime('%Y-%m-%d') ) self.assertContains(response, expected_output)