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()
# Create a mock Api class without importing the real one # Mock command line arguments
mock_api = Mock(return_value=mock_api_instance) mock_args = Mock()
mock_args.metrics = True
mock_args.metrics_port = 8000
mock_parse_args.return_value = mock_args
# Patch using context manager to avoid importing the real Api class # Create a simple mock instance without any async methods
with patch('trustgraph.gateway.service.Api', mock_api): mock_api_instance = Mock()
# Mock vars() to return a dict mock_api_instance.run = Mock()
with patch('builtins.vars') as mock_vars:
mock_vars.return_value = {
'metrics': True,
'metrics_port': 8000,
'pulsar_host': default_pulsar_host,
'timeout': default_timeout
}
run() # Create a mock Api class without importing the real one
mock_api = Mock(return_value=mock_api_instance)
# Verify metrics server was started # Patch using context manager to avoid importing the real Api class
mock_start_http_server.assert_called_once_with(8000) with patch('trustgraph.gateway.service.Api', mock_api):
# Mock vars() to return a dict
with patch('builtins.vars') as mock_vars:
mock_vars.return_value = {
'metrics': True,
'metrics_port': 8000,
'pulsar_host': default_pulsar_host,
'timeout': default_timeout
}
# Verify Api was created and run was called run()
mock_api.assert_called_once()
mock_api_instance.run.assert_called_once() # 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')