From 29e8faafc28ae8c71a14c9d0879671985b46f82b Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Mon, 30 Oct 2023 17:06:16 -0500 Subject: [PATCH] chore: quality fixes --- .../backends/async_events_router.py | 11 ++++++----- event_routing_backends/backends/events_router.py | 3 --- .../backends/sync_events_router.py | 11 ++++++----- .../backends/tests/test_events_router.py | 9 +++------ event_routing_backends/tests/test_handlers.py | 12 +++--------- 5 files changed, 18 insertions(+), 28 deletions(-) diff --git a/event_routing_backends/backends/async_events_router.py b/event_routing_backends/backends/async_events_router.py index a09d45a8..78935759 100644 --- a/event_routing_backends/backends/async_events_router.py +++ b/event_routing_backends/backends/async_events_router.py @@ -1,15 +1,16 @@ """ Generic async router to send events to hosts via celery. """ -from event_routing_backends.tasks import dispatch_bulk_events, dispatch_event, dispatch_event_persistent from event_routing_backends.backends.events_router import EventsRouter +from event_routing_backends.tasks import dispatch_bulk_events, dispatch_event, dispatch_event_persistent + class AsyncEventsRouter(EventsRouter): """ Router to send events to hosts via celery using requests library. """ - def dispatch_event(self, event_name, event, router_type, host_configurations): + def dispatch_event(self, event_name, updated_event, router_type, host_configurations): """ Dispatch the event to the configured router. @@ -19,7 +20,7 @@ def dispatch_event(self, event_name, event, router_type, host_configurations): router_type (str): type of the router host_configurations (dict): host configurations dict """ - dispatch_event.delay(event_name, event, router_type, host_configurations) + dispatch_event.delay(event_name, updated_event, router_type, host_configurations) def dispatch_bulk_events(self, events, router_type, host_configurations): """ @@ -32,7 +33,7 @@ def dispatch_bulk_events(self, events, router_type, host_configurations): """ dispatch_bulk_events.delay(events, router_type, host_configurations) - def dispatch_event_persistent(self, event_name, event, router_type, host_configurations): + def dispatch_event_persistent(self, event_name, updated_event, router_type, host_configurations): """ Dispatch the event to the configured router providing persistent storage. @@ -42,4 +43,4 @@ def dispatch_event_persistent(self, event_name, event, router_type, host_configu router_type (str): type of the router host_configurations (dict): host configurations dict """ - dispatch_event_persistent.delay(event_name, event, router_type, host_configurations) + dispatch_event_persistent.delay(event_name, updated_event, router_type, host_configurations) diff --git a/event_routing_backends/backends/events_router.py b/event_routing_backends/backends/events_router.py index bb32b595..4cb7cba3 100644 --- a/event_routing_backends/backends/events_router.py +++ b/event_routing_backends/backends/events_router.py @@ -159,9 +159,6 @@ def send(self, event): for events_for_route in event_routes.values(): for event_name, updated_event, host, is_business_critical in events_for_route: - from event_routing_backends.backends.sync_events_router import SyncEventsRouter - #if event_name == 'edx.course.enrollment.activated' and self.__class__ == SyncEventsRouter: - # raise ValueError(f"event_name {event_name}, host {host}, is_business_critical {is_business_critical}") if is_business_critical: self.dispatch_event_persistent( event_name, diff --git a/event_routing_backends/backends/sync_events_router.py b/event_routing_backends/backends/sync_events_router.py index 3de2df1d..5e21fdcf 100644 --- a/event_routing_backends/backends/sync_events_router.py +++ b/event_routing_backends/backends/sync_events_router.py @@ -1,15 +1,16 @@ """ Generic router to send events to hosts. """ -from event_routing_backends.tasks import send_event, bulk_send_events from event_routing_backends.backends.events_router import EventsRouter +from event_routing_backends.tasks import bulk_send_events, send_event + class SyncEventsRouter(EventsRouter): """ Router to send events to hosts via celery using requests library. """ - def dispatch_event(self, event_name, event, router_type, host_configurations): + def dispatch_event(self, event_name, updated_event, router_type, host_configurations): """ Dispatch the event to the configured router. @@ -19,7 +20,7 @@ def dispatch_event(self, event_name, event, router_type, host_configurations): router_type (str): type of the router host_configurations (dict): host configurations dict """ - send_event(None, event_name, event, router_type, host_configurations) + send_event(None, event_name, updated_event, router_type, host_configurations) def dispatch_bulk_events(self, events, router_type, host_configurations): """ @@ -32,7 +33,7 @@ def dispatch_bulk_events(self, events, router_type, host_configurations): """ bulk_send_events(None, events, router_type, host_configurations) - def dispatch_event_persistent(self, event_name, event, router_type, host_configurations): + def dispatch_event_persistent(self, event_name, updated_event, router_type, host_configurations): """ Dispatch the event to the configured router providing persistent storage. @@ -42,4 +43,4 @@ def dispatch_event_persistent(self, event_name, event, router_type, host_configu router_type (str): type of the router host_configurations (dict): host configurations dict """ - self.dispatch_event(event_name, event, router_type, host_configurations) + self.dispatch_event(event_name, updated_event, router_type, host_configurations) diff --git a/event_routing_backends/backends/tests/test_events_router.py b/event_routing_backends/backends/tests/test_events_router.py index 92590ab2..8f46f208 100644 --- a/event_routing_backends/backends/tests/test_events_router.py +++ b/event_routing_backends/backends/tests/test_events_router.py @@ -1,7 +1,7 @@ """ Test the EventsRouter """ -from unittest.mock import MagicMock, call, patch, sentinel, Mock +from unittest.mock import MagicMock, call, patch, sentinel import ddt from django.conf import settings @@ -214,7 +214,6 @@ def test_with_no_available_hosts(self, mocked_logger, mocked_post): mocked_logger.info.mock_calls ) - def test_with_non_dict_event(self): RouterConfigurationFactory.create( backend_name=RouterConfiguration.XAPI_BACKEND, @@ -227,7 +226,6 @@ def test_with_non_dict_event(self): with self.assertRaises(ValueError): router.send(transformed_event) - def test_unsuccessful_routing_of_event(self): host_configurations = { 'url': 'http://test3.com', @@ -260,9 +258,8 @@ def test_duplicate_xapi_event_id(self, mocked_logger): ) - @ddt.ddt -class TestAsyncEventsRouter(TestEventsRouter): +class TestAsyncEventsRouter(TestEventsRouter): # pylint: disable=test-inherits-tests """ Test the AsyncEventsRouter """ @@ -803,7 +800,7 @@ def test_successful_routing_of_bulk_events( @ddt.ddt -class TestSyncEventsRouter(TestEventsRouter): +class TestSyncEventsRouter(TestEventsRouter): # pylint: disable=test-inherits-tests """ Test the SyncEventsRouter """ diff --git a/event_routing_backends/tests/test_handlers.py b/event_routing_backends/tests/test_handlers.py index ecf2eba3..0a27a78d 100644 --- a/event_routing_backends/tests/test_handlers.py +++ b/event_routing_backends/tests/test_handlers.py @@ -2,7 +2,7 @@ Test handlers for signals emitted by the analytics app """ -from unittest.mock import patch, Mock +from unittest.mock import Mock, patch from django.test import TestCase from django.test.utils import override_settings @@ -26,22 +26,15 @@ class TestHandlers(TestCase): } ) @patch("event_routing_backends.handlers.get_tracker") - @patch("event_routing_backends.handlers.isinstance") def test_send_tracking_log_to_backends( - self, mock_isinstance, mock_get_tracker + self, mock_get_tracker ): """ Test for send_tracking_log_to_backends """ tracker = DjangoTracker() mock_get_tracker.return_value = tracker - mock_backend = Mock() - - tracker.backends["event_bus"].send_to_backends = mock_backend - - # mock_isinstance.return_value = True - tracker.backends["event_bus"].send_to_backends = mock_backend send_tracking_log_to_backends( @@ -63,6 +56,7 @@ def test_send_tracking_log_to_backends( "context": {}, } ) + @override_settings( EVENT_TRACKING_BACKENDS={ "event_bus": {