-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initialized event transformer
- Loading branch information
1 parent
e482a1e
commit 2e12c1b
Showing
18 changed files
with
250 additions
and
8 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
eox_nelp/edxapp_wrapper/backends/event_routing_backends_m_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Backend for event-routing-backends library. | ||
This is required since the library has explicit dependencies from openedx platform. | ||
https://github.com/openedx/event-routing-backends | ||
""" | ||
from event_routing_backends.processors.xapi import constants | ||
from event_routing_backends.processors.xapi.registry import XApiTransformersRegistry | ||
from event_routing_backends.processors.xapi.transformer import XApiTransformer | ||
|
||
|
||
def get_xapi_constants(): | ||
"""Allow to get the constants module from | ||
https://github.com/openedx/event-routing-backends/blob/master/event_routing_backends/processors/xapi/constants.py | ||
Returns: | ||
constants module. | ||
""" | ||
return constants | ||
|
||
|
||
def get_xapi_transformer_registry(): | ||
"""Allow to get the XApiTransformersRegistry class from | ||
https://github.com/openedx/event-routing-backends/blob/master/event_routing_backends/processors/xapi/registry.py#L7 | ||
Returns: | ||
XApiTransformersRegistry class. | ||
""" | ||
return XApiTransformersRegistry | ||
|
||
|
||
def get_xapi_transformer(): | ||
"""Allow to get the XApiTransformer class from | ||
https://github.com/openedx/event-routing-backends/blob/master/event_routing_backends/processors/xapi/transformer.py#L27 | ||
Returns: | ||
XApiTransformer class. | ||
""" | ||
return XApiTransformer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Wrapper for event-routing-backends library. | ||
This contains all the required dependencies from event-routing-backends. | ||
Attributes: | ||
constants: Wrapper constants module. | ||
XApiTransformer: Wrapper for the XApiTransformer class. | ||
XApiTransformersRegistry: Wrapper for the XApiTransformersRegistry class. | ||
""" | ||
from importlib import import_module | ||
|
||
from django.conf import settings | ||
|
||
backend = import_module(settings.EOX_NELP_EVENT_ROUTING_BACKEND) | ||
|
||
constants = backend.get_xapi_constants() | ||
XApiTransformer = backend.get_xapi_transformer() | ||
XApiTransformersRegistry = backend.get_xapi_transformer_registry() |
41 changes: 41 additions & 0 deletions
41
eox_nelp/edxapp_wrapper/test_backends/event_routing_backends_m_v1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Backend for event-routing-backends library. | ||
This is required since the library has explicit dependencies from openedx platform. | ||
https://github.com/openedx/event-routing-backends | ||
""" | ||
from mock import Mock | ||
|
||
|
||
def get_xapi_constants(): | ||
"""Test backend for the constants module. | ||
Returns: | ||
Mock class. | ||
""" | ||
constants = Mock() | ||
constants.EN = "en" | ||
constants.XAPI_VERB_INITIALIZED = "http://adlnet.gov/expapi/verbs/initialized" | ||
constants.INITIALIZED = "initialized" | ||
|
||
return constants | ||
|
||
|
||
def get_xapi_transformer_registry(): | ||
"""Test backend for the XApiTransformersRegistry class. | ||
Returns: | ||
Mock class. | ||
""" | ||
XApiTransformersRegistry = Mock() | ||
XApiTransformersRegistry.register.return_value = lambda x: x | ||
|
||
return XApiTransformersRegistry | ||
|
||
|
||
def get_xapi_transformer(): | ||
"""Test backend for the XApiTransformer class. | ||
Returns: | ||
Object type. | ||
""" | ||
return object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
76 changes: 76 additions & 0 deletions
76
eox_nelp/processors/tests/xapi/event_transformers/tests_initialized_events.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""This file contains all the test for the initialized_events.py file. | ||
Classes: | ||
InitializedCourseTransformerTestCase: Tests cases for InitializedCourseTransformer class. | ||
""" | ||
from django.test import TestCase | ||
from mock import Mock, patch | ||
from tincan import ActivityDefinition, LanguageMap | ||
|
||
from eox_nelp.edxapp_wrapper.event_routing_backends import constants | ||
from eox_nelp.processors.xapi import constants as eox_nelp_constants | ||
from eox_nelp.processors.xapi.event_transformers import InitializedCourseTransformer | ||
|
||
|
||
class InitializedCourseTransformerTestCase(TestCase): | ||
"""Test class for InitializedCourseTransformer class.""" | ||
|
||
def setUp(self): | ||
"""Setup common conditions for every test case""" | ||
self.get_data_mock = Mock() | ||
self.get_object_iri_mock = Mock() | ||
|
||
InitializedCourseTransformer.get_data = self.get_data_mock | ||
InitializedCourseTransformer.get_object_iri = self.get_object_iri_mock | ||
|
||
def test_verb_attribute(self): | ||
""" Test case that checks that the _verb attribute has the right values. | ||
Expected behavior: | ||
- Verb id is the expected value. | ||
- Verb display is the expected value. | ||
""" | ||
verb = InitializedCourseTransformer._verb # pylint: disable=protected-access | ||
|
||
self.assertEqual(constants.XAPI_VERB_INITIALIZED, verb.id) | ||
self.assertEqual(LanguageMap({constants.EN: constants.INITIALIZED}), verb.display) | ||
|
||
@patch("eox_nelp.processors.xapi.event_transformers.initialized_events.get_course_from_id") | ||
def test_expected_result(self, get_course_mock): | ||
""" Test case that verifies that the get_object method returns the expected Activity | ||
when all the course attributes are found. | ||
Expected behavior: | ||
- get_data method is called with the right value. | ||
- get_object_iri method is called with the right value. | ||
- get_course_from_id method is called with the right value. | ||
- Activity id is the expected value. | ||
- Activity definition is the expected value. | ||
""" | ||
course_id = "course-v1:edx+CS105+2023-T3" | ||
self.get_data_mock.return_value = course_id | ||
object_id = f"http://exemple.com/course/{course_id}" | ||
self.get_object_iri_mock.return_value = object_id | ||
course = { | ||
"display_name": "great-course", | ||
"language": "fr", | ||
"short_description": "This is the best course", | ||
} | ||
get_course_mock.return_value = course | ||
transformer = InitializedCourseTransformer() | ||
|
||
activity = transformer.get_object() | ||
|
||
self.get_data_mock.assert_called_once_with("data.course_id", True) | ||
self.get_object_iri_mock.assert_called_once_with("course", course_id) | ||
get_course_mock.assert_called_once_with(course_id) | ||
self.assertEqual(object_id, activity.id) | ||
self.assertEqual( | ||
ActivityDefinition( | ||
type=eox_nelp_constants.XAPI_ACTIVITY_COURSE, | ||
name=LanguageMap({course["language"]: course["display_name"]}), | ||
description=LanguageMap({course["language"]: course["short_description"]}), | ||
), | ||
activity.definition, | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
Constants for xAPI specifications, this contains the NELC required values. | ||
""" | ||
DEFAULT_LANGUAGE = "en" | ||
|
||
XAPI_ACTIVITY_COURSE = "https://w3id.org/xapi/cmi5/activitytype/course" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
All xAPI transformers. | ||
""" | ||
from eox_nelp.processors.xapi.event_transformers.initialized_events import InitializedCourseTransformer # noqa: F401 |
47 changes: 47 additions & 0 deletions
47
eox_nelp/processors/xapi/event_transformers/initialized_events.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Transformers for initialized events. | ||
Classes: | ||
InitializedCourseTransformer: Transformer for the event nelc.eox_nelp.initialized.course | ||
""" | ||
|
||
from tincan import Activity, ActivityDefinition, LanguageMap, Verb | ||
|
||
from eox_nelp.edxapp_wrapper.event_routing_backends import XApiTransformer, XApiTransformersRegistry, constants | ||
from eox_nelp.processors.xapi import constants as eox_nelp_constants | ||
from eox_nelp.utils import get_course_from_id | ||
|
||
|
||
@XApiTransformersRegistry.register("nelc.eox_nelp.initialized.course") | ||
class InitializedCourseTransformer(XApiTransformer): | ||
""" | ||
Transformers for event generated when an student start a course. | ||
""" | ||
_verb = Verb( | ||
id=constants.XAPI_VERB_INITIALIZED, | ||
display=LanguageMap({constants.EN: constants.INITIALIZED}), | ||
) | ||
|
||
def get_object(self): | ||
""" | ||
Get object for xAPI transformed event. | ||
Returns: | ||
`Activity` | ||
""" | ||
course_id = self.get_data('data.course_id', True) | ||
object_id = self.get_object_iri('course', course_id) | ||
course = get_course_from_id(course_id) | ||
display_name = course["display_name"] | ||
description = course["short_description"] | ||
# Set default value if language is not found | ||
course_language = course["language"] or eox_nelp_constants.DEFAULT_LANGUAGE | ||
|
||
return Activity( | ||
id=object_id, | ||
definition=ActivityDefinition( | ||
type=eox_nelp_constants.XAPI_ACTIVITY_COURSE, | ||
name=LanguageMap(**({course_language: display_name} if display_name is not None else {})), | ||
description=LanguageMap(**({course_language: description} if description is not None else {})) | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters