Files
odoo_source/addons/survey/tests/test_survey_randomize.py
T
Aurélien Warnon 445541989f [IMP] survey: add randomization and survey layout selection
Task #1902306

Purpose
=======

The "page" concept is now only present in the user interface.
Surveys are split in "sections" that have two purposes:
- Organizing a (long) survey/certification
- Allow to pick a random questions count for that section (more information here after)

On the user interface, based on the questions_layout field, surveys are displayed:
- On a single page, where sections still appear but are only used as visual separations
- On multiple pages, having one page per section (this matches the previous behavior)
- On multiple pages, having one page per *question*

On top of that page concept, this commit added randomization for the survey questions.
The randomization mechanism will take X questions per section of the survey.
The survey.user_input is initialized with the selected questions to be sure to keep that
set of questions and avoid showing a new set of questions every time the user refreshes the page.
This also allows to easilly go back and know which question we have to show.
2019-02-06 10:34:43 +00:00

60 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests.common import TransactionCase
class TestSurveyRandomize(TransactionCase):
def test_01_generate_randomized_questions(self):
""" Use random generate for a survey and verify that questions within the page are selected accordingly """
Question = self.env['survey.question'].sudo()
question_and_pages = self.env['survey.question']
page_1 = Question.create({
'title': 'Page 1',
'is_page': True,
'sequence': 1,
'random_questions_count': 3
})
question_and_pages |= page_1
question_and_pages = self._add_questions(question_and_pages, page_1, 5)
page_2 = Question.create({
'title': 'Page 2',
'is_page': True,
'sequence': 100,
'random_questions_count': 5
})
question_and_pages |= page_2
question_and_pages = self._add_questions(question_and_pages, page_2, 10)
page_3 = Question.create({
'title': 'Page 2',
'is_page': True,
'sequence': 1000,
'random_questions_count': 4
})
question_and_pages |= page_3
question_and_pages = self._add_questions(question_and_pages, page_3, 2)
self.survey1 = self.env['survey.survey'].sudo().create({
'title': "S0",
'question_and_page_ids': [(6, 0, question_and_pages.ids)],
'questions_selection': 'random'
})
generated_questions = self.survey1._prepare_answer_questions()
self.assertEqual(len(generated_questions.ids), 10, msg="Expected 10 unique questions")
self.assertEqual(len(generated_questions.filtered(lambda question: question.page_id == page_1)), 3, msg="Expected 3 questions in page 1")
self.assertEqual(len(generated_questions.filtered(lambda question: question.page_id == page_2)), 5, msg="Expected 5 questions in page 2")
self.assertEqual(len(generated_questions.filtered(lambda question: question.page_id == page_3)), 2, msg="Expected 2 questions in page 3")
def _add_questions(self, question_and_pages, page, count):
for i in range(count):
question_and_pages |= self.env['survey.question'].sudo().create({
'title': page.title + ' Q' + str(i + 1),
'sequence': page.sequence + (i + 1)
})
return question_and_pages