305 lines
10 KiB
Python
305 lines
10 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo.tests import common, tagged
|
|
|
|
|
|
@tagged('post_install', '-at_install', 'tour_genius')
|
|
class TestQuizModels(common.TransactionCase):
|
|
"""Test cases for Quiz Layer models"""
|
|
|
|
def setUp(self):
|
|
super(TestQuizModels, self).setUp()
|
|
self.Quiz = self.env['genius.quiz']
|
|
self.Question = self.env['genius.quiz.question']
|
|
self.Answer = self.env['genius.quiz.answer']
|
|
self.Attempt = self.env['genius.quiz.attempt']
|
|
self.Response = self.env['genius.quiz.response']
|
|
|
|
# Create test user
|
|
self.test_user = self.env['res.users'].create({
|
|
'name': 'Test Quiz User',
|
|
'login': 'test_quiz_user',
|
|
'email': 'quizuser@test.com',
|
|
})
|
|
|
|
# =========================================================================
|
|
# Quiz Tests
|
|
# =========================================================================
|
|
|
|
def test_quiz_creation(self):
|
|
"""Test creating a quiz"""
|
|
quiz = self.Quiz.create({
|
|
'name': 'Sales Quiz',
|
|
'passing_score': 70.0,
|
|
})
|
|
|
|
self.assertTrue(quiz.id)
|
|
self.assertEqual(quiz.passing_score, 70.0)
|
|
self.assertEqual(quiz.question_count, 0)
|
|
|
|
def test_quiz_question_count(self):
|
|
"""Test question count computation"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
|
|
self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>What is Odoo?</p>',
|
|
'question_type': 'single',
|
|
})
|
|
self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>How to create a record?</p>',
|
|
'question_type': 'single',
|
|
})
|
|
|
|
quiz.invalidate_cache()
|
|
self.assertEqual(quiz.question_count, 2)
|
|
|
|
# =========================================================================
|
|
# Question Tests
|
|
# =========================================================================
|
|
|
|
def test_question_creation_single_choice(self):
|
|
"""Test creating a single choice question"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>What is 2+2?</p>',
|
|
'question_type': 'single',
|
|
'points': 5,
|
|
})
|
|
|
|
self.assertTrue(question.id)
|
|
self.assertEqual(question.question_type, 'single')
|
|
self.assertEqual(question.points, 5)
|
|
|
|
def test_question_with_answers(self):
|
|
"""Test question with answer options"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>What is Odoo?</p>',
|
|
'question_type': 'single',
|
|
})
|
|
|
|
# Add answers
|
|
correct = self.Answer.create({
|
|
'question_id': question.id,
|
|
'answer_text': 'An ERP system',
|
|
'is_correct': True,
|
|
})
|
|
wrong = self.Answer.create({
|
|
'question_id': question.id,
|
|
'answer_text': 'A game',
|
|
'is_correct': False,
|
|
})
|
|
|
|
correct_answers = question.get_correct_answers()
|
|
self.assertEqual(len(correct_answers), 1)
|
|
self.assertEqual(correct_answers[0], correct)
|
|
|
|
def test_question_short_answer(self):
|
|
"""Test short answer question type"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>What is the capital of France?</p>',
|
|
'question_type': 'short_answer',
|
|
'correct_short_answer': 'Paris',
|
|
})
|
|
|
|
correct_answers = question.get_correct_answers()
|
|
self.assertEqual(correct_answers, ['Paris'])
|
|
|
|
# =========================================================================
|
|
# Attempt Tests
|
|
# =========================================================================
|
|
|
|
def test_attempt_creation(self):
|
|
"""Test creating a quiz attempt"""
|
|
quiz = self.Quiz.create({
|
|
'name': 'Test Quiz',
|
|
'passing_score': 70.0,
|
|
})
|
|
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
self.assertTrue(attempt.id)
|
|
self.assertEqual(attempt.state, 'in_progress')
|
|
|
|
def test_attempt_scoring(self):
|
|
"""Test attempt scoring calculation"""
|
|
quiz = self.Quiz.create({
|
|
'name': 'Test Quiz',
|
|
'passing_score': 50.0,
|
|
})
|
|
|
|
question1 = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>Question 1</p>',
|
|
'question_type': 'single',
|
|
'points': 10,
|
|
})
|
|
correct1 = self.Answer.create({
|
|
'question_id': question1.id,
|
|
'answer_text': 'Correct',
|
|
'is_correct': True,
|
|
})
|
|
|
|
question2 = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>Question 2</p>',
|
|
'question_type': 'single',
|
|
'points': 10,
|
|
})
|
|
correct2 = self.Answer.create({
|
|
'question_id': question2.id,
|
|
'answer_text': 'Correct',
|
|
'is_correct': True,
|
|
})
|
|
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
# Answer first question correctly
|
|
response1 = self.Response.create({
|
|
'attempt_id': attempt.id,
|
|
'question_id': question1.id,
|
|
'selected_answer_ids': [(6, 0, [correct1.id])],
|
|
})
|
|
response1._score_response()
|
|
self.assertTrue(response1.is_correct)
|
|
|
|
# Answer second question incorrectly (no answer selected)
|
|
response2 = self.Response.create({
|
|
'attempt_id': attempt.id,
|
|
'question_id': question2.id,
|
|
'selected_answer_ids': [(6, 0, [])],
|
|
})
|
|
response2._score_response()
|
|
self.assertFalse(response2.is_correct)
|
|
|
|
# Check score
|
|
attempt.invalidate_cache()
|
|
self.assertEqual(attempt.points_earned, 10)
|
|
self.assertEqual(attempt.points_possible, 20)
|
|
self.assertEqual(attempt.score, 50.0)
|
|
self.assertTrue(attempt.is_passed)
|
|
|
|
def test_attempt_submit(self):
|
|
"""Test submitting an attempt"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
self.assertEqual(attempt.state, 'in_progress')
|
|
|
|
attempt.action_submit()
|
|
|
|
self.assertEqual(attempt.state, 'submitted')
|
|
self.assertTrue(attempt.submitted_at)
|
|
|
|
# =========================================================================
|
|
# Response Tests
|
|
# =========================================================================
|
|
|
|
def test_response_scoring_single_choice(self):
|
|
"""Test response scoring for single choice"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>Test?</p>',
|
|
'question_type': 'single',
|
|
'points': 5,
|
|
})
|
|
correct = self.Answer.create({
|
|
'question_id': question.id,
|
|
'answer_text': 'Correct',
|
|
'is_correct': True,
|
|
})
|
|
wrong = self.Answer.create({
|
|
'question_id': question.id,
|
|
'answer_text': 'Wrong',
|
|
'is_correct': False,
|
|
})
|
|
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
# Correct response
|
|
response = self.Response.create({
|
|
'attempt_id': attempt.id,
|
|
'question_id': question.id,
|
|
'selected_answer_ids': [(6, 0, [correct.id])],
|
|
})
|
|
response._score_response()
|
|
|
|
self.assertTrue(response.is_correct)
|
|
self.assertEqual(response.points_awarded, 5)
|
|
|
|
def test_response_scoring_short_answer(self):
|
|
"""Test response scoring for short answer"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>Capital of France?</p>',
|
|
'question_type': 'short_answer',
|
|
'correct_short_answer': 'Paris',
|
|
'case_sensitive': False,
|
|
'points': 10,
|
|
})
|
|
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
# Correct response (different case)
|
|
response = self.Response.create({
|
|
'attempt_id': attempt.id,
|
|
'question_id': question.id,
|
|
'text_answer': 'paris',
|
|
})
|
|
response._score_response()
|
|
|
|
self.assertTrue(response.is_correct)
|
|
self.assertEqual(response.points_awarded, 10)
|
|
|
|
def test_response_scoring_short_answer_case_sensitive(self):
|
|
"""Test case-sensitive short answer"""
|
|
quiz = self.Quiz.create({'name': 'Test Quiz'})
|
|
question = self.Question.create({
|
|
'quiz_id': quiz.id,
|
|
'question_text': '<p>Password?</p>',
|
|
'question_type': 'short_answer',
|
|
'correct_short_answer': 'Secret123',
|
|
'case_sensitive': True,
|
|
'points': 10,
|
|
})
|
|
|
|
attempt = self.Attempt.create({
|
|
'quiz_id': quiz.id,
|
|
'user_id': self.test_user.id,
|
|
})
|
|
|
|
# Wrong case
|
|
response = self.Response.create({
|
|
'attempt_id': attempt.id,
|
|
'question_id': question.id,
|
|
'text_answer': 'secret123',
|
|
})
|
|
response._score_response()
|
|
|
|
self.assertFalse(response.is_correct)
|