|
| 1 | +"""Tests for the xAPI JSON-LD Profile.""" |
| 2 | +import json |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pydantic import ValidationError |
| 6 | + |
| 7 | +from ralph.models.selector import ModelSelector |
| 8 | +from ralph.models.xapi.profile import Profile |
| 9 | + |
| 10 | +from tests.fixtures.hypothesis_strategies import custom_given |
| 11 | + |
| 12 | + |
| 13 | +@custom_given(Profile) |
| 14 | +def test_models_xapi_profile_with_json_ld_keywords(profile): |
| 15 | + """Test a Profile MAY include JSON-LD keywords.""" |
| 16 | + profile = json.loads(profile.json(by_alias=True)) |
| 17 | + profile["@base"] = None |
| 18 | + try: |
| 19 | + Profile(**profile) |
| 20 | + except ValidationError as err: |
| 21 | + pytest.fail( |
| 22 | + f"A profile including JSON-LD keywords should not raise exceptions: {err}" |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +@custom_given(Profile) |
| 27 | +def test_models_xapi_profile_selector_with_valid_model(profile): |
| 28 | + """Test given a valid profile, the `get_first_model` method of the model |
| 29 | + selector should return the corresponding model. |
| 30 | + """ |
| 31 | + profile = json.loads(profile.json()) |
| 32 | + model_selector = ModelSelector(module="ralph.models.xapi.profile") |
| 33 | + assert model_selector.get_first_model(profile) is Profile |
| 34 | + |
| 35 | + |
| 36 | +@custom_given(Profile) |
| 37 | +def test_models_xapi_profile_with_valid_json_schema(profile): |
| 38 | + """Test given a profile with an extension concept containing a valid JSONSchema, |
| 39 | + should not raise exceptions. |
| 40 | + """ |
| 41 | + profile = json.loads(profile.json(by_alias=True)) |
| 42 | + profile["concepts"] = [ |
| 43 | + { |
| 44 | + "id": "http://example.com", |
| 45 | + "type": "ContextExtension", |
| 46 | + "inScheme": "http://example.profile.com", |
| 47 | + "prefLabel": { |
| 48 | + "en-us": "Example context extension", |
| 49 | + }, |
| 50 | + "definition": { |
| 51 | + "en-us": "To use when an example happens", |
| 52 | + }, |
| 53 | + "inlineSchema": json.dumps( |
| 54 | + { |
| 55 | + "$id": "https://example.com/example.schema.json", |
| 56 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 57 | + "title": "Example", |
| 58 | + "type": "object", |
| 59 | + "properties": { |
| 60 | + "example": {"type": "string", "description": "The example."}, |
| 61 | + }, |
| 62 | + } |
| 63 | + ), |
| 64 | + } |
| 65 | + ] |
| 66 | + try: |
| 67 | + Profile(**profile) |
| 68 | + except ValidationError as err: |
| 69 | + pytest.fail( |
| 70 | + f"A profile including a valid JSONSchema should not raise exceptions: {err}" |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +@custom_given(Profile) |
| 75 | +def test_models_xapi_profile_with_invalid_json_schema(profile): |
| 76 | + """Test given a profile with an extension concept containing an invalid JSONSchema, |
| 77 | + should raise an exception. |
| 78 | + """ |
| 79 | + profile = json.loads(profile.json(by_alias=True)) |
| 80 | + profile["concepts"] = [ |
| 81 | + { |
| 82 | + "id": "http://example.com", |
| 83 | + "type": "ContextExtension", |
| 84 | + "inScheme": "http://example.profile.com", |
| 85 | + "prefLabel": { |
| 86 | + "en-us": "Example context extension", |
| 87 | + }, |
| 88 | + "definition": { |
| 89 | + "en-us": "To use when an example happens", |
| 90 | + }, |
| 91 | + "inlineSchema": json.dumps({"type": "example"}), |
| 92 | + } |
| 93 | + ] |
| 94 | + msg = "Invalid JSONSchema: 'example' is not valid under any of the given schemas" |
| 95 | + with pytest.raises(ValidationError, match=msg): |
| 96 | + Profile(**profile) |
0 commit comments