From e9fb8dd3801c1859d7b0cdce95c05d6cc37d3d11 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Mon, 6 Mar 2023 15:35:26 +0100 Subject: [PATCH] component: add is_component_registry_ready Handy function to allow depending modules to check for registry readyness w/o exposing internals. --- component/tests/__init__.py | 1 + component/tests/test_utils.py | 20 ++++++++++++++++++++ component/utils.py | 14 ++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 component/tests/test_utils.py create mode 100644 component/utils.py diff --git a/component/tests/__init__.py b/component/tests/__init__.py index de5a88a91..29c286e26 100644 --- a/component/tests/__init__.py +++ b/component/tests/__init__.py @@ -2,3 +2,4 @@ from . import test_component from . import test_lookup from . import test_work_on +from . import test_utils diff --git a/component/tests/test_utils.py b/component/tests/test_utils.py new file mode 100644 index 000000000..f630a83e6 --- /dev/null +++ b/component/tests/test_utils.py @@ -0,0 +1,20 @@ +# Copyright 2023 Camptocamp SA +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from unittest import mock + +from odoo.addons.component.utils import is_component_registry_ready + +from .common import TransactionComponentRegistryCase + + +class TestUtils(TransactionComponentRegistryCase): + def test_registry_ready(self): + path = "odoo.addons.component.utils.get_component_registry" + with mock.patch(path) as mocked: + mocked.return_value = None + self.assertFalse(is_component_registry_ready(self.env.cr.dbname)) + self._setup_registry(self) + mocked.return_value = self.comp_registry + self.assertTrue(is_component_registry_ready(self.env.cr.dbname)) + self._teardown_registry(self) diff --git a/component/utils.py b/component/utils.py new file mode 100644 index 000000000..66e955125 --- /dev/null +++ b/component/utils.py @@ -0,0 +1,14 @@ +# Copyright 2023 Camptocamp SA +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from .core import _component_databases + + +def get_component_registry(dbname): + return _component_databases.get(dbname) + + +def is_component_registry_ready(dbname): + """Return True if the registry is ready to be used.""" + comp_registry = get_component_registry(dbname) + return comp_registry.ready if comp_registry else False