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
def test_answerset_avaialble(self, has_permission_mock): ''' If everything goes well: 1. If there is no answerset available then then there should be an empty list keyed against question_answers in the context. 2. If there is an answerset then the get_latest_question_Answer_in_order function on the answerset should be called, and it should put the returned list in the context, keyed with "question_answers" ''' #first, when there is no answerset present: self.carryout_shared_assertions([]) #now setup the answerset AnswerSet.objects.create( user=self.testUser, questionnaire=self.test_questionnaire, questiongroup=self.test_group) with patch('applicationform.views.AnswerSet.get_latest_question_answer_in_order') as answer_set_mock: answer_set_mock.return_value = [QuestionAnswer()] self.carryout_shared_assertions(answer_set_mock.return_value)