Files
odoo_source/addons/survey/tests/test_survey_randomize.py
T
Thibault Delavallée 2a35d92b6c [REF] survey: on survey.user_input, rename question_ids field to predefined_question_ids
PURPOSE

As new features are about to land in survey, notably live interactions [1]
and new survey building [2] performing a pre cleaning is necessary. In this
PR we clean survey models by: removing unnecessary fields, cleaning some code
and finally renaming models.

SPECIFICATIONS

``question_ids`` field name is quite difficult to understand as we wonder why
there is such a field on user input model. As its purpose is to store questions
user has to answer (randomly chosen or all chosen) let us rename it to
``predefined_question_ids``.

Moreover starting from now on if it is now given at create it is automatically
computed. Classic flows go through ``survey._create_answer()`` method that
prepares them accordingly. However tests or demo data do not necessarily go
through that method and it makes scoring fail (notably) if people forget
to define them.

Let us automatically generate them at user input create if not given.

LINKS

[0] Related to Task ID 2061901 (survey models cleaning and preparation)
[1] Task ID 1972640 (live interactions)
[2] Task ID 2119587 (new frontend for building surveys)

PR #40765
2019-12-05 15:21:28 +00:00

60 lines
2.5 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_user_input_predefined_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