Skip to content

Commit

Permalink
✅ Unit Test (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
mraniki authored Jul 8, 2024
2 parents f07c078 + 5d3e101 commit 83f88e6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
54 changes: 30 additions & 24 deletions iamlistening/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ addopts = """
-v
--show-capture=stderr
"""
asyncio_mode = "auto"

[tool.coverage.run]
omit = [
Expand Down
8 changes: 4 additions & 4 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
39 changes: 39 additions & 0 deletions tests/test_unit_exception.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 83f88e6

Please sign in to comment.