server_environment_ircp: Fix tests using contextmanager

Before, the values used in the tests were coming from server_environment_files_sample
that were made available using a symbolic link in .travis.yml.
Now, it's loaded dynamically at test execution.
This commit is contained in:
Akim Juillerat 2020-02-13 11:03:36 +01:00 committed by jsanchez
parent 9b9847c1a3
commit 1c83f5bc32
1 changed files with 68 additions and 47 deletions

View File

@ -3,14 +3,20 @@
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.modules.module import get_resource_path from odoo.modules.module import get_resource_path
from odoo.tests import common
from odoo.tools import convert_file from odoo.tools import convert_file
from odoo.addons.server_environment.tests.common import ServerEnvironmentCase
class TestEnv(common.TransactionCase): from ..models import ir_config_parameter
class TestEnv(ServerEnvironmentCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.ICP = self.env["ir.config_parameter"] self.ICP = self.env["ir.config_parameter"]
self.env_config = (
"[ir.config_parameter]\n" "ircp_from_config=config_value\n" "ircp_empty=\n"
)
def _load_xml(self, module, filepath): def _load_xml(self, module, filepath):
convert_file( convert_file(
@ -25,59 +31,74 @@ class TestEnv(common.TransactionCase):
def test_get_param(self): def test_get_param(self):
""" Get system parameter from config """ """ Get system parameter from config """
# it's not in db with self.load_config(
res = self.ICP.search([("key", "=", "ircp_from_config")]) public=self.env_config, serv_config_class=ir_config_parameter
self.assertFalse(res) ):
# read so it's created in db # it's not in db
value = self.ICP.get_param("ircp_from_config") res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(value, "config_value") self.assertFalse(res)
# now it's in db # read so it's created in db
res = self.ICP.search([("key", "=", "ircp_from_config")]) value = self.ICP.get_param("ircp_from_config")
self.assertEqual(len(res), 1) self.assertEqual(value, "config_value")
self.assertEqual(res.value, "config_value") # now it's in db
res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 1)
self.assertEqual(res.value, "config_value")
def test_set_param_1(self): def test_set_param_1(self):
""" We can't set parameters that are in config file """ """ We can't set parameters that are in config file """
# when creating, the value is overridden by config file with self.load_config(
self.ICP.set_param("ircp_from_config", "new_value") public=self.env_config, serv_config_class=ir_config_parameter
value = self.ICP.get_param("ircp_from_config") ):
self.assertEqual(value, "config_value") # when creating, the value is overridden by config file
# when writing, the value is overridden by config file self.ICP.set_param("ircp_from_config", "new_value")
res = self.ICP.search([("key", "=", "ircp_from_config")]) value = self.ICP.get_param("ircp_from_config")
self.assertEqual(len(res), 1) self.assertEqual(value, "config_value")
res.write({"value": "new_value"}) # when writing, the value is overridden by config file
value = self.ICP.get_param("ircp_from_config") res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(value, "config_value") self.assertEqual(len(res), 1)
# unlink works normally... res.write({"value": "new_value"})
res = self.ICP.search([("key", "=", "ircp_from_config")]) value = self.ICP.get_param("ircp_from_config")
self.assertEqual(len(res), 1) self.assertEqual(value, "config_value")
res.unlink() # unlink works normally...
res = self.ICP.search([("key", "=", "ircp_from_config")]) res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 0) self.assertEqual(len(res), 1)
# but the value is recreated when getting param again res.unlink()
value = self.ICP.get_param("ircp_from_config") res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(value, "config_value") self.assertEqual(len(res), 0)
res = self.ICP.search([("key", "=", "ircp_from_config")]) # but the value is recreated when getting param again
self.assertEqual(len(res), 1) value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, "config_value")
res = self.ICP.search([("key", "=", "ircp_from_config")])
self.assertEqual(len(res), 1)
def test_set_param_2(self): def test_set_param_2(self):
""" We can set parameters that are not in config file """ """ We can set parameters that are not in config file """
self.ICP.set_param("some.param", "new_value") with self.load_config(
self.assertEqual(self.ICP.get_param("some.param"), "new_value") public=self.env_config, serv_config_class=ir_config_parameter
res = self.ICP.search([("key", "=", "some.param")]) ):
res.unlink() self.ICP.set_param("some.param", "new_value")
res = self.ICP.search([("key", "=", "some.param")]) self.assertEqual(self.ICP.get_param("some.param"), "new_value")
self.assertFalse(res) res = self.ICP.search([("key", "=", "some.param")])
res.unlink()
res = self.ICP.search([("key", "=", "some.param")])
self.assertFalse(res)
def test_empty(self): def test_empty(self):
""" Empty config values cause error """ """ Empty config values cause error """
with self.assertRaises(UserError): with self.load_config(
self.ICP.get_param("ircp_empty") public=self.env_config, serv_config_class=ir_config_parameter
self.assertEqual(self.ICP.get_param("ircp_nonexistant"), False) ):
with self.assertRaises(UserError):
self.ICP.get_param("ircp_empty")
self.assertEqual(self.ICP.get_param("ircp_nonexistant"), False)
def test_override_xmldata(self): def test_override_xmldata(self):
self._load_xml( with self.load_config(
"server_environment_ir_config_parameter", "tests/config_param_test.xml" public=self.env_config, serv_config_class=ir_config_parameter
) ):
value = self.ICP.get_param("ircp_from_config") self._load_xml(
self.assertEqual(value, "config_value") "server_environment_ir_config_parameter", "tests/config_param_test.xml"
)
value = self.ICP.get_param("ircp_from_config")
self.assertEqual(value, "config_value")