def test_repo_get_answers(self): self._seed_data() # retrieve using response_id # first answer should have correct response_id and question_id answers_by_response = response_repository.get_answers_by_response_id( self.test_response.id) self.assertEqual(answers_by_response[0].response_id, self.test_response.id) self.assertEqual(answers_by_response[0].question_id, self.test_question.id) # retrieve using question_id # test the first answer here is the same as first answer above # test_question completed by dummy users and other_user answers_by_question = response_repository.get_answers_by_question_id( self.test_question.id) self.assertEqual(answers_by_question[0], answers_by_response[0]) self.assertEqual( len(self.test_users) + 1, len(answers_by_question)) # including other_user response # retrieve using response_id and question_id # use same question_id and response_id as previous test --> can test object is the same answer_by_qid_rid = response_repository.get_answer_by_question_id_and_response_id( self.test_question.id, self.test_response.id) self.assertEqual(answer_by_qid_rid, answers_by_response[0]) # use different ids. Check expected ids are in the returned answer answer_by_qid_rid_q2 = response_repository.get_answer_by_question_id_and_response_id( self.test_question2.id, self.responses[0].id) self.assertEqual(answer_by_qid_rid_q2.response_id, self.responses[0].id) self.assertEqual(answer_by_qid_rid_q2.question_id, self.test_question2.id) # retrieve all (Question, Answer) tuples using section_id and response_id qa_by_sid_rid = response_repository.get_question_answers_by_section_key_and_response_id( self.test_section.key, self.test_response.id) self.assertTupleEqual(qa_by_sid_rid[0], (self.test_question, self.test_answer1)) other_response = self.responses[0] qa_by_sid_rid_dummy = response_repository.get_question_answers_by_section_key_and_response_id( self.test_section.key, other_response.id) answer1 = response_repository.get_answer_by_question_id_and_response_id( self.test_question.id, other_response.id) answer2 = response_repository.get_answer_by_question_id_and_response_id( self.test_question2.id, other_response.id) self.assertEqual(len(qa_by_sid_rid_dummy), 2) # answered 2 questions self.assertTupleEqual(qa_by_sid_rid_dummy[0], (self.test_question, answer1)) self.assertTupleEqual(qa_by_sid_rid_dummy[1], (self.test_question2, answer2))
def put(self): args = self.put_req_parser.parse_args() user_id = g.current_user['id'] is_submitted = args['is_submitted'] language = args['language'] response = response_repository.get_by_id(args['id']) if not response: return errors.RESPONSE_NOT_FOUND if response.user_id != user_id: return errors.UNAUTHORIZED if response.application_form_id != args['application_form_id']: return errors.UPDATE_CONFLICT response.is_submitted = is_submitted response.language = language if is_submitted: response.submit() response_repository.save(response) answers = [] for answer_args in args['answers']: answer = response_repository.get_answer_by_question_id_and_response_id( answer_args['question_id'], response.id) if answer: answer.update(answer_args['value']) else: answer = Answer(response.id, answer_args['question_id'], answer_args['value']) answers.append(answer) response_repository.save_answers(answers) try: if response.is_submitted: LOGGER.info( 'Sending confirmation email for response with ID : {id}'. format(id=response.id)) user = user_repository.get_by_id(user_id) response = response_repository.get_by_id_and_user_id( response.id, user_id) self.send_confirmation(user, response) except: LOGGER.warn( 'Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully' .format(id=response.id)) finally: return response, 200