test_survey_flow_with_conditions.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.survey.tests import common
  4. from odoo.tests import tagged
  5. from odoo.tests.common import HttpCase
  6. @tagged('-at_install', 'post_install', 'functional')
  7. class TestSurveyFlowWithConditions(common.TestSurveyCommon, HttpCase):
  8. def test_conditional_flow_with_scoring(self):
  9. with self.with_user('survey_user'):
  10. survey = self.env['survey.survey'].create({
  11. 'title': 'Survey',
  12. 'access_mode': 'public',
  13. 'questions_layout': 'page_per_section',
  14. 'scoring_type': 'scoring_with_answers',
  15. 'scoring_success_min': 85.0,
  16. })
  17. page_0 = self.env['survey.question'].with_user(self.survey_manager).create({
  18. 'title': 'First page',
  19. 'survey_id': survey.id,
  20. 'sequence': 1,
  21. 'is_page': True,
  22. 'question_type': False,
  23. })
  24. q01 = self._add_question(
  25. page_0, 'Question 1', 'simple_choice',
  26. sequence=1,
  27. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  28. labels=[
  29. {'value': 'Answer 1'},
  30. {'value': 'Answer 2'},
  31. {'value': 'Answer 3'},
  32. {'value': 'Answer 4', 'is_correct': True, 'answer_score': 1.0}
  33. ])
  34. q02 = self._add_question(
  35. page_0, 'Question 2', 'simple_choice',
  36. sequence=2,
  37. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  38. is_conditional=True, triggering_question_id=q01.id, triggering_answer_id=q01.suggested_answer_ids.filtered(lambda q: q.is_correct).id,
  39. labels=[
  40. {'value': 'Answer 1'},
  41. {'value': 'Answer 2', 'is_correct': True, 'answer_score': 1.0},
  42. {'value': 'Answer 3'},
  43. {'value': 'Answer 4'}
  44. ])
  45. q03 = self._add_question(
  46. page_0, 'Question 3', 'simple_choice',
  47. sequence=1,
  48. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  49. labels=[
  50. {'value': 'Answer 1'},
  51. {'value': 'Answer 2'},
  52. {'value': 'Answer 3'},
  53. {'value': 'Answer 4', 'is_correct': True, 'answer_score': 1.0}
  54. ])
  55. self._add_question( # q04
  56. page_0, 'Question 4', 'simple_choice',
  57. sequence=2,
  58. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  59. is_conditional=True, triggering_question_id=q03.id, triggering_answer_id=q03.suggested_answer_ids.filtered(lambda q: q.is_correct).id,
  60. labels=[
  61. {'value': 'Answer 1'},
  62. {'value': 'Answer 2', 'is_correct': True, 'answer_score': 1.0},
  63. {'value': 'Answer 3'},
  64. {'value': 'Answer 4'}
  65. ])
  66. q05 = self._add_question(
  67. page_0, 'Question 5', 'simple_choice',
  68. sequence=1,
  69. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  70. labels=[
  71. {'value': 'Answer 1'},
  72. {'value': 'Answer 2'},
  73. {'value': 'Answer 3'},
  74. {'value': 'Answer 4', 'is_correct': True, 'answer_score': 1.0}
  75. ])
  76. q06 = self._add_question(
  77. page_0, 'Question 6', 'simple_choice',
  78. sequence=2,
  79. constr_mandatory=True, constr_error_msg='Please select an answer', survey_id=survey.id,
  80. is_conditional=True, triggering_question_id=q05.id, triggering_answer_id=q05.suggested_answer_ids.filtered(lambda q: q.is_correct).id,
  81. labels=[
  82. {'value': 'Answer 1'},
  83. {'value': 'Answer 2', 'is_correct': True, 'answer_score': 1.0},
  84. {'value': 'Answer 3'},
  85. {'value': 'Answer 4'}
  86. ])
  87. # User opens start page
  88. self._access_start(survey)
  89. # -> this should have generated a new user_input with a token
  90. user_inputs = self.env['survey.user_input'].search([('survey_id', '=', survey.id)])
  91. self.assertEqual(len(user_inputs), 1)
  92. answer_token = user_inputs.access_token
  93. # User begins survey with first page
  94. response = self._access_page(survey, answer_token)
  95. self.assertResponse(response, 200)
  96. csrf_token = self._find_csrf_token(response.text)
  97. r = self._access_begin(survey, answer_token)
  98. self.assertResponse(r, 200)
  99. answers = {
  100. q01: q01.suggested_answer_ids[3], # Right
  101. q02: q02.suggested_answer_ids[1], # Right
  102. q03: q03.suggested_answer_ids[0], # Wrong
  103. q05: q05.suggested_answer_ids[3], # Right
  104. q06: q06.suggested_answer_ids[2], # Wrong
  105. }
  106. self._answer_page(page_0, answers, answer_token, csrf_token)
  107. user_inputs.invalidate_recordset()
  108. self.assertEqual(round(user_inputs.scoring_percentage), 60, "Three right answers out of five (the fourth one is still hidden)")
  109. self.assertFalse(user_inputs.scoring_success)