def add_question(): # add a new question into database body = request.get_json() new_question = body.get('question', None) new_answer = body.get('answer', None) new_category = body.get('category', None) new_difficulty = body.get('difficulty', None) # old_question = Question.query.filter_by(question=new_question) \ # .filter_by(category=new_category).one_or_none() # if old_question is not None: # abort(400, 'question already exists') if not new_question or not new_answer or not new_category or not new_difficulty: abort(400, 'invalid inputs of the new question') try: question = Question(question=new_question, answer=new_answer, category=new_category, difficulty=new_difficulty) question.insert() new_id = question.get_last_id() return jsonify({'success': True, 'new_id': new_id}) except: flash('An error occur when adding new question') abort(500, 'failed to add new question') print(sys.exc_info())
def test_delete_question(self): new_question = Question(question='question to delete', answer='ans', category=2, difficulty=1) new_question.insert() new_id = new_question.get_last_id() res = self.client().delete('/questions/{}'.format(new_id)) data = json.loads(res.data) question = Question.query.filter_by(id=new_id).one_or_none() self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True) self.assertEqual(data['deleted'], new_id) self.assertIsNone(question)