From e012f20969b0d28c86752547daa4dffdc24dd444 Mon Sep 17 00:00:00 2001 From: Mmequignon Date: Thu, 15 Jun 2023 13:49:36 +0200 Subject: [PATCH] server_environment_ir_config_parameter: Fix create/write overrides --- .../models/ir_config_parameter.py | 8 +-- .../tests/config_param_test.xml | 5 ++ .../tests/test_server_environment_ircp.py | 59 +++++++++++++++++-- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/server_environment_ir_config_parameter/models/ir_config_parameter.py b/server_environment_ir_config_parameter/models/ir_config_parameter.py index 541aafe..513d880 100644 --- a/server_environment_ir_config_parameter/models/ir_config_parameter.py +++ b/server_environment_ir_config_parameter/models/ir_config_parameter.py @@ -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 diff --git a/server_environment_ir_config_parameter/tests/config_param_test.xml b/server_environment_ir_config_parameter/tests/config_param_test.xml index 29d2a99..297a14e 100644 --- a/server_environment_ir_config_parameter/tests/config_param_test.xml +++ b/server_environment_ir_config_parameter/tests/config_param_test.xml @@ -3,4 +3,9 @@ ircp_from_config value_from_xml + + + other_ircp_from_config + other_value_from_xml + diff --git a/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py b/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py index b8e9485..867cea7 100644 --- a/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py +++ b/server_environment_ir_config_parameter/tests/test_server_environment_ircp.py @@ -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"], + )