From 17989d0ac7e9940c6d667375a938e423fd1afa86 Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Fri, 15 Sep 2023 08:16:17 -0700 Subject: [PATCH] Add tests for local event handler decorator. --- kolibri/core/auth/test/test_utils.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/kolibri/core/auth/test/test_utils.py b/kolibri/core/auth/test/test_utils.py index 71bca53c3f2..4d66120fd3b 100644 --- a/kolibri/core/auth/test/test_utils.py +++ b/kolibri/core/auth/test/test_utils.py @@ -11,6 +11,9 @@ 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 @@ -18,6 +21,7 @@ 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 @@ -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()