Rev gateway tests

This commit is contained in:
Cyber MacGeddon 2025-07-13 09:05:06 +01:00
parent a96489b037
commit eed008530d
3 changed files with 30 additions and 39 deletions

View file

@ -3,10 +3,13 @@ Tests for Gateway Config Dispatch
""" """
import pytest import pytest
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch, AsyncMock
from trustgraph.gateway.dispatch.config import ConfigRequestor from trustgraph.gateway.dispatch.config import ConfigRequestor
# Import parent class for local patching
from trustgraph.gateway.dispatch.requestor import ServiceRequestor
class TestConfigRequestor: class TestConfigRequestor:
"""Test cases for ConfigRequestor class""" """Test cases for ConfigRequestor class"""
@ -48,11 +51,14 @@ class TestConfigRequestor:
# Setup translator response # Setup translator response
mock_request_translator.to_pulsar.return_value = "translated_request" mock_request_translator.to_pulsar.return_value = "translated_request"
requestor = ConfigRequestor( # Temporarily patch ServiceRequestor async methods to prevent coroutine warnings
pulsar_client=MagicMock(), with patch.object(ServiceRequestor, 'start', new_callable=AsyncMock), \
consumer="test-consumer", patch.object(ServiceRequestor, 'process', new_callable=AsyncMock):
subscriber="test-subscriber" requestor = ConfigRequestor(
) pulsar_client=MagicMock(),
consumer="test-consumer",
subscriber="test-subscriber"
)
# Call to_request # Call to_request
result = requestor.to_request({"test": "body"}) result = requestor.to_request({"test": "body"})

View file

@ -9,6 +9,8 @@ import uuid
from trustgraph.gateway.dispatch.manager import DispatcherManager, DispatcherWrapper from trustgraph.gateway.dispatch.manager import DispatcherManager, DispatcherWrapper
# Keep the real methods intact for proper testing
class TestDispatcherWrapper: class TestDispatcherWrapper:
"""Test cases for DispatcherWrapper class""" """Test cases for DispatcherWrapper class"""
@ -313,9 +315,12 @@ class TestDispatcherManager:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_process_flow_import_with_invalid_kind(self): async def test_process_flow_import_with_invalid_kind(self):
"""Test process_flow_import with invalid kind""" """Test process_flow_import with invalid kind"""
mock_pulsar_client = Mock() import warnings
mock_config_receiver = Mock() with warnings.catch_warnings():
manager = DispatcherManager(mock_pulsar_client, mock_config_receiver) warnings.simplefilter("ignore", RuntimeWarning)
mock_pulsar_client = Mock()
mock_config_receiver = Mock()
manager = DispatcherManager(mock_pulsar_client, mock_config_receiver)
# Setup test flow # Setup test flow
manager.flows["test_flow"] = { manager.flows["test_flow"] = {

View file

@ -10,37 +10,11 @@ import pulsar
from trustgraph.gateway.service import Api, run, default_pulsar_host, default_prometheus_url, default_timeout, default_port, default_api_token from trustgraph.gateway.service import Api, run, default_pulsar_host, default_prometheus_url, default_timeout, default_port, default_api_token
# Patch async methods at module level to prevent coroutine warnings # Store the original method before patching
async def mock_app_factory(self): _original_app_factory = Api.app_factory
"""Mock app_factory that mimics the real behavior for tests"""
app = web.Application(
middlewares=[],
client_max_size=256 * 1024 * 1024
)
# Mock the real app_factory behavior that tests expect
if hasattr(self, 'config_receiver') and hasattr(self.config_receiver, 'start'):
await self.config_receiver.start()
# Handle custom endpoints
if hasattr(self, 'endpoints'):
for ep in self.endpoints:
if hasattr(ep, 'add_routes'):
ep.add_routes(app)
for ep in self.endpoints:
if hasattr(ep, 'start'):
await ep.start()
# Handle endpoint manager
if hasattr(self, 'endpoint_manager'):
if hasattr(self.endpoint_manager, 'add_routes'):
self.endpoint_manager.add_routes(app)
if hasattr(self.endpoint_manager, 'start'):
await self.endpoint_manager.start()
return app
Api.app_factory = mock_app_factory # Replace with AsyncMock to prevent coroutine warnings during introspection
Api.app_factory = AsyncMock()
class TestApi: class TestApi:
@ -154,6 +128,9 @@ class TestApi:
api = Api() api = Api()
# Temporarily restore the real app_factory for this test
api.app_factory = _original_app_factory.__get__(api, Api)
# Mock the dependencies # Mock the dependencies
api.config_receiver = Mock() api.config_receiver = Mock()
api.config_receiver.start = AsyncMock() api.config_receiver.start = AsyncMock()
@ -181,6 +158,9 @@ class TestApi:
api = Api() api = Api()
# Temporarily restore the real app_factory for this test
api.app_factory = _original_app_factory.__get__(api, Api)
# Mock custom endpoints # Mock custom endpoints
mock_endpoint1 = Mock() mock_endpoint1 = Mock()
mock_endpoint1.add_routes = Mock() mock_endpoint1.add_routes = Mock()