Skip to content

Commit

Permalink
Add tests for local event handler decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Sep 15, 2023
1 parent b470fcd commit 17989d0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions kolibri/core/auth/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
from django.core.management.base import CommandError
from django.test import TestCase
from morango.registry import syncable_models
from morango.sync.context import CompositeSessionContext
from morango.sync.context import LocalSessionContext
from morango.sync.context import NetworkSessionContext

from ..models import Facility
from kolibri.core.auth.management import utils
from kolibri.core.auth.models import AdHocGroup
from kolibri.core.auth.models import Classroom
from kolibri.core.auth.models import FacilityUser
from kolibri.core.auth.models import LearnerGroup
from kolibri.core.auth.sync_event_hook_utils import _local_event_handler
from kolibri.core.auth.test.test_api import ClassroomFactory
from kolibri.core.auth.test.test_api import FacilityFactory
from kolibri.core.auth.test.test_api import FacilityUserFactory
Expand Down Expand Up @@ -673,3 +677,38 @@ def test_deletion_inclusion(self):
delete_group = get_delete_group_for_facility(facility)
all_deleted_models = set(qs.model for qs in delete_group.get_querysets())
self.assertTrue(all_deleted_models.issuperset(all_facility_models))


class TestLocalEventHandler(TestCase):
def test_local_event_handler_local_context(self):
"""
Test that the local event handler calls the wrapped method when there is a local session context
"""
mock_method = mock.Mock()
context = LocalSessionContext()
context.sync_session = mock.Mock()
_local_event_handler(mock_method)(context)
mock_method.assert_called_once()

def test_local_event_handler_composite_context(self):
"""
Test that the local event handler calls the wrapped method when there is a local session context
"""
mock_method = mock.Mock()
local_context = LocalSessionContext()
network_context = NetworkSessionContext(mock.Mock())
context = CompositeSessionContext([network_context, local_context])
local_context.sync_session = mock.Mock()
_local_event_handler(mock_method)(context)
mock_method.assert_called_once()

def test_local_event_handler_composite_context_no_local_child(self):
"""
Test that the local event handler calls the wrapped method when there is a local session context
"""
mock_method = mock.Mock()
network_context1 = NetworkSessionContext(mock.Mock())
network_context2 = NetworkSessionContext(mock.Mock())
context = CompositeSessionContext([network_context1, network_context2])
_local_event_handler(mock_method)(context)
mock_method.assert_not_called()

0 comments on commit 17989d0

Please sign in to comment.