Skip to content

Commit

Permalink
test: add test for event bus backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 26, 2023
1 parent af7a47f commit 9c56b7b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 31 deletions.
45 changes: 31 additions & 14 deletions eventtracking/backends/tests/test_event_bus.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""
Test the async routing backend.
"""
from unittest import TestCase
import json

from unittest.mock import sentinel, patch
from unittest import TestCase
from unittest.mock import sentinel, patch, Mock
from eventtracking.backends.event_bus import EventBusRoutingBackend
from openedx_events.analytics.data import TrackingLogData

Expand All @@ -16,9 +17,9 @@ def setUp(self):
super().setUp()
self.sample_event = {
'name': str(sentinel.name),
'data': 'data',
'data': {'foo': 'bar'},
'timestamp': '2020-01-01T12:12:12.000000+00:00',
'context': {},
'context': {'baz': 'qux'},
}

@patch('eventtracking.backends.event_bus.EventBusRoutingBackend.send')
Expand All @@ -27,15 +28,31 @@ def test_successful_send(self, mocked_send_event):
backend.send(self.sample_event)
mocked_send_event.assert_called_once_with(self.sample_event)

@patch('eventtracking.backends.event_bus.TRACKING_EVENT_EMITTED.send_event')
def test_successful_send_event(self, mocked_send_event):
@patch('eventtracking.backends.event_bus.get_producer')
def test_successful_send_event(self, mock_get_producer):
backend = EventBusRoutingBackend()
backend.send(self.sample_event)
mock_send = Mock()

mock_get_producer.return_value = Mock(send=mock_send)

mock_get_producer.assert_called()

mock_send.assert_called()
mock_send.assert_called_once_with(
tracking_log=TrackingLogData(
name=self.sample_event['name'],
timestamp=self.sample_event['timestamp'],
data=json.dumps(self.sample_event['data']),
context=json.dumps(self.sample_event['context']),
)


@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):
mock_is_enabled.return_value = False
backend = EventBusRoutingBackend()
backend.send(self.sample_event)
mocked_send_event.assert_called_once_with(
tracking_log=TrackingLogData(
name=self.sample_event['name'],
timestamp=self.sample_event['timestamp'],
data=self.sample_event['data'],
context=self.sample_event['context']
)
)
mock_is_enabled.assert_called_once()
mock_get_producer.assert_not_called()
2 changes: 0 additions & 2 deletions eventtracking/django/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ def ready(self):
# pylint: disable=import-outside-toplevel
from eventtracking.django.django_tracker import override_default_tracker
override_default_tracker()

import eventtracking.handlers # pylint: disable=unused-import
3 changes: 2 additions & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pytz
six
celery
edx-django-utils
openedx_events
git+https://github.com/edunext/openedx-events@cag/add-openedx-event-signal
edx-toggles
31 changes: 27 additions & 4 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,52 @@ click==8.1.7
# click-didyoumean
# click-plugins
# click-repl
# code-annotations
# edx-django-utils
click-didyoumean==0.3.0
# via celery
click-plugins==1.1.1
# via celery
click-repl==0.3.0
# via celery
code-annotations==1.5.0
# via edx-toggles
django==3.2.22
# via
# -c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt
# -r requirements/base.in
# django-crum
# django-waffle
# edx-django-utils
# edx-toggles
# openedx-events
django-crum==0.7.9
# via edx-django-utils
# via
# edx-django-utils
# edx-toggles
django-waffle==4.0.0
# via edx-django-utils
# via
# edx-django-utils
# edx-toggles
edx-django-utils==5.7.0
# via -r requirements/base.in
# via
# -r requirements/base.in
# edx-toggles
edx-opaque-keys[django]==2.5.1
# via openedx-events
edx-toggles==5.1.0
# via -r requirements/base.in
fastavro==1.8.4
# via openedx-events
jinja2==3.1.2
# via code-annotations
kombu==5.3.2
# via celery
markupsafe==2.1.3
# via jinja2
newrelic==9.1.1
# via edx-django-utils
openedx-events==9.0.0
openedx-events @ git+https://github.com/edunext/openedx-events@cag/add-openedx-event-signal
# via -r requirements/base.in
pbr==5.11.1
# via stevedore
Expand All @@ -75,10 +91,14 @@ pynacl==1.5.0
# via edx-django-utils
python-dateutil==2.8.2
# via celery
python-slugify==8.0.1
# via code-annotations
pytz==2023.3.post1
# via
# -r requirements/base.in
# django
pyyaml==6.0.1
# via code-annotations
six==1.16.0
# via
# -r requirements/base.in
Expand All @@ -87,8 +107,11 @@ sqlparse==0.4.4
# via django
stevedore==5.1.0
# via
# code-annotations
# edx-django-utils
# edx-opaque-keys
text-unidecode==1.3
# via python-slugify
typing-extensions==4.8.0
# via
# asgiref
Expand Down
12 changes: 10 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ code-annotations==1.5.0
# via
# -r requirements/test.txt
# edx-lint
# edx-toggles
coverage[toml]==7.3.2
# via
# -r requirements/ci.txt
Expand All @@ -107,19 +108,24 @@ django==3.2.22
# django-crum
# django-waffle
# edx-django-utils
# edx-toggles
# openedx-events
django-crum==0.7.9
# via
# -r requirements/test.txt
# edx-django-utils
# edx-toggles
django-waffle==4.0.0
# via
# -r requirements/test.txt
# edx-django-utils
# edx-toggles
docutils==0.20.1
# via sphinx
edx-django-utils==5.7.0
# via -r requirements/test.txt
# via
# -r requirements/test.txt
# edx-toggles
edx-lint==5.3.6
# via
# -r requirements/dev.in
Expand All @@ -129,6 +135,8 @@ edx-opaque-keys[django]==2.5.1
# -r requirements/test.txt
# edx-opaque-keys
# openedx-events
edx-toggles==5.1.0
# via -r requirements/test.txt
exceptiongroup==1.1.3
# via
# -r requirements/test.txt
Expand Down Expand Up @@ -182,7 +190,7 @@ newrelic==9.1.1
# via
# -r requirements/test.txt
# edx-django-utils
openedx-events==9.0.0
openedx-events @ git+https://github.com/edunext/openedx-events@cag/add-openedx-event-signal
# via -r requirements/test.txt
packaging==23.2
# via
Expand Down
36 changes: 28 additions & 8 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ click-plugins==1.1.1
# -r requirements/base.txt
# celery
code-annotations==1.5.0
# via edx-lint
# via
# -r requirements/base.txt
# edx-lint
# edx-toggles
coverage[toml]==7.3.2
# via
# coverage
Expand All @@ -73,24 +76,31 @@ dill==0.3.7
# django-crum
# django-waffle
# edx-django-utils
# edx-toggles
# openedx-events
django-crum==0.7.9
# via
# -r requirements/base.txt
# edx-django-utils
# edx-toggles
django-waffle==4.0.0
# via
# -r requirements/base.txt
# edx-django-utils
# edx-toggles
edx-django-utils==5.7.0
# via -r requirements/base.txt
# via
# -r requirements/base.txt
# edx-toggles
edx-lint==5.3.6
# via -r requirements/test.in
edx-opaque-keys[django]==2.5.1
# via
# -r requirements/base.txt
# edx-opaque-keys
# openedx-events
edx-toggles==5.1.0
# via -r requirements/base.txt
exceptiongroup==1.1.3
# via pytest
fastavro==1.8.4
Expand All @@ -102,12 +112,16 @@ iniconfig==2.0.0
isort==5.12.0
# via pylint
jinja2==3.1.2
# via code-annotations
# via
# -r requirements/base.txt
# code-annotations
# via
# -r requirements/base.txt
# celery
markupsafe==2.1.3
# via jinja2
# via
# -r requirements/base.txt
# jinja2
mccabe==0.7.0
# via pylint
mock==5.1.0
Expand All @@ -116,7 +130,7 @@ newrelic==9.1.1
# via
# -r requirements/base.txt
# edx-django-utils
openedx-events==9.0.0
openedx-events @ git+https://github.com/edunext/openedx-events@cag/add-openedx-event-signal
# via -r requirements/base.txt
packaging==23.2
# via pytest
Expand Down Expand Up @@ -172,13 +186,17 @@ python-dateutil==2.8.2
# -r requirements/base.txt
# celery
python-slugify==8.0.1
# via code-annotations
# via
# -r requirements/base.txt
# code-annotations
pytz==2023.3.post1
# via
# -r requirements/base.txt
# django
pyyaml==6.0.1
# via code-annotations
# via
# -r requirements/base.txt
# code-annotations
six==1.16.0
# via
# -r requirements/base.txt
Expand All @@ -195,7 +213,9 @@ stevedore==5.1.0
# edx-django-utils
# edx-opaque-keys
text-unidecode==1.3
# via python-slugify
# via
# -r requirements/base.txt
# python-slugify
tomli==2.0.1
# via
# coverage
Expand Down

0 comments on commit 9c56b7b

Please sign in to comment.