|
| 1 | +# Copyright 2018 Camptocamp (https://www.camptocamp.com). |
| 2 | +# Copyright 2024 XCG Consulting (https://xcg-consulting.fr). |
| 3 | +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from odoo_test_helper import FakeModelLoader |
| 7 | + |
| 8 | +from odoo.tests import tagged |
| 9 | +from odoo.tools.config import config as odoo_config |
| 10 | + |
| 11 | +from .. import server_env |
| 12 | +from ..models import server_env_mixin |
| 13 | +from . import common |
| 14 | + |
| 15 | + |
| 16 | +# Test need to be run post install otherwise the _register_hook is not called yet |
| 17 | +@tagged("post_install", "-at_install") |
| 18 | +class TestEnv(common.ServerEnvironmentCase): |
| 19 | + @classmethod |
| 20 | + def setUpClass(cls): |
| 21 | + super().setUpClass() |
| 22 | + # Load fake models ->/ |
| 23 | + cls.loader = FakeModelLoader(cls.env, cls.__module__) |
| 24 | + cls.loader.backup_registry() |
| 25 | + from .models import ExternalService |
| 26 | + |
| 27 | + cls.loader.update_registry((ExternalService,)) |
| 28 | + cls.env["external_service"].create([{"name": "ftp2", "description": "another"}]) |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def tearDownClass(cls): |
| 32 | + cls.loader.restore_registry() |
| 33 | + super().tearDownClass() |
| 34 | + |
| 35 | + @patch.dict(odoo_config.options, {"running_env": "autocreate"}) |
| 36 | + def test_autocreate(self): |
| 37 | + original_serv_config = server_env_mixin.serv_config |
| 38 | + try: |
| 39 | + with self.set_config_dir("testfiles"): |
| 40 | + parser = server_env._load_config() |
| 41 | + server_env_mixin.serv_config = parser |
| 42 | + # Needed to force _register_hook with auto creation |
| 43 | + self.loader.update_registry(tuple()) |
| 44 | + |
| 45 | + # auto created record |
| 46 | + record = self.env.ref("__server_environment__.external_service.ftp") |
| 47 | + self.assertEqual(record.name, "ftp") |
| 48 | + self.assertEqual(record.description, "ftp server") |
| 49 | + self.assertEqual(record.host, "sftp.example.com") |
| 50 | + self.assertEqual(record.user, "foo") |
| 51 | + self.assertEqual(record.password, "bar") |
| 52 | + |
| 53 | + # create record in setupClass |
| 54 | + # Test it has no xmlid |
| 55 | + record = self.env.ref( |
| 56 | + "__server_environment__.external_service.ftp2", False |
| 57 | + ) |
| 58 | + self.assertFalse(record) |
| 59 | + # look for it |
| 60 | + record = self.env["external_service"].search([("name", "=", "ftp2")]) |
| 61 | + self.assertEqual(len(record), 1) |
| 62 | + self.assertEqual(record.name, "ftp2") |
| 63 | + # different from __autocreate dict as it is created in setUpClass |
| 64 | + self.assertEqual(record.description, "another") |
| 65 | + self.assertEqual(record.host, "sftp2.example.com") |
| 66 | + self.assertEqual(record.user, "monty") |
| 67 | + self.assertEqual(record.password, "python") |
| 68 | + finally: |
| 69 | + server_env_mixin.serv_config = original_serv_config |
0 commit comments