server_environment_ir_config_parameter: Fix create/write overrides

This commit is contained in:
Mmequignon 2023-06-15 13:49:36 +02:00 committed by Marcos Oitaben
parent 30b9a58915
commit e1fe05c712
3 changed files with 64 additions and 8 deletions

View File

@ -59,18 +59,18 @@ class IrConfigParameter(models.Model):
def create(self, vals_list):
for vals in vals_list:
key = vals.get("key")
if serv_config.has_option(SECTION, key):
if key and serv_config.has_option(SECTION, key):
# enforce value from config file
vals = dict(vals, value=serv_config.get(SECTION, key))
vals.update(value=serv_config.get(SECTION, key))
return super().create(vals_list)
def write(self, vals):
for rec in self:
key = vals.get("key") or rec.key
key = vals.get("key", rec.key)
if serv_config.has_option(SECTION, key):
# enforce value from config file
newvals = dict(vals, value=serv_config.get(SECTION, key))
else:
newvals = vals
super().write(newvals)
super(IrConfigParameter, rec).write(newvals)
return True

View File

@ -3,4 +3,9 @@
<field name="key">ircp_from_config</field>
<field name="value">value_from_xml</field>
</record>
<record model="ir.config_parameter" id="some_other_record_id">
<field name="key">other_ircp_from_config</field>
<field name="value">other_value_from_xml</field>
</record>
</odoo>

View File

@ -9,15 +9,22 @@ from odoo.addons.server_environment.tests.common import ServerEnvironmentCase
from ..models import ir_config_parameter
CONFIG = """
[ir.config_parameter]
ircp_from_config=config_value
other_ircp_from_config=other_config_value
ircp_without_record=config_value_without_record
other_ircp_without_record=other_config_value_without_record
ircp_empty=
mail.catchall.alias=my_alias
"""
class TestEnv(ServerEnvironmentCase):
def setUp(self):
super().setUp()
self.ICP = self.env["ir.config_parameter"]
self.env_config = """[ir.config_parameter]
ircp_from_config=config_value
ircp_empty=
mail.catchall.alias=my_alias"""
self.env_config = CONFIG
def _load_xml(self, module, filepath):
convert_file(
@ -116,3 +123,47 @@ mail.catchall.alias=my_alias"""
res = self.ICP.search([("key", "=", "mail.catchall.alias")])
self.assertEqual(len(res), 1)
self.assertEqual(res.value, "my_alias")
def test_write(self):
# there's a write override, test it here
self._load_xml(
"server_environment_ir_config_parameter", "tests/config_param_test.xml"
)
with self.load_config(
public=self.env_config, serv_config_class=ir_config_parameter
):
ICP = self.ICP
icp1 = ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(icp1.value, "value_from_xml")
icp2 = ICP.search([("key", "=", "other_ircp_from_config")])
self.assertEqual(icp2.value, "other_value_from_xml")
# Ensures that each record has its own value at write
(icp1 | icp2).write({"value": "test"})
self.assertEqual(icp1.value, "config_value")
self.assertEqual(icp2.value, "other_config_value")
self.assertEqual(ICP.get_param(icp1.key), "config_value")
self.assertEqual(ICP.get_param(icp2.key), "other_config_value")
def test_create(self):
self._load_xml(
"server_environment_ir_config_parameter", "tests/config_param_test.xml"
)
with self.load_config(
public=self.env_config, serv_config_class=ir_config_parameter
):
vals = [
{
"key": "ircp_without_record",
"value": "NOPE",
},
{
"key": "other_ircp_without_record",
"value": "NOPE",
},
]
records = self.ICP.create(vals)
# Ensures each record has its own value at create
self.assertEqual(
records.mapped("value"),
["config_value_without_record", "other_config_value_without_record"],
)