def test_post_question_valid(self): """ Test that a POST request to the /questions endpoint returns the correct response for a valid question. """ with self.client as c: result = json.loads( c.post( "/questions", headers={ "Content-Type": "application/json" }, data=json.dumps({ "question": "Test Question 13?", "answer": "Test Answer 13", "difficulty": 5, "category_id": self.c_1.format()["id"], }), ).data) self.assertTrue(result["success"]) self.assertEqual( result["created"], Question.search("Test Question 13?")[0]["id"], ) self.assertEqual( result["questions"], [question.format() for question in Question.get_all()[0:10]], ) self.assertEqual(result["total_questions"], len(Question.get_all()))
def search_questions(): search_term = request.get_json()["search_term"] questions = Question.search(search_term) if len(questions) == 0: abort(404) return jsonify({ "questions": questions, "total_questions": len(questions), "success": True, })
def test_search(self): """Test the search method for the Question model.""" with self.app_context: questions = Question.search("Test Question 3?") self.assertIsNotNone(questions) self.assertEqual(questions, [self.q_3.format()])