Exemplo n.º 1
0
    def test_search_question(self):
        question = Question(
            question='dummy_question_search',
            answer='answer',
            category='1',
            difficulty='4'
        )

        question.insert()

        search_term = 'dummy_Question'
        body = {
            'searchTerm': search_term
        }
        res = self.client().post(
            f'/questions/search',
            data=json.dumps(body)
        )

        status_code = res.status_code
        data = json.loads(res.data)
        success = data['success']
        questions = data['questions']
        total_questions = data['total_questions']

        question.delete()

        self.assertEqual(status_code, 200)
        self.assertEqual(len(questions), 1)
        self.assertEqual(total_questions, 1)
        self.assertTrue(success)
Exemplo n.º 2
0
    def test_get_questions_by_category(self):
        category = Category(
            type='dummy_test_category',
        )
        category.insert()

        question = Question(
            question='dummy_test_question',
            answer='answer',
            category=str(category.id),
            difficulty='4'
        )
        question.insert()

        res = self.client().get(
            f'/categories/{category.id}/questions'
        )
        status_code = res.status_code
        data = json.loads(res.data)
        questions = data['questions']
        total_questions = data['total_questions']

        question.delete()
        category.delete()

        self.assertEqual(status_code, 200)
        self.assertEqual(len(questions), 1)
        self.assertEqual(total_questions, 1)
Exemplo n.º 3
0
    def test_get_quizzes_only_returns_question_not_in_previous_question(self):

        previous_questions = [question.id for question in Question.query.all()]

        question1 = Question(
            question="Is python a high level programming language",
            answer="answer2",
            difficulty=1,
            category=1,
        )
        question1.insert()

        response = self.client.post(
            "/quizzes",
            data=json.dumps({
                "previous_questions": previous_questions,
                "quiz_category": {
                    "id": "1"
                },
            }),
            headers=self.headers,
        )
        data = json.loads(response.data.decode())
        question = data["question"]

        self.assertEqual(response.status_code, 200)
        self.assertTrue(data["success"])
        self.assertEqual(question["id"], question1.id)
        self.assertEqual(question["answer"], question1.answer)
        self.assertEqual(question["question"], question1.question)
        self.assertEqual(question["difficulty"], question1.difficulty)
        self.assertEqual(question["category"], question1.category)
        self.assertEqual(question["category"], 1)

        question1.delete()
Exemplo n.º 4
0
    def test_search_question(self):
        question1 = Question(
            question="True or False: Django is a high-level Python Web"
            " framework that encourages rapid development"
            " and clean, pragmatic design",
            answer="True",
            difficulty=1,
            category=1,
        )
        question2 = Question(
            question="True or False: django is a micro web framework "
            "written in Python",
            answer="False",
            difficulty=1,
            category=2,
        )

        question1.insert()
        question2.insert()

        response = self.client.post(
            "/questions/search",
            data=json.dumps({"searchTerm": "django"}),
            headers=self.headers,
        )

        data = json.loads(response.data.decode())
        self.assertEqual(response.status_code, 200)
        self.assertTrue(data["success"])
        self.assertTrue(isinstance(data["questions"], list))
        self.assertEqual(data["total_questions"], 2)

        question1.delete()
        question2.delete()
Exemplo n.º 5
0
 def delete_question(question_id):
     question_data = Question.query.get(question_id)
     if question_data:
         Question.delete(question_data)
         result = {"success": True}
         return jsonify(result)
     abort(404)
Exemplo n.º 6
0
 def delete_question(question_id):
     question = Question.query.filter_by(id=question_id).first()
     try:
         Question.delete(question)
         return jsonify({"success": True, "delete_question": "yes"})
     except:
         abort(422)
Exemplo n.º 7
0
    def delete_question(id):
        question = Question.query.filter(Question.id == id).one_or_none()

        if question:
            Question.delete(question)
            return jsonify({"message": "Question Deleted"}), 204
        else:
            abort(404)
Exemplo n.º 8
0
    def delete_question(question_id):
        question_query = Question.query.get(question_id)

        if question_query:
            Question.delete(question_query)
            return jsonify({'success': True})
        else:
            abort(404)
