Skip to content

Commit

Permalink
test: add event-tracking test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Aug 23, 2023
1 parent fa3a781 commit 2a8e44c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
51 changes: 51 additions & 0 deletions completion/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from datetime import datetime

from django.contrib import auth
from django.test.utils import override_settings
from edx_toggles.toggles.testutils import override_waffle_switch
from eventtracking import tracker
from django.test import TestCase
from eventtracking.django import DjangoTracker
import factory
from factory.django import DjangoModelFactory
from opaque_keys.edx.keys import UsageKey
Expand Down Expand Up @@ -120,3 +124,50 @@ def override_completion_switch(self, enabled):
"""
with override_waffle_switch(waffle.ENABLE_COMPLETION_TRACKING_SWITCH, enabled):
yield


IN_MEMORY_BACKEND_CONFIG = {
'mem': {
'ENGINE': 'completion.test_utils.InMemoryBackend'
}
}


class InMemoryBackend:
"""A backend that simply stores all events in memory"""

def __init__(self):
super().__init__() # lint-amnesty, pylint: disable=super-with-arguments
self.events = []

def send(self, event):
"""Store the event in a list"""
self.events.append(event)


@override_settings(
EVENT_TRACKING_BACKENDS=IN_MEMORY_BACKEND_CONFIG
)
class EventTrackingTestCase(TestCase):
"""
Supports capturing of emitted events in memory and inspecting them.
Each test gets a "clean slate" and can retrieve any events emitted during their execution.
"""

# Make this more robust to the addition of new events that the test doesn't care about.

def setUp(self):
super().setUp() # lint-amnesty, pylint: disable=super-with-arguments

self.recreate_tracker()

def recreate_tracker(self):
"""
Re-initialize the tracking system using updated django settings.
Use this if you make use of the @override_settings decorator to customize the tracker configuration.
"""
self.tracker = DjangoTracker()
tracker.register_tracker(self.tracker)
4 changes: 2 additions & 2 deletions completion/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from opaque_keys.edx.keys import CourseKey, UsageKey

from .. import models
from ..test_utils import CompletionSetUpMixin, UserFactory, submit_completions_for_testing
from ..test_utils import CompletionSetUpMixin, EventTrackingTestCase, UserFactory, submit_completions_for_testing


class PercentValidatorTestCase(TestCase):
Expand All @@ -29,7 +29,7 @@ def test_invalid_percent(self):
self.assertRaises(ValidationError, models.validate_percent, value)


class SubmitCompletionTestCase(CompletionSetUpMixin, TestCase):
class SubmitCompletionTestCase(CompletionSetUpMixin, EventTrackingTestCase, TestCase):
"""
Test that BlockCompletion.objects.submit_completion has the desired
semantics.
Expand Down

0 comments on commit 2a8e44c

Please sign in to comment.