Skip to content

Commit

Permalink
✨(models) add xAPI Profile model
Browse files Browse the repository at this point in the history
We want to support xapi profile validation in Ralph.
Therefore we implement the xAPI Profile model which should
follow the xAPI profiles structures specification.
  • Loading branch information
SergioSim committed Aug 18, 2023
1 parent 2331f40 commit dc207cf
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to

## [Unreleased]

### Added

- Implement xAPI JSON-LD profile validation
(CLI command: `ralph validate -f xapi.profile`)

### Changed

- Helm chart: improve chart modularity
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ include_package_data = True
install_requires =
; By default, we only consider core dependencies required to use Ralph as a
; library (mostly models).
jsonschema>=4.0.0, <5.0 # Note: v4.18.0 dropped support for python 3.7.
langcodes>=3.2.0
pydantic[dotenv,email]>=1.10.0, <2.0
rfc3987>=1.3.0
Expand Down
6 changes: 3 additions & 3 deletions src/ralph/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ def extract(parser):
"-f",
"--format",
"format_",
type=click.Choice(["edx", "xapi"]),
type=click.Choice(["edx", "xapi", "xapi.profile"]),
required=True,
help="Input events format to validate",
help="Input data format to validate",
)
@click.option(
"-I",
Expand All @@ -462,7 +462,7 @@ def extract(parser):
"--fail-on-unknown",
default=False,
is_flag=True,
help="Stop validating at first unknown event",
help="Stop validating at first unknown record",
)
def validate(format_, ignore_errors, fail_on_unknown):
"""Validate input events of given format."""
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/models/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _validate_event(self, event_str: str):
event_str (str): The cleaned JSON-formatted input event_str.
"""
event = json.loads(event_str)
return self.get_first_valid_model(event).json()
return self.get_first_valid_model(event).json(by_alias=True)

@staticmethod
def _log_error(message, event_str, error=None):
Expand Down
427 changes: 427 additions & 0 deletions src/ralph/models/xapi/profile.py

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions tests/models/xapi/test_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Tests for the xAPI JSON-LD Profile."""
import json

import pytest
from pydantic import ValidationError

from ralph.models.selector import ModelSelector
from ralph.models.xapi.profile import Profile

from tests.fixtures.hypothesis_strategies import custom_given


@custom_given(Profile)
def test_models_xapi_profile_with_json_ld_keywords(profile):
"""Test a Profile MAY include JSON-LD keywords."""
profile = json.loads(profile.json(by_alias=True))
profile["@base"] = None
try:
Profile(**profile)
except ValidationError as err:
pytest.fail(
f"A profile including JSON-LD keywords should not raise exceptions: {err}"
)


@custom_given(Profile)
def test_models_xapi_profile_selector_with_valid_model(profile):
"""Test given a valid profile, the `get_first_model` method of the model
selector should return the corresponding model.
"""
profile = json.loads(profile.json())
model_selector = ModelSelector(module="ralph.models.xapi.profile")
assert model_selector.get_first_model(profile) is Profile


@custom_given(Profile)
def test_models_xapi_profile_with_valid_json_schema(profile):
"""Test given a profile with an extension concept containing a valid JSONSchema,
should not raise exceptions.
"""
profile = json.loads(profile.json(by_alias=True))
profile["concepts"] = [
{
"id": "http://example.com",
"type": "ContextExtension",
"inScheme": "http://example.profile.com",
"prefLabel": {
"en-us": "Example context extension",
},
"definition": {
"en-us": "To use when an example happens",
},
"inlineSchema": json.dumps(
{
"$id": "https://example.com/example.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Example",
"type": "object",
"properties": {
"example": {"type": "string", "description": "The example."},
},
}
),
}
]
try:
Profile(**profile)
except ValidationError as err:
pytest.fail(
f"A profile including a valid JSONSchema should not raise exceptions: {err}"
)


@custom_given(Profile)
def test_models_xapi_profile_with_invalid_json_schema(profile):
"""Test given a profile with an extension concept containing an invalid JSONSchema,
should raise an exception.
"""
profile = json.loads(profile.json(by_alias=True))
profile["concepts"] = [
{
"id": "http://example.com",
"type": "ContextExtension",
"inScheme": "http://example.profile.com",
"prefLabel": {
"en-us": "Example context extension",
},
"definition": {
"en-us": "To use when an example happens",
},
"inlineSchema": json.dumps({"type": "example"}),
}
]
msg = "Invalid JSONSchema: 'example' is not valid under any of the given schemas"
with pytest.raises(ValidationError, match=msg):
Profile(**profile)
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ralph.exceptions import ConfigurationException
from ralph.models.edx.navigational.statements import UIPageClose
from ralph.models.xapi.navigation.statements import PageTerminated
from ralph.models.xapi.profile import Profile

from tests.fixtures.backends import (
ES_TEST_HOSTS,
Expand Down Expand Up @@ -482,6 +483,16 @@ def test_cli_validate_command_with_edx_format(event):
assert event_str in result.output


@custom_given(Profile)
def test_cli_validate_command_with_xapi_profile_format(event):
"""Test the validate command using the xAPI profile format."""

event_str = event.json(by_alias=True)
runner = CliRunner()
result = runner.invoke(cli, "validate -f xapi.profile".split(), input=event_str)
assert event_str in result.output


@hypothesis_settings(deadline=None)
@custom_given(UIPageClose)
@pytest.mark.parametrize("valid_uuid", ["ee241f8b-174f-5bdb-bae9-c09de5fe017f"])
Expand Down
13 changes: 9 additions & 4 deletions tests/test_cli_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ def test_cli_validate_command_usage():
assert result.exit_code == 0
assert (
"Options:\n"
" -f, --format [edx|xapi] Input events format to validate [required]\n"
" -I, --ignore-errors Continue validating regardless of raised errors\n"
" -F, --fail-on-unknown Stop validating at first unknown event\n"
" -f, --format [edx|xapi|xapi.profile]\n"
" Input data format to validate [required]\n"
" -I, --ignore-errors Continue validating regardless of raised\n"
" errors\n"
" -F, --fail-on-unknown Stop validating at first unknown record\n"
) in result.output

result = runner.invoke(cli, ["validate"])
assert result.exit_code > 0
assert (
"Error: Missing option '-f' / '--format'. Choose from:\n\tedx,\n\txapi\n"
"Error: Missing option '-f' / '--format'. Choose from:\n"
"\tedx,\n"
"\txapi,\n"
"\txapi.profile\n"
) in result.output


Expand Down

0 comments on commit dc207cf

Please sign in to comment.