def test_update_question(self): response = set_questions([{ 'text': 'Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) question = db.session.query(Question).one() response = set_questions([{ 'question_id': question.id, 'text': 'A Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) questions = db.session.query(Question) self.assertEqual(questions.count(), 1) question = questions.one() self.assertEqual(question.text, 'A Question?')
def test_add_two_questions_in_series(self): response = set_questions([{ 'text': 'Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) self.assertEqual(db.session.query(Question).count(), 1) self.assertEqual( db.session.query(Question).filter_by(text='Question?').count(), 1) response = set_questions([{ 'text': 'Another Question?' }], current_user=self.admin) self.assertEqual(response, {'status': 'ok'}) questions = db.session.query(Question) self.assertEqual(questions.count(), 2) self.assertEqual( db.session.query(Question).filter_by(text='Question?').count(), 1) self.assertEqual( db.session.query(Question).filter_by( text='Another Question?').count(), 1)
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_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_set_questions(): """ Set/update questions asked to applicants. Args: questions (list) List of dict with keys 'question_id' and 'text'. question_id is None, if the question is new. Returns: {'status': 'ok'} Error codes: Forbidden (403): If logged in user is not an admin. Bad Request (400): If questions are not given, or if question_id is invalid for any questions (doesn't already exist). """ questions = request.get_json().get('questions', None) if questions is None: raise BadRequestException('questions were not given.') else: return jsonify(set_questions(questions, current_user=current_user))
def test_add_question_forbidden(self): for user in (self.recruiter, self.senior_recruiter, self.applicant, self.not_applicant): with self.assertRaises(ForbiddenException): set_questions([{'text': 'Question?'}], current_user=user)