diff --git a/consumer/event_consumer.py b/consumer/event_consumer.py index 7f4b810..16a5f4a 100644 --- a/consumer/event_consumer.py +++ b/consumer/event_consumer.py @@ -1,5 +1,6 @@ import json from abc import ABC +from typing import Type from common.event import BaseEvent from common.qconnector import QConnector diff --git a/tests/conftest.py b/tests/conftest.py index 92d8c82..8319909 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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__) @@ -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 diff --git a/tests/test_event_consumer.py b/tests/test_event_consumer.py new file mode 100644 index 0000000..6814c97 --- /dev/null +++ b/tests/test_event_consumer.py @@ -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()