diff --git a/iamlistening/main.py b/iamlistening/main.py index d86bc68a1..7f1382535 100644 --- a/iamlistening/main.py +++ b/iamlistening/main.py @@ -55,36 +55,41 @@ def __init__(self): None """ # Check if the module is enabled - self.enabled = settings.iamlistening_enabled or True + self.enabled = settings.iamlistening_enabled # Create a mapping of library names to client classes self.client_classes = self.get_all_client_classes() # logger.debug("client_classes available {}", self.client_classes) if not self.enabled: - logger.info("Module is disabled. No clients will be created.") + logger.info("Module is disabled. No Client will be created.") return self.clients = [] - # Create a client for each client in settings.myllm + # Create a client for each client in settings.iamlistening for name, client_config in settings.platform.items(): - # Skip template and empty string client names - if name in ["", "template"] or not client_config.get("enabled"): + if ( + # Skip empty client configs + client_config is None + # Skip non-dict client configs + or not isinstance(client_config, dict) + # Skip template and empty string client names + or name in ["", "template"] + # Skip disabled clients + or not client_config.get("enabled") + ): continue - try: - # Create the client - logger.debug("Creating client {}", name) - client = self._create_client(**client_config, name=name) - # If the client has a valid client attribute, append it to the list - if client and getattr(client, "client", None): - self.clients.append(client) - except Exception as e: - # Log the error if the client fails to be created - logger.error(f"Failed to create client {name}: {e}") + + # Create the client + logger.debug("Creating client {}", name) + client = self._create_client(**client_config, name=name) + # If the client has a valid client attribute, append it to the list + if client and getattr(client, "client", None): + self.clients.append(client) # Log the number of clients that were created logger.info(f"Loaded {len(self.clients)} clients") if not self.clients: logger.warning( - "No clients were created. Check your settings or disable the module." + "No Client were created. Check your settings or disable the module." ) def _create_client(self, **kwargs): @@ -120,14 +125,15 @@ def _create_client(self, **kwargs): library is not supported. """ - library = kwargs.get("platform") - client_class = self.client_classes.get(f"{library.capitalize()}Handler") - - if client_class is None: - logger.error(f"library {library} not supported") - return None - - return client_class(**kwargs) + library = ( + kwargs.get("library") + or kwargs.get("platform") + or kwargs.get("protocol") + or kwargs.get("parser_library") + or "telegram" + ) + cls = self.client_classes.get((f"{library.capitalize()}Handler")) + return None if cls is None else cls(**kwargs) def get_all_client_classes(self): """ diff --git a/pyproject.toml b/pyproject.toml index 6d87dcd93..be89387a3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,6 +249,7 @@ addopts = """ -v --show-capture=stderr """ +asyncio_mode = "auto" [tool.coverage.run] omit = [ diff --git a/tests/test_unit.py b/tests/test_unit.py index 8aa5b1d6f..75cd8a0b0 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -31,10 +31,10 @@ async def test_dynaconf(): @pytest.fixture(name="listener") -def listener(caplog): - fixture = Listener() - assert "notalibrary not supported" in caplog.text - return fixture +def listener(): + return Listener() + # # assert "notalibrary not supported" in caplog.text + # return fixture @pytest.fixture(name="message") diff --git a/tests/test_unit_exception.py b/tests/test_unit_exception.py new file mode 100644 index 000000000..aaf4740d4 --- /dev/null +++ b/tests/test_unit_exception.py @@ -0,0 +1,39 @@ +""" +FindMyOrder Exception Testing +""" + +import pytest + +from iamlistening import Listener +from iamlistening.config import settings + + +@pytest.fixture(scope="session", autouse=True) +def set_test_settings(): + settings.configure(FORCE_ENV_FOR_DYNACONF="exception") + + +@pytest.mark.asyncio +async def test_module_exception(caplog): + result = Listener() + print(result) + assert any( + record.message == "Module is disabled. No Client will be created." + for record in caplog.records + if record.levelname == "INFO" + ) + + +# @pytest.mark.asyncio +async def test_create_client_exception(caplog): + settings.iamlistening_enabled = True + test_class = Listener() + result = test_class._create_client() + print(result) + assert result is not None + assert any( + record.message + == "No Client were created. Check your settings or disable the module." + for record in caplog.records + if record.levelname == "WARNING" + )