Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
test: add test for external id signal
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Sep 25, 2023
1 parent 99e3f54 commit 755614c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion event_sink_clickhouse/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Meta:

class UserExternalIDSerializer(BaseSinkSerializer, serializers.ModelSerializer):
"""Serializer for user external ID events."""

external_id_type = serializers.CharField(source="external_id_type.name")
username = serializers.CharField(source="user.username")

Expand Down Expand Up @@ -114,7 +115,9 @@ def get_course_data_json(self, overview):
json_fields = {
"advertised_start": getattr(overview, "advertised_start", ""),
"announcement": getattr(overview, "announcement", ""),
"lowest_passing_grade": float(getattr(overview, "lowest_passing_grade", 0.0)),
"lowest_passing_grade": float(
getattr(overview, "lowest_passing_grade", 0.0)
),
"invitation_only": getattr(overview, "invitation_only", ""),
"max_student_enrollments_allowed": getattr(
overview, "max_student_enrollments_allowed", None
Expand Down
2 changes: 1 addition & 1 deletion event_sink_clickhouse/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from event_sink_clickhouse.sinks.external_id_sink import ExternalIDSInk
from event_sink_clickhouse.utils import get_model


Expand Down Expand Up @@ -41,7 +42,6 @@ def on_externalid_saved( # pylint: disable=unused-argument # pragma: no cover
"""
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
from event_sink_clickhouse.tasks import dump_data_to_clickhouse # pylint: disable=import-outside-toplevel
from event_sink_clickhouse.sinks.external_id_sink import ExternalIDSInk

sink = ExternalIDSInk(None, None)
dump_data_to_clickhouse.delay(
Expand Down
20 changes: 19 additions & 1 deletion tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from django.test import TestCase

from event_sink_clickhouse.signals import on_user_profile_updated, receive_course_publish
from event_sink_clickhouse.signals import on_externalid_saved, on_user_profile_updated, receive_course_publish
from event_sink_clickhouse.sinks.external_id_sink import ExternalIDSInk


class SignalHandlersTestCase(TestCase):
Expand Down Expand Up @@ -34,3 +35,20 @@ def test_on_user_profile_updated(self, mock_dump_task):
on_user_profile_updated(sender, instance)

mock_dump_task.delay.assert_called_once_with(instance.id)

@patch("event_sink_clickhouse.tasks.dump_data_to_clickhouse")
def test_on_externalid_saved(self, mock_dump_task):
"""
Test that on_externalid_saved calls dump_data_to_clickhouse.
"""
instance = Mock()
sender = Mock()
on_externalid_saved(sender, instance)

sink = ExternalIDSInk(None, None)

mock_dump_task.delay.assert_called_once_with(
sink_module=sink.__module__,
sink_name=sink.__class__.__name__,
object_id=str(instance.id),
)
24 changes: 24 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,27 @@ def test_dump_data_to_clickhouse(self, mock_celery_log, mock_import_module):
connection_overrides={"param": "value"}, log=mock_celery_log
)
mock_Sink_instance.dump.assert_called_once_with("object_id")

@patch("event_sink_clickhouse.tasks.import_module")
@patch("event_sink_clickhouse.tasks.celery_log")
def test_dump_data_to_clickhouse_disabled_sink(
self, mock_celery_log, mock_import_module
):
# Mock the required objects and methods
mock_Sink_class = MagicMock()
mock_Sink_class.is_enabled.return_value = False
mock_Sink_instance = mock_Sink_class.return_value
mock_Sink_instance.dump.return_value = None
mock_import_module.return_value = MagicMock(**{"sink_name": mock_Sink_class})

dump_data_to_clickhouse(
"sink_module",
"sink_name",
"object_id",
connection_overrides={"param": "value"},
)

# Assertions
mock_import_module.assert_called_once_with("sink_module")
mock_Sink_class.assert_not_called()
mock_Sink_instance.dump.assert_not_called()

0 comments on commit 755614c

Please sign in to comment.