Fix unit tests

This commit is contained in:
Cyber MacGeddon 2025-12-18 21:49:10 +00:00
parent 8a7be0a3f1
commit 6f18e11b26
2 changed files with 36 additions and 35 deletions

View file

@ -14,19 +14,20 @@ from trustgraph.base.async_processor import AsyncProcessor
class TestAsyncProcessorSimple(IsolatedAsyncioTestCase): class TestAsyncProcessorSimple(IsolatedAsyncioTestCase):
"""Test AsyncProcessor base class functionality""" """Test AsyncProcessor base class functionality"""
@patch('trustgraph.base.async_processor.PulsarClient') @patch('trustgraph.base.async_processor.get_pubsub')
@patch('trustgraph.base.async_processor.Consumer') @patch('trustgraph.base.async_processor.Consumer')
@patch('trustgraph.base.async_processor.ProcessorMetrics') @patch('trustgraph.base.async_processor.ProcessorMetrics')
@patch('trustgraph.base.async_processor.ConsumerMetrics') @patch('trustgraph.base.async_processor.ConsumerMetrics')
async def test_async_processor_initialization_basic(self, mock_consumer_metrics, mock_processor_metrics, async def test_async_processor_initialization_basic(self, mock_consumer_metrics, mock_processor_metrics,
mock_consumer, mock_pulsar_client): mock_consumer, mock_get_pubsub):
"""Test basic AsyncProcessor initialization""" """Test basic AsyncProcessor initialization"""
# Arrange # Arrange
mock_pulsar_client.return_value = MagicMock() mock_backend = MagicMock()
mock_get_pubsub.return_value = mock_backend
mock_consumer.return_value = MagicMock() mock_consumer.return_value = MagicMock()
mock_processor_metrics.return_value = MagicMock() mock_processor_metrics.return_value = MagicMock()
mock_consumer_metrics.return_value = MagicMock() mock_consumer_metrics.return_value = MagicMock()
config = { config = {
'id': 'test-async-processor', 'id': 'test-async-processor',
'taskgroup': AsyncMock() 'taskgroup': AsyncMock()
@ -42,14 +43,14 @@ class TestAsyncProcessorSimple(IsolatedAsyncioTestCase):
assert processor.running == True assert processor.running == True
assert hasattr(processor, 'config_handlers') assert hasattr(processor, 'config_handlers')
assert processor.config_handlers == [] assert processor.config_handlers == []
# Verify PulsarClient was created # Verify get_pubsub was called to create backend
mock_pulsar_client.assert_called_once_with(**config) mock_get_pubsub.assert_called_once_with(**config)
# Verify metrics were initialized # Verify metrics were initialized
mock_processor_metrics.assert_called_once() mock_processor_metrics.assert_called_once()
mock_consumer_metrics.assert_called_once() mock_consumer_metrics.assert_called_once()
# Verify Consumer was created for config subscription # Verify Consumer was created for config subscription
mock_consumer.assert_called_once() mock_consumer.assert_called_once()

View file

@ -8,22 +8,22 @@ from trustgraph.base.publisher import Publisher
@pytest.fixture @pytest.fixture
def mock_pulsar_client(): def mock_pulsar_backend():
"""Mock Pulsar client for testing.""" """Mock Pulsar backend for testing."""
client = MagicMock() backend = MagicMock()
producer = AsyncMock() producer = AsyncMock()
producer.send = MagicMock() producer.send = MagicMock()
producer.flush = MagicMock() producer.flush = MagicMock()
producer.close = MagicMock() producer.close = MagicMock()
client.create_producer.return_value = producer backend.create_producer.return_value = producer
return client return backend
@pytest.fixture @pytest.fixture
def publisher(mock_pulsar_client): def publisher(mock_pulsar_backend):
"""Create Publisher instance for testing.""" """Create Publisher instance for testing."""
return Publisher( return Publisher(
client=mock_pulsar_client, backend=mock_pulsar_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -34,12 +34,12 @@ def publisher(mock_pulsar_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_queue_drain(): async def test_publisher_queue_drain():
"""Verify Publisher drains queue on shutdown.""" """Verify Publisher drains queue on shutdown."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -85,12 +85,12 @@ async def test_publisher_queue_drain():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_rejects_messages_during_drain(): async def test_publisher_rejects_messages_during_drain():
"""Verify Publisher rejects new messages during shutdown.""" """Verify Publisher rejects new messages during shutdown."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -113,12 +113,12 @@ async def test_publisher_rejects_messages_during_drain():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_drain_timeout(): async def test_publisher_drain_timeout():
"""Verify Publisher respects drain timeout.""" """Verify Publisher respects drain timeout."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -169,12 +169,12 @@ async def test_publisher_drain_timeout():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_successful_drain(): async def test_publisher_successful_drain():
"""Verify Publisher drains successfully under normal conditions.""" """Verify Publisher drains successfully under normal conditions."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -224,12 +224,12 @@ async def test_publisher_successful_drain():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_state_transitions(): async def test_publisher_state_transitions():
"""Test Publisher state transitions during graceful shutdown.""" """Test Publisher state transitions during graceful shutdown."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,
@ -276,9 +276,9 @@ async def test_publisher_state_transitions():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_publisher_exception_handling(): async def test_publisher_exception_handling():
"""Test Publisher handles exceptions during drain gracefully.""" """Test Publisher handles exceptions during drain gracefully."""
mock_client = MagicMock() mock_backend = MagicMock()
mock_producer = MagicMock() mock_producer = MagicMock()
mock_client.create_producer.return_value = mock_producer mock_backend.create_producer.return_value = mock_producer
# Mock producer.send to raise exception on second call # Mock producer.send to raise exception on second call
call_count = 0 call_count = 0
@ -291,7 +291,7 @@ async def test_publisher_exception_handling():
mock_producer.send.side_effect = failing_send mock_producer.send.side_effect = failing_send
publisher = Publisher( publisher = Publisher(
client=mock_client, backend=mock_backend,
topic="test-topic", topic="test-topic",
schema=dict, schema=dict,
max_size=10, max_size=10,