Files
odoo_source/addons/survey/tests/test_survey_randomize.py
T
Umesh Gupta 48737a8ffc [FIX] survey*: avoid traceback for data/demo data surveys
Currently, in demo data (or data) of some surveys, there are records of
'survey.question' having 'is_page' field value as true and 'question_type'
is not defined. When question type is not defined, default value
'simple_choice' is assigned to the 'question_type' field. Now when we
create a live session of that survey and go to the page, it tries to read
the data for bar chart but data is not available (undefined) and the
traceback is thrown. This happens  because question types 'simple_choice'
and 'multiple_choice') are expected to load a bar chart.

It used to work before commit[1] where default 'question_type' value was
being set to 'text_box' (so attempt to load bar chart was not made).

This commit improves the behavior by adapting data/demo data/test cases
and by adding a python constraint that prevents question creation of
page type if there is a question type set, and compute `question_type`
based on `is_page` field. Now that we have python constraint and a
compute method, we no longer need the `default_get` method, so it is
also removed.

commit[1] - https://github.com/odoo/odoo/commit/1fb99d79b1567d4c150dc07f309ce54a3beea8e3

taskID-2841582

closes odoo/odoo#93414

Related: odoo/enterprise#28305
Related: odoo/upgrade#3600
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2022-07-06 17:09:59 +02:00

63 lines
2.6 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,
'question_type': False,
'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,
'question_type': False,
'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,
'question_type': False,
'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