Skip to content

Commit

Permalink
Remove explicit reinitialization() method/API in domain
Browse files Browse the repository at this point in the history
`domain.init()` should reinitialize dependencies. When there is a need
for explicit reinitialization, for e.g. after changing config during
testing, we can simply use `domain._initialize()`.
  • Loading branch information
subhashb committed Apr 29, 2024
1 parent 426ba01 commit be247fd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/protean/adapters/broker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions src/protean/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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, ".")
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/repository/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/repository/test_repository_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions tests/test_brokers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

0 comments on commit be247fd

Please sign in to comment.