Compare commits
2 Commits
cd6261b34a
...
f40a2f8451
| Author | SHA1 | Date | |
|---|---|---|---|
| f40a2f8451 | |||
| 500f14a57a |
@@ -1,3 +1,2 @@
|
|||||||
from . import test_survey_record_creation
|
from . import test_survey_record_creation
|
||||||
from . import test_end_migration_18_0_1_0_1
|
from . import test_end_migration_18_0_1_0_1
|
||||||
from . import test_computed_fields
|
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
from odoo import api, fields, models
|
|
||||||
|
|
||||||
|
|
||||||
class SurveyRecordGenerationFakeParent(models.Model):
|
|
||||||
_name = "survey.record.generation.fake.parent"
|
|
||||||
_description = "Fake parent record (tests only)"
|
|
||||||
|
|
||||||
name = fields.Char()
|
|
||||||
price = fields.Float()
|
|
||||||
|
|
||||||
|
|
||||||
class SurveyRecordGenerationFakeLine(models.Model):
|
|
||||||
"""Line whose price is computed from its parent, like the price of a
|
|
||||||
subscription line (subscription_oca).
|
|
||||||
|
|
||||||
Pattern reproduced: the survey creates a parent, then a line linked to
|
|
||||||
it, with a price answered by the user. The user's price must be kept,
|
|
||||||
the parent's price only applies when the user gave none.
|
|
||||||
"""
|
|
||||||
|
|
||||||
_name = "survey.record.generation.fake.line"
|
|
||||||
_description = "Fake line record (tests only)"
|
|
||||||
|
|
||||||
parent_id = fields.Many2one("survey.record.generation.fake.parent")
|
|
||||||
price_unit = fields.Float(
|
|
||||||
compute="_compute_price_unit", store=True, readonly=False
|
|
||||||
)
|
|
||||||
|
|
||||||
@api.depends("parent_id.price")
|
|
||||||
def _compute_price_unit(self):
|
|
||||||
for line in self:
|
|
||||||
if line.parent_id:
|
|
||||||
line.price_unit = line.parent_id.price
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
from odoo_test_helper import FakeModelLoader
|
|
||||||
|
|
||||||
from odoo.addons.survey.tests.common import SurveyCase
|
|
||||||
|
|
||||||
|
|
||||||
class TestComputedFields(SurveyCase):
|
|
||||||
"""User's answers must not be overridden by computed fields depending on
|
|
||||||
a link to another generated record.
|
|
||||||
|
|
||||||
Use case: a membership form creating a subscription and a subscription
|
|
||||||
line whose price is typed by the participant (free amount), while the
|
|
||||||
line price is computed from the subscription.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls):
|
|
||||||
super().setUpClass()
|
|
||||||
cls.loader = FakeModelLoader(cls.env, cls.__module__)
|
|
||||||
cls.loader.backup_registry()
|
|
||||||
from .fake_models import (
|
|
||||||
SurveyRecordGenerationFakeLine,
|
|
||||||
SurveyRecordGenerationFakeParent,
|
|
||||||
)
|
|
||||||
|
|
||||||
cls.loader.update_registry(
|
|
||||||
(SurveyRecordGenerationFakeParent, SurveyRecordGenerationFakeLine)
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def tearDownClass(cls):
|
|
||||||
cls.loader.restore_registry()
|
|
||||||
super().tearDownClass()
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super().setUp()
|
|
||||||
self.survey = self.env["survey.survey"].create({"title": "Join us"})
|
|
||||||
self.question_amount = self._add_question(
|
|
||||||
page=None,
|
|
||||||
name="Free amount",
|
|
||||||
qtype="numerical_box",
|
|
||||||
survey_id=self.survey.id,
|
|
||||||
sequence=1,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Parent, with a default price of 20
|
|
||||||
self.parent_creation = self._add_record_creation(
|
|
||||||
"Parent", "survey.record.generation.fake.parent", 0
|
|
||||||
)
|
|
||||||
self._add_field_value(
|
|
||||||
self.parent_creation, "name", value_origin="fixed",
|
|
||||||
fixed_value_char="Membership",
|
|
||||||
)
|
|
||||||
self._add_field_value(
|
|
||||||
self.parent_creation, "price", value_origin="fixed",
|
|
||||||
fixed_value_float=20.0,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Line, linked to the parent, with the price answered by the user
|
|
||||||
self.line_creation = self._add_record_creation(
|
|
||||||
"Line", "survey.record.generation.fake.line", 1
|
|
||||||
)
|
|
||||||
self._add_field_value(
|
|
||||||
self.line_creation, "parent_id", value_origin="other_record",
|
|
||||||
other_created_record_id=self.parent_creation.id,
|
|
||||||
)
|
|
||||||
self._add_field_value(
|
|
||||||
self.line_creation, "price_unit", value_origin="question",
|
|
||||||
question_id=self.question_amount.id,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _add_record_creation(self, name, model, sequence):
|
|
||||||
return self.env["survey.record.creation"].create(
|
|
||||||
{
|
|
||||||
"name": name,
|
|
||||||
"survey_id": self.survey.id,
|
|
||||||
"model_id": self.env["ir.model"]._get(model).id,
|
|
||||||
"sequence": sequence,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
def _add_field_value(self, record_creation, field_name, **vals):
|
|
||||||
field = self.env["ir.model.fields"]._get(
|
|
||||||
record_creation.model_id.model, field_name
|
|
||||||
)
|
|
||||||
return self.env["survey.record.creation.field.values"].create(
|
|
||||||
{
|
|
||||||
"survey_record_creation_id": record_creation.id,
|
|
||||||
"survey_id": self.survey.id,
|
|
||||||
"model_id": record_creation.model_id.id,
|
|
||||||
"field_id": field.id,
|
|
||||||
**vals,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
def _submit(self, amount=None):
|
|
||||||
answer = self._add_answer(
|
|
||||||
survey=self.survey, partner=False, email="jean@test.fr"
|
|
||||||
)
|
|
||||||
if amount is not None:
|
|
||||||
self._add_answer_line(
|
|
||||||
question=self.question_amount, answer=answer, answer_value=amount
|
|
||||||
)
|
|
||||||
answer._mark_done()
|
|
||||||
return self.env["survey.record.generation.fake.line"].search(
|
|
||||||
[("parent_id.name", "=", "Membership")]
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_answer_is_not_overridden_by_computed_field(self):
|
|
||||||
line = self._submit(amount=33)
|
|
||||||
|
|
||||||
self.assertEqual(len(line), 1)
|
|
||||||
self.assertEqual(line.price_unit, 33.0)
|
|
||||||
|
|
||||||
def test_computed_field_applies_when_question_is_not_displayed(self):
|
|
||||||
# E.g. the "free amount" question is only displayed when the user
|
|
||||||
# chose "Other amount": otherwise the line keeps the computed price.
|
|
||||||
line = self._submit()
|
|
||||||
|
|
||||||
self.assertEqual(len(line), 1)
|
|
||||||
self.assertEqual(line.price_unit, 20.0)
|
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from psycopg2 import IntegrityError
|
from psycopg2 import IntegrityError
|
||||||
|
|
||||||
from odoo.addons.survey.tests.common import SurveyCase
|
from odoo.addons.survey.tests.common import SurveyCase
|
||||||
@@ -1073,6 +1075,67 @@ class TestSurveyRecordCreation(SurveyCase):
|
|||||||
self.assertNotEqual(self.answer.partner_id, jeanne)
|
self.assertNotEqual(self.answer.partner_id, jeanne)
|
||||||
self.assertEqual(self.answer.email, jean.email)
|
self.assertEqual(self.answer.email, jean.email)
|
||||||
|
|
||||||
|
def test_link_to_already_created_record_is_set_at_creation(self):
|
||||||
|
# When a NOT required "other_record" field points to a record that
|
||||||
|
# is already created (lower sequence), the link must be given in
|
||||||
|
# create() and not written afterwards: a later write would trigger
|
||||||
|
# computed fields depending on this link (e.g. price_unit of a
|
||||||
|
# sale.subscription.line depends on its subscription) and override
|
||||||
|
# the values answered by the user.
|
||||||
|
self.survey_record_creation.sequence = 0
|
||||||
|
child_creation = self.env["survey.record.creation"].create(
|
||||||
|
{
|
||||||
|
"name": "Child contact",
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"sequence": 1,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": child_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"field_id": self.name_field.id,
|
||||||
|
"value_origin": "fixed",
|
||||||
|
"fixed_value_char": "Child",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
parent_field = self.env["ir.model.fields"]._get("res.partner", "parent_id")
|
||||||
|
self.env["survey.record.creation.field.values"].create(
|
||||||
|
{
|
||||||
|
"survey_record_creation_id": child_creation.id,
|
||||||
|
"survey_id": self.survey.id,
|
||||||
|
"model_id": self.res_partner_model.id,
|
||||||
|
"field_id": parent_field.id,
|
||||||
|
"value_origin": "other_record",
|
||||||
|
"other_created_record_id": self.survey_record_creation.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.answer = self._add_answer(
|
||||||
|
survey=self.survey, partner=False, email="jean@test.fr"
|
||||||
|
)
|
||||||
|
self._add_answer_line(
|
||||||
|
question=self.question_name, answer=self.answer, answer_value="Jean"
|
||||||
|
)
|
||||||
|
|
||||||
|
partner_class = type(self.env["res.partner"])
|
||||||
|
original_write = partner_class.write
|
||||||
|
written_fields = []
|
||||||
|
|
||||||
|
def spy_write(records, vals):
|
||||||
|
written_fields.extend(vals)
|
||||||
|
return original_write(records, vals)
|
||||||
|
|
||||||
|
with patch.object(partner_class, "write", spy_write):
|
||||||
|
self.answer._mark_done()
|
||||||
|
|
||||||
|
jean = self.env["res.partner"].search([("name", "=", "Jean")])
|
||||||
|
child = self.env["res.partner"].search([("name", "=", "Child")])
|
||||||
|
self.assertEqual(child.parent_id, jean)
|
||||||
|
self.assertNotIn("parent_id", written_fields)
|
||||||
|
|
||||||
def test_question_not_displayed_does_not_override_default_value(self):
|
def test_question_not_displayed_does_not_override_default_value(self):
|
||||||
# When a question is not displayed to the user (e.g. a conditional
|
# When a question is not displayed to the user (e.g. a conditional
|
||||||
# question), the field must keep its default (or computed) value
|
# question), the field must keep its default (or computed) value
|
||||||
|
|||||||
Reference in New Issue
Block a user