-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
564 additions
and
8 deletions.
There are no files selected for viewing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,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) |
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