Exemplo n.º 9
0
 def delete_question(question_id):
     try:
         question = Question.query.get_or_404(question_id)
         Question.delete(question)
     except DatabaseError:
         db.session.rollback()
     finally:
         db.session.close()
         return jsonify({'success': True})
Exemplo n.º 10
0
 def delete_questions(id):
     question_data = Question.query.filter(Question.id == id).one_or_none()
     if question_data:
         Question.delete(question_data)
         return jsonify({
             "success": True,
         })
     else:
         abort(404)
Exemplo n.º 11
0
 def delete_question(question_id):
     question_item = Question.query.get(question_id)
     if question_item:
         Question.delete(question_item)
         result = {
             "success": True,
         }
         return jsonify(result)
     abort(404)
 def delete_question(question_id):
     try:
         deleted_item = Question.query.get(question_id)
         Question.delete(deleted_item)
         if (deleted_item is None):
             abort(404)
         all_questions = Question.query.all()
         return jsonify({'success': True, 'deleted': question_id})
     except:
         abort(422)
 def test_search_question(self):
     question = Question(question='@@@', answer='a', category=1, difficulty=1)
     question.insert()
     res = self.client().post('/questions/search',json={'searchTerm':'@@'})
     data = json.loads(res.data)
     #print("DATA",data)
     self.assertEqual(res.status_code, 200)
     self.assertEqual(data['success'], True)
     self.assertTrue(data['questions'])
     self.assertTrue(data['current_category'])
     question.delete()
Exemplo n.º 14
0
 def delete_questions(question_id):
   try:
     question = Question.query.get(question_id)
     Question.delete(question)
   except Exception:
     db.session.rollback()
   finally:
     db.session.close()
   return jsonify({
     "sucess": True
   })
    def test_422_unprocessable_delete_question(self):
        question = Question('question', 'answer', 1, 3)
        question.insert()
        question.delete()

        res = self.client().delete(f'/questions/{question.id}')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['message'], 'unprocessable')
        self.assertFalse(data['success'])
Exemplo n.º 16
0
 def delete_question(question_id):
     question_query = Question.query.get(question_id)
     if question_query:
         Question.delete(question_query)
         result = {
             "success": True,
             "deleted_id": question_id,
             "total_questions": len(Question.query.all())
         }
         return jsonify(result)
     abort(404)
Exemplo n.º 17
0
    def delete_question(questionId):

        question = Question.query.filter(
            Question.id == questionId).one_or_none()

        if question is None:
            abort(422)

        Question.delete(question)

        return jsonify({'success': True})
Exemplo n.º 18
0
    def delete_question(question_id):
        try:
            delete_question = Question.query.get(question_id)
            Question.delete(delete_question)
            if delete_question == None:

                abort(404)

            return jsonify({'success': True, 'deleted': question_id})
        except:
            abort(422)
Exemplo n.º 19
0
def question_delete():
    _id = request.form.get('id')
    Question.delete(g._db, _id)
    # 更新robotd的问题库
    try:
        rpc.refresh_questions()
    except xmlrpclib.ProtocolError as err:
        logging.warning("refresh questions err:%s", err)
    except Exception as err:
        logging.warning("refresh questions err:%s", err)

    return ''
Exemplo n.º 20
0
    def delete_question(question_id):
        '''
        Create an endpoint to DELETE question using a question ID.
        '''
        question = Question.query.filter(
            Question.id == question_id).one_or_none()

        if not question:
            abort(404)

        Question.delete(question)
        return jsonify({'deleted': question.id, 'success': True})
Exemplo n.º 21
0
def question_delete():
    _id = request.form.get('id')
    Question.delete(g._db, _id)
    # 更新robotd的问题库
    try:
        rpc.refresh_questions()
    except xmlrpclib.ProtocolError as err:
        logging.warning("refresh questions err:%s", err)
    except Exception as err:
        logging.warning("refresh questions err:%s", err)

    return ''
