def test_question_checkAnswer_wrong(self): quest = 'q?' ans = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) question = Question(quest, ans) res = question.checkAnswer('b') self.assertEqual(res, 0)
def test_question_checkAnswer_correct(self): quest = 'q?' ans = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) question = Question(quest, ans) given_answer = 'a' res = question.checkAnswer(given_answer) self.assertEqual(res, 1)
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 test_question_checkAnswer_nonexisting(self): quest = 'q?' ans = [] ans.append(Answer('a', 1)) ans.append(Answer('b', 0)) ans.append(Answer('c', 0)) question = Question(quest, ans) given_answer = 'd' self.assertRaises(NonExistingAnswerError, question.checkAnswer, given_answer)
def test_quiz_checkAnswer_wrong(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)) quiz = Quiz(0, questions) self.assertRaises(LostQuizError, quiz.checkAnswer, 'b')
def test_quiz_checkAnswer_complete(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)) quiz = Quiz(0, questions) self.assertRaises(CompletedQuizError, quiz.checkAnswer, 'a')
def create_quiz(request): global _LOADED_QUIZZES, _QUIZNUMBER json_data = request.get_json() qs = json_data['questions'] questions = [] for q in qs: question = q['question'] answers = [] for a in q['answers']: answers.append(Answer(a['answer'], a['correct'])) question = Question(question, answers) questions.append(question) _LOADED_QUIZZES[str(_QUIZNUMBER)] = Quiz(_QUIZNUMBER, questions) _QUIZNUMBER += 1 return jsonify({'quiznumber': _QUIZNUMBER - 1})
def createDummyQuiz(self): json_data = { "questions": [{ "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }, { "question": "What's the answer to all questions?", "answers": [{ "answer": "33", "correct": 0 }, { "answer": "42", "correct": 1 }, { "answer": "1", "correct": 0 }] }] } qs = json_data['questions'] questions = [] for q in qs: question = q['question'] answers = [] for a in q['answers']: answers.append(Answer(a['answer'], a['correct'])) question = Question(question, answers) questions.append(question) return Quiz(1, questions)
def test_isCompleted(self): answer_1 = Answer("la prima", 1) answer_2 = Answer("la seconda") question = Question("Buona?", [answer_1, answer_2]) quiz = Quiz(1, [question]) self.assertEqual(quiz.isCompleted(), False)
def test_isCompleted(self): answer_1 = Answer("la prima", 1) answer_2 = Answer("la seconda") question = Question("Buona?", [answer_1, answer_2]) self.assertEqual(question.checkAnswer("la prima"), True)