Rev gateway tests

This commit is contained in:
Cyber MacGeddon 2025-07-14 14:02:58 +01:00
parent 32f02d6e22
commit 6d29214b72

View file

@ -216,45 +216,47 @@ class TestApi:
class TestRunFunction: class TestRunFunction:
"""Test cases for the run() function""" """Test cases for the run() function"""
@patch('trustgraph.gateway.service.start_http_server') def test_run_function_with_metrics_enabled(self):
@patch('argparse.ArgumentParser.parse_args')
def test_run_function_with_metrics_enabled(self, mock_parse_args, mock_start_http_server):
"""Test run function with metrics enabled""" """Test run function with metrics enabled"""
import warnings import warnings
# Suppress the specific async warning for this test # Suppress the specific async warning with a broader pattern
warnings.filterwarnings("ignore", message="coroutine 'Api.app_factory' was never awaited") warnings.filterwarnings("ignore", message=".*Api.app_factory.*was never awaited", category=RuntimeWarning)
# Mock command line arguments
mock_args = Mock()
mock_args.metrics = True
mock_args.metrics_port = 8000
mock_parse_args.return_value = mock_args
# Create a simple mock instance without any async methods with patch('argparse.ArgumentParser.parse_args') as mock_parse_args, \
mock_api_instance = Mock() patch('trustgraph.gateway.service.start_http_server') as mock_start_http_server:
mock_api_instance.run = Mock()
# Mock command line arguments
# Create a mock Api class without importing the real one mock_args = Mock()
mock_api = Mock(return_value=mock_api_instance) mock_args.metrics = True
mock_args.metrics_port = 8000
# Patch using context manager to avoid importing the real Api class mock_parse_args.return_value = mock_args
with patch('trustgraph.gateway.service.Api', mock_api):
# Mock vars() to return a dict # Create a simple mock instance without any async methods
with patch('builtins.vars') as mock_vars: mock_api_instance = Mock()
mock_vars.return_value = { mock_api_instance.run = Mock()
'metrics': True,
'metrics_port': 8000, # Create a mock Api class without importing the real one
'pulsar_host': default_pulsar_host, mock_api = Mock(return_value=mock_api_instance)
'timeout': default_timeout
} # Patch using context manager to avoid importing the real Api class
with patch('trustgraph.gateway.service.Api', mock_api):
run() # Mock vars() to return a dict
with patch('builtins.vars') as mock_vars:
# Verify metrics server was started mock_vars.return_value = {
mock_start_http_server.assert_called_once_with(8000) 'metrics': True,
'metrics_port': 8000,
# Verify Api was created and run was called 'pulsar_host': default_pulsar_host,
mock_api.assert_called_once() 'timeout': default_timeout
mock_api_instance.run.assert_called_once() }
run()
# Verify metrics server was started
mock_start_http_server.assert_called_once_with(8000)
# Verify Api was created and run was called
mock_api.assert_called_once()
mock_api_instance.run.assert_called_once()
@patch('trustgraph.gateway.service.start_http_server') @patch('trustgraph.gateway.service.start_http_server')
@patch('argparse.ArgumentParser.parse_args') @patch('argparse.ArgumentParser.parse_args')