Exemplo n.º 22
0
 def test_delete_question(self):
     question_id = 1
     q = Question(question="Which is the tallest mountain in the world?",
                  answer="Mount Everest",
                  category=1,
                  difficulty=1,
                  rating=1)
     q.insert()
     res = self.client().delete("/questions/{}".format(question_id))
     data = json.loads(res.data)
     self.assertEqual(data['success'], True)
     self.assertEqual(data['question_id'], question_id)
     q.delete()
Exemplo n.º 23
0
    def test_404_if_search_results_are_empty(self):
        new_question = Question(question='whose shisha is this?',
                                answer='smart ans',
                                category=self.sample_question['category'],
                                difficulty=self.sample_question['difficulty'])
        new_question.insert()

        response = self.client().get('/questions/tootie')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 404)
        self.assertFalse(data['success'])
        new_question.delete()
Exemplo n.º 24
0
 def delete_question(question_id):
     question_object = Question.query.get(question_id)
     if question_object:
         Question.delete(question_object)
         res = Question.format(question_object)
         data = {
             "id": res['id'],
             "question": res['question'],
             "difficulty": res['difficulty'],
             "category_id": res['category_id']
         }
         return format_result(data)
     abort(STATUS_NOT_FOUND)
Exemplo n.º 25
0
 def test_get_questions_one_record(self):
     category = Category("Science")
     category.insert()
     question = Question('Q1', 'A1', "1", category.id)
     question.category = category
     question.insert()
     response = self.client().get('/api/questions')
     data = response.get_json()
     print(data)
     # self.assertEqual(data['questions'], [])
     self.assertEqual(data['total_questions'], 1)
     self.assertEqual(response.status_code, 200)
     question.delete()
     category.delete()
Exemplo n.º 26
0
    def test_search_questions(self):
        new_question = Question(question='whose laptop is this?',
                                answer='smart ans',
                                category=self.sample_question['category'],
                                difficulty=self.sample_question['difficulty'])
        new_question.insert()

        response = self.client().get('/questions/apto')
        data = json.loads(response.data)

        self.assertEqual(response.status_code, 200)
        self.assertTrue(data['success'])
        self.assertEqual(data['questions'][0]['answer'], 'smart ans')
        self.assertEqual(len(data['questions']), 1)
        new_question.delete()
Exemplo n.º 27
0
    def test_get_questions_paginated_fail(self):
        question = Question(question=self.new_question_1['question'],
                            answer=self.new_question_1['answer'],
                            difficulty=self.new_question_1['difficulty'],
                            category=self.new_question_1['category'])
        question.insert()

        res = self.client().get('/questions?page=1000')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'resource not found')

        question.delete()
Exemplo n.º 28
0
    def test_get_all_questions_fail(self):
        question = Question(question=self.new_question_1['question'],
                            answer=self.new_question_1['answer'],
                            difficulty=self.new_question_1['difficulty'],
                            category=self.new_question_1['category'])
        question.insert()

        res = self.client().get('/questions', json={'type': 3})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 400)
        self.assertEqual(data['success'], False)
        self.assertEqual(data['message'], 'bad request')

        question.delete()
Exemplo n.º 29
0
    def delete_question(question_id):
        deleted_question = Question.query.filter(
            Question.id == question_id).one_or_none()

        if deleted_question:
            Question.delete(deleted_question)
            questions = Question.query.all()
            current_questions = paginate_questions(request, questions)

            return jsonify({
                'success': True,
                'questions': current_questions,
                'deleted_question_id': question_id
            })
        abort(404)
Exemplo n.º 30
0
    def test_search_questions_non_matching_word(self):
        category = Category("Science")
        category.insert()
        question = Question('Who moved my cheese', 'Not Me!', "1", category.id)
        question.category = category
        question.insert()

        response = self.client().post('/api/questions/search',
                                      json={'searchTerm': 'happy'})
        data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(data['total_questions'], 0)

        question.delete()
        category.delete()
Exemplo n.º 31
0
  def delete_question(question_id):
    # question = Question.query.get(id=question_id).one_or_none()

    # if(question is None):
    #     abort(404)

    try:
        question = Question.query.get(question_id)
        Question.delete(question)        
        return jsonify({
              'success':True,
        })
    except Exception as e:
        print(e)
        abort(422)