Ejemplo n.º 1
0
def submit_form(request):
    if ('going-to-vote' not in request.POST):
        return questionnaire_form(request, "Please say whether you're planning to vote")

    going_to_vote = request.POST['going-to-vote']
    constituency_id = request.POST['constituency']

    if (not constituency_id.isdigit()):
        return questionnaire_form(request, "Please select a constituency")

    constituency = Constituency.objects.get(id=constituency_id)
    parties = Party.objects.filter(abbreviation=request.POST['party'])
    party = None

    if (parties.count() > 0):
        party = parties[0]

    answer_set = AnswerSet(
        constituency=constituency,
        voting_for=party,
        going_to_vote=request.POST['going-to-vote']
    )
    answer_set.save()

    return HttpResponseRedirect(reverse('results', args=(constituency.id,)))
Ejemplo n.º 2
0
    def test_creation(self):
        """ Create answerset """

        constituency = Constituency(name="Merthyr Tydfil and Rhymney")
        constituency.save()

        party = Party(name="Plaid Cymru", abbreviation="PC")
        party.save()

        answers = AnswerSet(
            constituency=constituency,
            going_to_vote="yes",
            voting_for=party
        )
        answers.save()
Ejemplo n.º 3
0
 def test_get_latest_question_answers(self):
     '''
         This should return a list of only the latest QuestionAnswers that relate to this answerset
         this should be achieved by querying the LatestQuestionAnswer
     ''' 
     #generate an AnswerSet
     
     test_answer_set = AnswerSet(user=User.objects.create_user('test_user', '*****@*****.**', 'test'), 
                                 questionnaire=Questionnaire.objects.get(pk=1),
                                 questiongroup=QuestionGroup.objects.get(pk=1))
     #patch the LatestQuestionAnswer objects function
     with mock.patch('questionnaire.models.LatestQuestionAnswer.objects') as objects_patch:
         
         objects_patch.filter.return_value = [MagicMock(question_answer='questionAnswer1'),MagicMock(question_answer='questionAnswer2')]
         answers = test_answer_set.get_latest_question_answers()#pass the mock in as the self argument
     
         objects_patch.filter.assert_called_once_with(answer_set=test_answer_set)
         self.assertEqual(answers, ['questionAnswer1','questionAnswer2'])
Ejemplo n.º 4
0
 def test_save_override(self):
     '''
         The overriden save method should ensure that the correct question answert is stored in the 
         LatestQuestionAnswer table
         
         If there is no record for this question in this answer_set then create one with this question answer
         If there is a record, but the latest is not this then update it to make this the latest
         If there is a record, and it is already set to this do nothing
     '''
     #intially we need an answerset
     test_answer_set = AnswerSet(user=User.objects.create_user('test', '*****@*****.**', 'test') ,
                                  questionnaire=Questionnaire.objects.get(pk=1), questiongroup=QuestionGroup.objects.get(pk=1))
     
     test_answer_set.save()
     #initialy there shouldnt be any record in the latestquestionanswer so the function should create one
     an_answer = QuestionAnswer(question=Question.objects.get(pk=1),
                                answer='my answer',
                                answer_set = test_answer_set)
     an_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, an_answer)
     #now if we save a new question answer then the record should be updated
     
     a_new_answer = QuestionAnswer(question=Question.objects.get(pk=1),
                                answer='my new answer',
                                answer_set = test_answer_set)
     a_new_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, a_new_answer)
     a_new_answer_created = LatestQuestionAnswer.objects.latest('id').created
     
     #then if we save this question asnwer again then nothing should happen
     
     a_new_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, a_new_answer)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').created, a_new_answer_created)#the timestamp should'nt have changed