def test_remove_only_question(self): response = set_questions([{ 'text': 'Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) questions = get_questions(current_user=self.admin) self.assertEqual(len(questions), 1) for question_id, text in questions.items(): response = remove_question(question_id, current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) questions = get_questions(current_user=self.admin) self.assertEqual(len(questions), 0)
def test_multiple_questions_as_admin(self): for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() result = get_questions(current_user=self.admin) self.assertIsInstance(result, dict) self.assertEqual(len(result), 3)
def test_one_question(self): question = Question(text='Question 1?') db.session.add(question) db.session.commit() result = get_questions() self.assertIsInstance(result, dict) self.assertEqual(len(result), 1) self.assertEqual(list(result.values())[0], 'Question 1?')
def test_remove_one_question(self): response = set_questions([{ 'text': 'Question?' }, { 'text': 'Another Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) questions = get_questions(current_user=self.admin) self.assertEqual(len(questions), 2) for question_id, text in questions.items(): if text == 'Question?': break remove_question(question_id, current_user=self.admin) questions = get_questions(current_user=self.admin) self.assertEqual(len(questions), 1) self.assertEqual(list(questions.values())[0], 'Another Question?')
def api_questions(): """ Get questions asked to applicants. Returns: response (dict): A dictionary whose keys are integer question ids and values are question text. """ return jsonify(get_questions(current_user=current_user))
def test_cannot_set_answers_for_submitted_application(self): self.application.is_submitted = True db.session.commit() for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() result = get_questions(current_user=self.applicant) question_keys = [] answers = [] for k in result: question_keys.append(k) answers.append({'question_id': k, 'text': f'answer for {k}'}) with self.assertRaises(ForbiddenException): set_answers(self.applicant.id, answers=answers, current_user=self.applicant)
def test_set_answers_applicant_with_application_applicant(self): self.application.is_submitted = False db.session.commit() for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() result = get_questions(current_user=self.applicant) question_keys = [] answers = [] for k in result: question_keys.append(k) answers.append({'question_id': k, 'text': f'answer for {k}'}) set_answers(self.applicant.id, answers=answers, current_user=self.applicant) db.session.commit() after_set_result = get_answers(self.applicant.id, current_user=self.applicant) self.assertIsInstance(after_set_result, dict) self.assertEqual(len(after_set_result['questions']), 3) for k in question_keys: self.assertEqual(after_set_result['questions'][k]['answer'], f'answer for {k}')
def test_no_questions(self): result = get_questions() self.assertIsInstance(result, dict) self.assertEqual(len(result), 0, result)