def create_polls_index(apps, schema_editor): from molo.core.models import Main from molo.polls.models import PollsIndexPage main = Main.objects.all().first() if main: polls_index = PollsIndexPage(title='Polls', slug='polls') main.add_child(instance=polls_index) polls_index.save_revision().publish()
def setUp(self): self.user = self.login() self.mk_main() # Creates Main language self.english = SiteLanguage.objects.create(locale='en') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish()
def setUp(self): self.superuser = User.objects.create_superuser( username='******', email='*****@*****.**', password='******', is_staff=True) self.mk_main() # Create Main language self.english = SiteLanguage.objects.create(locale='en') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() self.client = Client() self.client.login(username='******', password='******')
class ModelsTestCase(MoloTestCaseMixin, TestCase): def setUp(self): self.user = self.login() self.mk_main() # Creates Main language self.english = SiteLanguage.objects.create(locale='en') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() 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_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_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)
class ModelsTestCase(TestCase, MoloTestCaseMixin): def setUp(self): self.user = self.login() self.mk_main() # Creates Main language self.english = SiteLanguage.objects.create(locale='en') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() def test_download_csv_question(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 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,' '"yes,no"\r\n') self.assertEquals(str(response), expected_output) def test_choice_short_name(self): # make choices choice1 = Choice(title='yes', short_name='y') choice2 = Choice(title='no', short_name='n') # 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 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,n"\r\n') self.assertEquals(str(response), expected_output) 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_download_csv_free_text_question(self): question = FreeTextQuestion( title='is this a test') self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username='******', password='******') response = client.get('/') self.assertContains(response, 'is this a test') client.post(reverse('molo.polls:free_text_vote', kwargs={'question_id': question.id}), {'answer': 'this is an answer'}) 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,' 'this is an answer\r\n') self.assertEquals(str(response), expected_output) def test_download_csv_free_text_question_short_name(self): question = FreeTextQuestion( title='is this a test', short_name='short') self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username='******', password='******') response = client.get('/') self.assertContains(response, 'is this a test') client.post(reverse('molo.polls:free_text_vote', kwargs={'question_id': question.id}), {'answer': 'this is an answer'}) 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\nshort,' + date + ',superuser,' 'this is an answer\r\n') self.assertEquals(str(response), expected_output)
class ModelsTestCase(MoloTestCaseMixin, TestCase): def setUp(self): self.user = self.login() self.mk_main() # Creates Main language self.english = SiteLanguage.objects.create(locale='en') # Creates Child language self.french = SiteLanguage.objects.create(locale='fr') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() 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_translated_choice_exists(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() response = self.client.get(reverse( 'wagtailadmin_explore', args=[question.id])) self.assertContains(response, '<a href="/admin/pages/%s/edit/"' % page.id) 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_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_translated_free_text_question_exists(self): client = Client() client.login(username='******', password='******') question = FreeTextQuestion(title='what is this') self.polls_index.add_child(instance=question) question.save_revision().publish() self.client.post(reverse( 'add_translation', args=[question.id, 'fr'])) page = FreeTextQuestion.objects.get( slug='french-translation-of-what-is-this') 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_free_text_question_reply_stored_against_main_language(self): client = Client() client.login(username='******', password='******') question = FreeTextQuestion(title='what is this') self.polls_index.add_child(instance=question) question.save_revision().publish() self.client.post(reverse( 'add_translation', args=[question.id, 'fr'])) page = FreeTextQuestion.objects.get( slug='french-translation-of-what-is-this') page.save_revision().publish() client.post(reverse( 'molo.polls:free_text_vote', kwargs={'question_id': page.id}), {'answer': 'A test free text question '}) answer = FreeTextVote.objects.all().first() self.assertEquals(answer.question.id, question.id)
class TestQuestionResultsAdminView(TestCase, MoloTestCaseMixin): def setUp(self): self.superuser = User.objects.create_superuser( username='******', email='*****@*****.**', password='******', is_staff=True) self.mk_main() # Create Main language self.english = SiteLanguage.objects.create(locale='en') # Create polls index page self.polls_index = PollsIndexPage(title='Polls', slug='polls') self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() self.client = Client() self.client.login(username='******', password='******') def test_question_appears_in_wagtail_admin(self): question = Question(title='is this a test') self.polls_index.add_child(instance=question) response = self.client.get( '/admin/modeladmin/polls/question/' ) self.assertContains(response, question.title) 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) def test_freetextquestion_results_view(self): question = FreeTextQuestion(title='is this a test') self.polls_index.add_child(instance=question) question.save_revision().publish() free_text_vote = FreeTextVote(user=self.superuser, question=question, answer='yeah probably') free_text_vote.save() 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'), free_text_vote.answer, 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,yeah probably,superuser\r\n' % datetime.today().strftime('%Y-%m-%d') ) self.assertContains(response, expected_output)
class ModelsTestCase(TestCase, MoloTestCaseMixin): def setUp(self): self.user = self.login() self.mk_main() # Create Main language self.english = SiteLanguage.objects.create(locale="en") # Create polls index page self.polls_index = PollsIndexPage(title="Polls", slug="polls") self.main.add_child(instance=self.polls_index) self.polls_index.save_revision().publish() 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_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_results_as_total(self): # make choices choice1 = Choice(title="yes") # make a question question = Question(title="is this a test", result_as_percentage=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, "1 vote") 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_free_text_vote_successful(self): question = FreeTextQuestion(title="is this a test") self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") client.post( reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "this is an answer"} ) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "this is an answer") self.assertContains(response, "Thank you for voting!") response = client.get("/") self.assertContains(response, "already been submitted.") def test_numerical_text_vote_successful(self): question = FreeTextQuestion(title="is this a test", numerical=True) self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") client.post(reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "1234"}) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "1234") self.assertContains(response, "Thank you for voting!") response = client.get("/") self.assertContains(response, "already been submitted.") def test_numerical_text_vote_unsuccessful(self): question = FreeTextQuestion(title="is this a test", numerical=True) self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") response = client.post( reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "text answer"} ) self.assertEquals(FreeTextVote.objects.all().count(), 0) self.assertContains(response, "You did not enter a numerical value") response = client.get("/") self.assertNotContains(response, "already been submitted.") def test_free_text_vote_resubmission(self): question = FreeTextQuestion(title="is this a test") self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") client.post( reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "this is an answer"} ) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "this is an answer") response = client.post( reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "this is not an answer"}, ) self.assertRedirects(response, reverse("molo.polls:results", args=(question.id,))) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "this is an answer") def test_numerical_text_vote_resubmission(self): question = FreeTextQuestion(title="is this a test") self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") client.post(reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "1234"}) response = client.get(reverse("molo.polls:results", kwargs={"poll_id": question.id})) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "1234") response = client.post( reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id}), {"answer": "2345"} ) self.assertRedirects(response, reverse("molo.polls:results", args=(question.id,))) self.assertEquals(FreeTextVote.objects.all().count(), 1) self.assertEquals(FreeTextVote.objects.all()[0].answer, "1234") def test_free_text_vote_blank_answer(self): question = FreeTextQuestion(title="is this a test") self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") response = client.post(reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id})) self.assertContains(response, "field is required") self.assertEquals(FreeTextVote.objects.all().count(), 0) response = client.get("/") self.assertNotContains(response, "already been submitted.") def test_numerical_text_vote_blank_answer(self): question = FreeTextQuestion(title="is this a test") self.polls_index.add_child(instance=question) question.save_revision().publish() client = Client() client.login(username="******", password="******") response = client.get("/") self.assertContains(response, "is this a test") response = client.post(reverse("molo.polls:free_text_vote", kwargs={"question_id": question.id})) self.assertContains(response, "field is required") self.assertEquals(FreeTextVote.objects.all().count(), 0) response = client.get("/") self.assertNotContains(response, "already been submitted.")