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 test_get_questions_with_page(self): """ Test that a GET request to the /questions endpoint returns the correct response when there are questions in the database and a page in url parameters. """ with self.client as c: result = json.loads( c.get( "/questions?page=2", headers={ "Content-Type": "application/json" }, ).data) self.assertTrue(result["success"]) self.assertEqual( result["categories"], { str(self.c_1.format()["id"]): self.c_1.format()["type"], str(self.c_2.format()["id"]): self.c_2.format()["type"], }, ) self.assertEqual( result["questions"], [question.format() for question in Question.get_all()[10:20]], ) self.assertEqual(result["total_questions"], len(Question.get_all()))
def __init__(self, request): # lele add dummy questions le if len(Question.get_all()) == 0: print "I MAEK DUMMY SHIZZLE" userID = g.lti.get_user_id() q1 = Question("1","1","What am I?",True,1000) q2 = Question("2","1","Who am I?",True,1000) q3 = Question("3","1","Where am I?",True,1000) session.add(q1) session.add(q2) session.add(q3) a11 = AnswerModel("einseins",1,userID,0,1000.0) a12 = AnswerModel("einszwei",1,1338, 0,1000.0) a13 = AnswerModel("einsdrei",1,1339, 0,1000.0) a21 = AnswerModel("zweieins",2,userID,0,1000.0) a22 = AnswerModel("zweizwei",2,1338, 0,1000.0) a23 = AnswerModel("zweidrei",2,1339, 0,1000.0) a31 = AnswerModel("dreieins",3,userID,0,1000.0) a32 = AnswerModel("dreizwei",3,1338, 0,1000.0) a33 = AnswerModel("dreidrei",3,1339, 0,1000.0) session.add(a11) session.add(a12) session.add(a13) session.add(a21) session.add(a22) session.add(a23) session.add(a31) session.add(a32) session.add(a33) session.commit()
def get_questions(): questions = Question.get_by_page(request, QUESTIONS_PER_PAGE) if len(questions) == 0: abort(404) return jsonify({ "questions": questions, "total_questions": len(Question.get_all()), "success": True, "categories": Category.to_dict(), })
def test_delete_question(self): """ Test that a DELETE request to the /questions/<int:question_id> endpoint returns the correct response when the question is in the database. """ with self.client as c: question_id = self.q_12.format()["id"] result = json.loads( c.delete( f"/questions/{question_id}", headers={ "Content-Type": "application/json" }, ).data) self.assertTrue(result["success"]) self.assertEqual(result["deleted"], question_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 create_question(): data = request.get_json() question = Question(**data) result = question.insert_record() if result["error"]: abort(500) _id = result["id"] questions = Question.get_by_page(request, QUESTIONS_PER_PAGE) return jsonify({ "created": _id, "questions": questions, "total_questions": len(Question.get_all()), "success": True, })
def delete_question(question_id): question = Question.get_by_id(question_id) if question is None: abort(404) result = question.delete_record() if result["error"]: abort(500) questions = Question.get_by_page(request, QUESTIONS_PER_PAGE) return jsonify({ "deleted": question_id, "questions": questions, "total_questions": len(Question.get_all()), "success": True, })
def test_get_all(self): """Test the get_all method for the Question model.""" with self.app_context: questions = Question.get_all() self.assertIsNotNone(questions) self.assertEqual(len(questions), 3)