Skip to content

Commit

Permalink
test: update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Feb 9, 2024
1 parent 98ac6a0 commit a58d05c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
1 change: 0 additions & 1 deletion eventtracking/backends/event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.conf import settings
from openedx_events.analytics.data import TrackingLogData
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED
from openedx_events.event_bus import get_producer

from eventtracking.backends.logger import DateTimeJSONEncoder
from eventtracking.backends.routing import RoutingBackend
Expand Down
39 changes: 15 additions & 24 deletions eventtracking/backends/tests/test_event_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,33 @@ def test_successful_send(self, mocked_send_event):
SEND_TRACKING_EVENT_EMITTED_SIGNAL=True,
EVENT_BUS_TRACKING_LOGS=["sample_event"],
)
@patch("eventtracking.backends.event_bus.get_producer")
def test_successful_send_event(self, mock_get_producer):
mock_send = Mock()
mock_get_producer.return_value = Mock(send=mock_send)

@patch("eventtracking.backends.event_bus.TRACKING_EVENT_EMITTED.send_event")
def test_successful_send_event(self, mock_send_event):
backend = EventBusRoutingBackend()
backend.send(self.sample_event)

mock_get_producer.assert_called()
mock_send.assert_called()
mock_send_event.assert_called()
self.assertDictContainsSubset(
{
"signal": TRACKING_EVENT_EMITTED,
"topic": "analytics",
"event_key_field": "tracking_log.name",
"event_data": {
"tracking_log": TrackingLogData(
name=self.sample_event["name"],
timestamp=datetime.strptime(
self.sample_event["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z"
),
data=json.dumps(self.sample_event["data"]),
context=json.dumps(self.sample_event["context"]),
)
},
"tracking_log": TrackingLogData(
name=self.sample_event["name"],
timestamp=datetime.strptime(
self.sample_event["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z"
),
data=json.dumps(self.sample_event["data"]),
context=json.dumps(self.sample_event["context"]),
)
},
mock_send.call_args.kwargs,
mock_send_event.call_args.kwargs,
)

@patch(
"eventtracking.backends.event_bus.SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled"
)
@patch("eventtracking.backends.event_bus.get_producer")
def test_event_is_disabled(self, mock_get_producer, mock_is_enabled):
@patch("eventtracking.backends.event_bus.TRACKING_EVENT_EMITTED.send_event")
def test_event_is_disabled(self, mock_send_event, mock_is_enabled):
mock_is_enabled.return_value = False
backend = EventBusRoutingBackend()
backend.send(self.sample_event)
mock_is_enabled.assert_called_once()
mock_get_producer.assert_not_called()
mock_send_event.assert_not_called()

0 comments on commit a58d05c

Please sign in to comment.