def test_get_questions_by_category_valid(self): """ Test that a GET request to the /categories/<int:category_id>/questions endpoint returns the correct response when there are questions in the database for the chosen category. """ with self.client as c: category_id = self.c_1.format()["id"] result = json.loads( c.get( f"/categories/{category_id}/questions", headers={ "Content-Type": "application/json" }, ).data) self.assertTrue(result["success"]) self.assertEqual( result["current_category"], Category.get_by_id(category_id).type, ) self.assertEqual( result["questions"], [ question.format() for question in Question.get_by_category(category_id)[0:10] ], ) self.assertEqual( result["total_questions"], len(Question.get_by_category(category_id)), )
def test_get_by_category(self): """Test the get_by_category method for the Question model.""" with self.app_context: questions = Question.get_by_category(self.c_2.id) self.assertIsNotNone(questions) self.assertEqual(len(questions), 2) self.assertIn( questions[0].format(), [self.q_2.format(), self.q_3.format()] ) self.assertIn( questions[1].format(), [self.q_2.format(), self.q_3.format()] )
def questions_by_category(category_id): questions = Question.get_by_category_by_page(category_id, request, QUESTIONS_PER_PAGE) if len(questions) == 0: abort(404) return jsonify({ "questions": questions, "total_questions": len(Question.get_by_category(category_id)), "current_category": Category.get_by_id(category_id).type, "success": True, })