def test_quiz_checkAnswer_correct(self): quest = 'q?' ans = [] questions = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) questions.append(Question(quest, ans)) questions.append(Question(quest, ans)) questions.append(Question(quest, ans)) quiz = Quiz(0, questions) res = quiz.checkAnswer('a') self.assertEqual(res, 1) res = quiz.checkAnswer('a') self.assertEqual(res, 2)
def test_quiz_islost(self): quest = 'q?' ans = [] questions = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) questions.append(Question(quest, ans)) questions.append(Question(quest, ans)) quiz = Quiz(0, questions) quiz.checkAnswer('a') self.assertEqual(quiz.currentQuestion, 1) try: quiz.checkAnswer('b') except LostQuizError: self.assertEqual(quiz.currentQuestion, -1)
def test_quiz_iscompleted(self): quest = 'q?' ans = [] questions = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) questions.append(Question(quest, ans)) questions.append(Question(quest, ans)) quiz = Quiz(0, questions) quiz.checkAnswer('a') self.assertFalse(quiz.isCompleted()) try: quiz.checkAnswer('a') except CompletedQuizError: self.assertTrue(quiz.isCompleted())
def answer_question(id, answer): global _LOADED_QUIZZES result = "" id = str(id) exists_quiz(id) quiz = _LOADED_QUIZZES[id] try: Quiz.isOpen(quiz) if 'PUT' == request.method: try: result = Quiz.checkAnswer(quiz, answer) except CompletedQuizError: result = "you won 1 million clams!" except NonExistingAnswerError: result = "non-existing answer!" except CompletedQuizError: result = "completed quiz" except LostQuizError: result = "you lost!" return jsonify({"msg": result})