Skip to content

Commit

Permalink
Define methodology for RMQ unit tests with basic initialization test …
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
NeonDaniel committed Nov 28, 2024
1 parent 68c8b0c commit 70f3b43
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install apt dependencies
run: |
apt update
apt install -y rabbitmq-server
- name: Install package
run: |
python -m pip install --upgrade pip
Expand Down
5 changes: 3 additions & 2 deletions neon_llm_core/rmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from abc import abstractmethod, ABC
from threading import Thread
from typing import Optional

from neon_mq_connector.connector import MQConnector
from neon_mq_connector.utils.rabbit_utils import create_mq_callback
Expand All @@ -44,10 +45,10 @@ class NeonLLMMQConnector(MQConnector, ABC):
"""
Module for processing MQ requests to Fast Chat LLM
"""
def __init__(self):
def __init__(self, config: Optional[dict] = None):
self.service_name = f'neon_llm_{self.name}'

self.ovos_config = load_config()
self.ovos_config = config or load_config()
mq_config = self.ovos_config.get("MQ", dict())
super().__init__(config=mq_config, service_name=self.service_name)
self.vhost = LLM_VHOST
Expand Down
3 changes: 2 additions & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest
pytest
pytest-rabbitmq
102 changes: 100 additions & 2 deletions tests/test_rmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,107 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pytest

from unittest import TestCase
from unittest.mock import Mock

from mirakuru import ProcessExitedWithError
from port_for import get_port
from pytest_rabbitmq.factories.executor import RabbitMqExecutor
from pytest_rabbitmq.factories.process import get_config

from neon_llm_core.llm import NeonLLM
from neon_llm_core.rmq import NeonLLMMQConnector


class NeonMockLlm(NeonLLMMQConnector):
def __init__(self):
config = {"MQ": {"server": "127.0.0.1", "port": 25672,
"users": {
"mq_handler": {"user": "neon_api_utils",
"password": "Klatchat2021"},
"neon_llm_mock_mq": {"user": "test_llm_user",
"password": "test_llm_password"}}}}
NeonLLMMQConnector.__init__(self, config=config)

@property
def name(self):
return "mock_mq"

@property
def model(self) -> NeonLLM:
return Mock()

@staticmethod
def compose_opinion_prompt(respondent_nick: str,
question: str,
answer: str) -> str:
return "opinion prompt"


@pytest.fixture(scope="class")
def rmq_instance(request, tmp_path_factory):
config = get_config(request)
rabbit_ctl = config["ctl"]
rabbit_server = config["server"]
rabbit_host = "127.0.0.1"
rabbit_port = 25672
rabbit_distribution_port = get_port(
config["distribution_port"], [rabbit_port]
)
assert rabbit_distribution_port
assert (
rabbit_distribution_port != rabbit_port
), "rabbit_port and distribution_port can not be the same!"

tmpdir = tmp_path_factory.mktemp(f"pytest-rabbitmq-{request.fixturename}")

rabbit_plugin_path = config["plugindir"]

rabbit_logpath = config["logsdir"]

if not rabbit_logpath:
rabbit_logpath = tmpdir / "logs"

rabbit_executor = RabbitMqExecutor(
rabbit_server,
rabbit_host,
rabbit_port,
rabbit_distribution_port,
rabbit_ctl,
logpath=rabbit_logpath,
path=tmpdir,
plugin_path=rabbit_plugin_path,
node_name=config["node"],
)

rabbit_executor.start()

# Init RMQ config
rabbit_executor.rabbitctl_output("add_user", "test_llm_user", "test_llm_password")
rabbit_executor.rabbitctl_output("add_vhost", "/llm")
rabbit_executor.rabbitctl_output("set_permissions_globally", "test_llm_user", ".*", ".*", ".*")

request.cls.rmq_instance = rabbit_executor


@pytest.mark.usefixtures("rmq_instance")
class TestNeonLLMMQConnector(TestCase):
# TODO
pass

@classmethod
def tearDownClass(cls):
try:
cls.rmq_instance.stop()
except ProcessExitedWithError:
pass

def test_00_init(self):
self.mq_llm = NeonMockLlm()

self.assertIn(self.mq_llm.name, self.mq_llm.service_name)
self.assertIsInstance(self.mq_llm.ovos_config, dict)
self.assertEqual(self.mq_llm.vhost, "/llm")
self.assertIsNotNone(self.mq_llm.model)
self.assertEqual(self.mq_llm._personas_provider.service_name,
self.mq_llm.name)

0 comments on commit 70f3b43

Please sign in to comment.