Fix tests

This commit is contained in:
Cyber MacGeddon 2025-09-26 00:33:28 +01:00
parent f30bc1f1c9
commit 43c8d25092
2 changed files with 23 additions and 4 deletions

View file

@ -14,6 +14,7 @@ from trustgraph.base import ParameterSpec, ConsumerSpec, ProducerSpec
def mock_async_processor_init(self, **params):
"""Mock AsyncProcessor.__init__ that properly initializes required attributes"""
self.config_handlers = []
self.id = params.get('id', 'test-service')
# Apply the mock globally for this test module
@ -28,6 +29,7 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',
@ -60,6 +62,7 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',
@ -70,7 +73,7 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
# Create different spec types
param_spec = ParameterSpec(name="model")
consumer_spec = ConsumerSpec(name="input")
consumer_spec = ConsumerSpec(name="input", schema=MagicMock(), handler=MagicMock())
producer_spec = ProducerSpec(name="output")
# Act
@ -95,6 +98,7 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',
@ -128,6 +132,7 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',
@ -152,11 +157,12 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase):
assert len(param_specs) >= 1 # At least one should be registered
@patch('trustgraph.base.flow_processor.Flow')
async def test_parameter_specs_available_to_flows(self, mock_async_init, mock_flow_class):
async def test_parameter_specs_available_to_flows(self, mock_flow_class):
"""Test that parameter specs are available when flows are created"""
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',
@ -197,6 +203,7 @@ class TestParameterSpecValidation(IsolatedAsyncioTestCase):
# Arrange
def mock_init(self, **kwargs):
self.config_handlers = []
self.id = kwargs.get('id', 'test-service')
config = {
'id': 'test-flow-processor',

View file

@ -103,6 +103,10 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase):
service = LlmService(**config)
# Mock the metrics
service.text_completion_model_metric = MagicMock()
service.text_completion_model_metric.labels.return_value.info = AsyncMock()
# Mock the generate_content method to capture parameters
service.generate_content = AsyncMock(return_value=LlmResult(
text="test response",
@ -130,7 +134,7 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase):
}.get(param, f"mock-{param}")
mock_producer = AsyncMock()
mock_flow.return_value = mock_producer # flow("response") returns producer
mock_flow.producer = {"response": mock_producer}
# Act
await service.on_request(mock_message, mock_consumer, mock_flow)
@ -166,6 +170,10 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase):
service = LlmService(**config)
# Mock the metrics
service.text_completion_model_metric = MagicMock()
service.text_completion_model_metric.labels.return_value.info = AsyncMock()
# Mock the generate_content method
service.generate_content = AsyncMock(return_value=LlmResult(
text="test response",
@ -189,7 +197,7 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase):
mock_flow.return_value = None # Both parameters return None
mock_producer = AsyncMock()
mock_flow.return_value = mock_producer
mock_flow.producer = {"response": mock_producer}
# Act
await service.on_request(mock_message, mock_consumer, mock_flow)
@ -221,6 +229,10 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase):
service = LlmService(**config)
# Mock the metrics
service.text_completion_model_metric = MagicMock()
service.text_completion_model_metric.labels.return_value.info = AsyncMock()
# Mock generate_content to raise an exception
service.generate_content = AsyncMock(side_effect=Exception("Test error"))