From 128d7b5ec0c985f669ff0bb057b4402d612072fa Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Fri, 26 Sep 2025 00:59:11 +0100 Subject: [PATCH] Fixing tests --- .../test_base/test_flow_parameter_specs.py | 53 ++++++++----------- .../test_base/test_llm_service_parameters.py | 33 ++++-------- 2 files changed, 32 insertions(+), 54 deletions(-) diff --git a/tests/unit/test_base/test_flow_parameter_specs.py b/tests/unit/test_base/test_flow_parameter_specs.py index 0a3c3354..c813d66c 100644 --- a/tests/unit/test_base/test_flow_parameter_specs.py +++ b/tests/unit/test_base/test_flow_parameter_specs.py @@ -11,21 +11,24 @@ from trustgraph.base.flow_processor import FlowProcessor from trustgraph.base import ParameterSpec, ConsumerSpec, ProducerSpec +class MockAsyncProcessor: + def __init__(self, **params): + self.config_handlers = [] + self.id = params.get('id', 'test-service') + self.specifications = [] class TestFlowParameterSpecs(IsolatedAsyncioTestCase): """Test flow processor parameter specification functionality""" + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) def test_parameter_spec_registration(self): """Test that parameter specs can be registered with flow processors""" # Arrange - def mock_init(self, **kwargs): - self.config_handlers = [] - self.id = kwargs.get('id', 'test-service') - config = { 'id': 'test-flow-processor', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) @@ -49,16 +52,14 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase): assert "model" in param_names assert "temperature" in param_names + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) def test_mixed_specification_types(self): """Test registration of mixed specification types (parameters, consumers, producers)""" # Arrange - def mock_init(self, **kwargs): - self.config_handlers = [] - self.id = kwargs.get('id', 'test-service') - config = { 'id': 'test-flow-processor', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) @@ -85,16 +86,14 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase): assert len(consumer_specs) == 1 assert len(producer_specs) == 1 + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) def test_parameter_spec_metadata(self): """Test parameter specification metadata handling""" # Arrange - def mock_init(self, **kwargs): - self.config_handlers = [] - self.id = kwargs.get('id', 'test-service') - config = { 'id': 'test-flow-processor', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) @@ -119,16 +118,14 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase): assert model_spec_registered.name == "model" assert temperature_spec_registered.name == "temperature" + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) def test_duplicate_parameter_spec_handling(self): """Test handling of duplicate parameter spec registration""" # Arrange - def mock_init(self, **kwargs): - self.config_handlers = [] - self.id = kwargs.get('id', 'test-service') - config = { 'id': 'test-flow-processor', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) @@ -148,17 +145,15 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase): # Either should have 2 duplicates or the system should handle deduplication assert len(param_specs) >= 1 # At least one should be registered + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) @patch('trustgraph.base.flow_processor.Flow') 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', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) @@ -190,16 +185,14 @@ class TestFlowParameterSpecs(IsolatedAsyncioTestCase): class TestParameterSpecValidation(IsolatedAsyncioTestCase): """Test parameter specification validation functionality""" + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) def test_parameter_spec_name_validation(self): """Test parameter spec name validation""" # Arrange - def mock_init(self, **kwargs): - self.config_handlers = [] - self.id = kwargs.get('id', 'test-service') - config = { 'id': 'test-flow-processor', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } processor = FlowProcessor(**config) diff --git a/tests/unit/test_base/test_llm_service_parameters.py b/tests/unit/test_base/test_llm_service_parameters.py index 4b9abb39..65cdf9a5 100644 --- a/tests/unit/test_base/test_llm_service_parameters.py +++ b/tests/unit/test_base/test_llm_service_parameters.py @@ -91,19 +91,14 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase): assert temperature_spec is not None assert temperature_spec.name == "temperature" + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) async def test_on_request_extracts_parameters_from_flow(self): """Test that on_request method extracts model and temperature from flow""" # Arrange - - # Create service instance and manually initialize required attributes - service = LlmService.__new__(LlmService) - service.config_handlers = [] # Required by FlowProcessor - service.flows = {} # Required by FlowProcessor - service.specifications = [] # Required by FlowProcessor - config = { 'id': 'test-llm-service', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } service = LlmService(**config) @@ -158,19 +153,14 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase): mock_flow.assert_any_call("model") mock_flow.assert_any_call("temperature") + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) async def test_on_request_handles_missing_parameters_gracefully(self): """Test that on_request handles missing parameters gracefully""" # Arrange - - # Create service instance and manually initialize required attributes - service = LlmService.__new__(LlmService) - service.config_handlers = [] # Required by FlowProcessor - service.flows = {} # Required by FlowProcessor - service.specifications = [] # Required by FlowProcessor - config = { 'id': 'test-llm-service', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } service = LlmService(**config) @@ -217,19 +207,14 @@ class TestLlmServiceParameters(IsolatedAsyncioTestCase): assert call_args[0][2] is None # model (will use processor default) assert call_args[0][3] is None # temperature (will use processor default) + @patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor) async def test_on_request_error_handling_preserves_behavior(self): """Test that parameter extraction doesn't break existing error handling""" # Arrange - - # Create service instance and manually initialize required attributes - service = LlmService.__new__(LlmService) - service.config_handlers = [] # Required by FlowProcessor - service.flows = {} # Required by FlowProcessor - service.specifications = [] # Required by FlowProcessor - config = { 'id': 'test-llm-service', - 'concurrency': 1 + 'concurrency': 1, + 'taskgroup': AsyncMock() } service = LlmService(**config)