def guess_answer(): question_oid_str = request.args.get("qid", None) question_oid, error_str = get_oid(question_oid_str) if not question_oid: return jsonify(ok=False, error=error_str) user_oid_str = request.args.get("uid", None) user_oid, error_str = get_oid(user_oid_str) if not user_oid: return jsonify(ok=False, error=error_str) answer = request.args.get("ans", None) if not answer: return jsonify(ok=False, error="No answer given") question = question_mod.get_question(question_oid) if not question: return jsonify(ok=False, error="Question does not exist") user = user_mod.User.from_oid(user_oid) if not user: return jsonify(ok=False, error="User does not exist") if user.answered_question(question.oid): return jsonify(ok=False, error="User already answered that question") if question.right_answer(answer): user.was_correct(question.oid) question.answered_right(user._demographics) return jsonify(ok=True, is_correct=True, demographics=question.demographics) else: user.was_wrong(question.oid) question.answered_wrong(user._demographics) return jsonify(ok=True, is_correct=False, demographics=question.demographics)
def test_play(self): self.dan_user.add_demographic('favorite food', 'mexican') question_doc = db.insert_question("question text", "correct", "wrong1", "wrong2", "wrong3") response = self.client.get("/get_question?uid=%s" % (str(self.dan_user._oid),)) self.assertTrue(response.json["ok"]) self.assertEquals(str(question_doc["_id"]), response.json["question_oid"]) wrong_answer_response = self.client.get("/guess?qid=%s&uid=%s&ans=wrong2" % (str(question_doc["_id"]), str(self.dan_user._oid))) self.assertTrue(wrong_answer_response.json["ok"]) self.assertFalse(wrong_answer_response.json["is_correct"]) self.assertEquals({"favorite food": {"mexican": {"right": 0, "wrong": 1}}}, wrong_answer_response.json["demographics"]) no_more_questions = self.client.get("/get_question?uid=%s" % (str(self.dan_user._oid),)) self.assertFalse(no_more_questions.json["ok"]) self.assertEquals("User answered too many questions?", no_more_questions.json["error"]) new_question_doc = db.insert_question("question #2", "right", "bad1", "bad2", "bad3") question_mod.add_question(new_question_doc) new_question_response = self.client.get("/get_question?uid=%s" % (str(self.dan_user._oid),)) right_answer_response = self.client.get("/guess?qid=%s&uid=%s&ans=right" % (str(new_question_doc["_id"]), str(self.dan_user._oid))) self.assertTrue(right_answer_response.json["ok"]) self.assertTrue(right_answer_response.json["is_correct"]) self.assertEquals({"favorite food": {"mexican": {"right": 1, "wrong": 0}}}, right_answer_response.json["demographics"]) new_dan = user_mod.User.from_username('dan') self.assertEquals(set([question_doc["_id"], new_question_doc["_id"]]), new_dan._answered_questions) self.assertEquals(1, new_dan._num_correct) self.assertEquals(1, new_dan._num_wrong) new_question = question_mod.get_question(new_question_doc["_id"]) self.assertEquals(question_mod.Question, type(new_question)) self.assertEquals(1, new_question.num_right)