diff --git a/src/protean/adapters/broker/__init__.py b/src/protean/adapters/broker/__init__.py index 27048f51..5f057e40 100644 --- a/src/protean/adapters/broker/__init__.py +++ b/src/protean/adapters/broker/__init__.py @@ -64,7 +64,7 @@ def _initialize(self): if broker_name not in self._brokers: raise ConfigurationError( - f"Broker {broker_name} has not been configured." + f"Broker `{broker_name}` has not been configured." ) self._brokers[broker_name].register(subscriber.meta_.event, subscriber) diff --git a/src/protean/domain/__init__.py b/src/protean/domain/__init__.py index 4c14b6a2..3165f19f 100644 --- a/src/protean/domain/__init__.py +++ b/src/protean/domain/__init__.py @@ -193,10 +193,12 @@ def init(self, traverse=True): # noqa: C901 This method bubbles up circular import issues, if present, in the domain code. """ + # Initialize domain dependencies and adapters + self._initialize() + if traverse is True: # Standard Library Imports import importlib.util - import inspect import os import pathlib @@ -208,7 +210,7 @@ def init(self, traverse=True): # noqa: C901 logger.debug(f"Loading domain from {dir_name}...") - for root, dirs, files in os.walk(dir_name): + for root, _, files in os.walk(dir_name): if pathlib.PurePath(root).name not in ["__pycache__"]: package_path = root[len(str(system_folder_path)) + 1 :] module_name = package_path.replace(os.sep, ".") @@ -245,16 +247,12 @@ def init(self, traverse=True): # noqa: C901 # Initialize adapters after loading domain self._initialize() - # Initialize domain adapters def _initialize(self): + """Initialize domain dependencies and adapters.""" self.providers._initialize() self.caches._initialize() self.brokers._initialize() - # Reinitialize domain after config changes - def reinitialize(self): - self._initialize() - def make_config(self): """Used to construct the config; invoked by the Domain constructor.""" defaults = dict(self.default_config) diff --git a/tests/conftest.py b/tests/conftest.py index e41c92bb..2be978ad 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -194,10 +194,9 @@ def test_domain(db_config, store_config, request): domain.config["DATABASES"]["default"] = db_config domain.config["EVENT_STORE"] = store_config - with domain.domain_context(): - # Always reinitialize the domain after config changes - domain.reinitialize() + domain._initialize() + with domain.domain_context(): yield domain diff --git a/tests/repository/test_providers.py b/tests/repository/test_providers.py index a748023b..32064bfb 100644 --- a/tests/repository/test_providers.py +++ b/tests/repository/test_providers.py @@ -63,7 +63,7 @@ def custom_test_domain(self, test_domain): "DATABASE": "SQLITE", "DATABASE_URI": "sqlite:///test.db", } - test_domain.reinitialize() + test_domain._initialize() yield test_domain def test_default_repository_construction_and_registration(self, custom_test_domain): diff --git a/tests/repository/test_repository_registration.py b/tests/repository/test_repository_registration.py index 855f2ce0..09c9f1b1 100644 --- a/tests/repository/test_repository_registration.py +++ b/tests/repository/test_repository_registration.py @@ -122,7 +122,7 @@ def test_retrieving_the_database_specific_repository(self, test_domain): "DATABASE": Database.ELASTICSEARCH.value, "DATABASE_URI": {"hosts": ["localhost"]}, } - test_domain.reinitialize() + test_domain._initialize() @test_domain.aggregate class User: diff --git a/tests/shared.py b/tests/shared.py index c57b8b83..dd72112e 100644 --- a/tests/shared.py +++ b/tests/shared.py @@ -22,8 +22,8 @@ def initialize_domain(file_path): if os.path.exists(config_path): domain.config.from_pyfile(config_path) - # Always reinitialize the domain after config changes - domain.reinitialize() + # Reinitialize the domain after config changes + domain._initialize() return domain diff --git a/tests/test_brokers.py b/tests/test_brokers.py index ffc45532..2e696250 100644 --- a/tests/test_brokers.py +++ b/tests/test_brokers.py @@ -140,7 +140,7 @@ class TestBrokerSubscriberInitialization: def test_that_registered_subscribers_are_initialized(self, test_domain): test_domain.register(NotifySSOSubscriber) - test_domain.reinitialize() + test_domain._initialize() assert ( "tests.test_brokers.PersonAdded" @@ -159,8 +159,10 @@ def test_that_subscribers_with_unknown_brokers_cannot_be_initialized( NotifySSOSubscriber.meta_.broker = "unknown" test_domain.register(NotifySSOSubscriber) - with pytest.raises(ConfigurationError): - test_domain.reinitialize() + with pytest.raises(ConfigurationError) as exc: + test_domain._initialize() + + assert "Broker `unknown` has not been configured." in str(exc.value) # Reset the broker after test NotifySSOSubscriber.meta_.broker = "default"