Skip to content

Commit

Permalink
Feat: 100% Consumer Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk committed Nov 18, 2023
1 parent f4cb36b commit d01c0aa
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions consumer/event_consumer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from abc import ABC
from typing import Type

from common.event import BaseEvent
from common.qconnector import QConnector
Expand Down
28 changes: 26 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
from unittest import mock
import psycopg2
import pytest
from common.event import base_event

from common.qconnector import RabbitMQConnector
from common import log

from producer.event_producer import EventProducer
from consumer.event_consumer import EventConsumer

logger = log.get_logger(__name__)

Expand Down Expand Up @@ -190,3 +190,27 @@ def delete_response():
'new': {},
'diff': {}
}


@pytest.fixture
def event_consumer_init_params():
return {
'qconnector_cls': RabbitMQConnector,
'event_cls': base_event.BaseEvent,
'rabbitmq_url': 'amqp://admin:password@rabbitmq:5672/?heartbeat=0',
'rabbitmq_exchange': 'test',
'queue_name': 'test',
'binding_keys': '#'
}


@pytest.fixture
def mock_consumer(event_consumer_init_params):
return EventConsumer(**event_consumer_init_params)


@pytest.fixture
def mock_event(update_response):
event = base_event.BaseEvent()
event.from_dict(update_response)
return event
58 changes: 58 additions & 0 deletions tests/test_event_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from unittest import mock
from common.event import base_event
from common.qconnector.rabbitmq_connector import RabbitMQConnector
from consumer.event_consumer import EventConsumer


# Test init
def test_init(event_consumer_init_params):
c = EventConsumer(**event_consumer_init_params)

assert c.qconnector_cls == RabbitMQConnector
assert c.event_cls == base_event.BaseEvent
assert c.qconnector._RabbitMQConnector__rabbitmq_url == 'amqp://admin:password@rabbitmq:5672/?heartbeat=0'
assert c.qconnector._RabbitMQConnector__rabbitmq_exchange == 'test'


# Test connect
def test_connect(mock_consumer, mock_pika_connect):
mock_consumer.connect()
mock_pika_connect.assert_called_once()


# Test start_consuming
def test_start_consuming(mock_consumer):
# mock basic_consume
mock_consumer.qconnector.consume_stream = mock.Mock()
mock_consumer.start_consuming()

mock_consumer.qconnector.consume_stream.assert_called_once()

# test stream_consumer function
mock_consumer.process_message = mock.Mock()
mock_consumer.check_shutdown = mock.Mock()
mock_consumer.qconnector.consume_stream.call_args[1]['callback_fn']('test', '{"test": "test"}')

mock_consumer.process_message.assert_called_once()
mock_consumer.check_shutdown.assert_called_once()


# Test process_message
def test_process_message(mock_consumer, mock_event):
with mock.patch('logging.Logger.info') as mock_logger:
mock_consumer.process_message('test', mock_event)
mock_logger.call_count == 3


# Test shutdown
def test_shutdown(mock_consumer):
mock_consumer.shutdown()
assert mock_consumer._EventConsumer__shutdown is True


# Test check_shutdown
def test_check_shutdown(mock_consumer):
mock_consumer._EventConsumer__shutdown = True
mock_consumer.qconnector.check_shutdown = mock.Mock()
mock_consumer.check_shutdown()
mock_consumer.qconnector.check_shutdown.assert_called_once()

0 comments on commit d01c0aa

Please sign in to comment.