예제 #1
0
파일: api.py 프로젝트: budershank/triv
def add_question():
    args = request.args
    for required_param in ["question", "correct", "wrong1", "wrong2", "wrong3"]:
        if not args.get(required_param, None):
            return jsonify(ok=False, error="missing parameter `%s`" % (required_param,))

    inserted_doc = db.insert_question(args["question"], args["correct"],
                                      args["wrong1"], args["wrong2"], args["wrong3"])
    question_mod.add_question(inserted_doc)
    return jsonify(ok=True, question_oid=str(inserted_doc["_id"]))
예제 #2
0
파일: test_api.py 프로젝트: budershank/triv
    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)