|
| 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, ProfilePattern, ProfileTemplateRule |
| 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 | +@pytest.mark.parametrize( |
| 27 | + "missing", |
| 28 | + [ |
| 29 | + ("prefLabel",), ("definition", ), ("prefLabel", "definition") |
| 30 | + ] |
| 31 | +) |
| 32 | +@custom_given(ProfilePattern) |
| 33 | +def test_models_xapi_profile_pattern_with_invalid_primary_value(missing, pattern): |
| 34 | + """Test a `ProfilePattern` MUST include `prefLabel` and `definition` fields.""" |
| 35 | + pattern = json.loads(pattern.json(by_alias=True)) |
| 36 | + pattern["primary"] = True |
| 37 | + for field in missing: |
| 38 | + del pattern[field] |
| 39 | + |
| 40 | + msg = "A `primary` pattern MUST include `prefLabel` and `definition` fields" |
| 41 | + with pytest.raises(ValidationError, match=msg): |
| 42 | + ProfilePattern(**pattern) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "rules", |
| 47 | + [ |
| 48 | + (), |
| 49 | + ("alternates", "optional"), |
| 50 | + ("oneOrMore", "sequence"), |
| 51 | + ("zeroOrMore", "alternates") |
| 52 | + ] |
| 53 | +) |
| 54 | +@custom_given(ProfilePattern) |
| 55 | +def test_models_xapi_profile_pattern_with_invalid_number_of_match_rules( |
| 56 | + rules, pattern |
| 57 | +): |
| 58 | + """Test a `ProfilePattern` MUST contain exactly one of `alternates`, `optional`, |
| 59 | + `oneOrMore`, `sequence`, and `zeroOrMore`. |
| 60 | + """ |
| 61 | + rule_values = { |
| 62 | + "alternates": ["https://example.com", "https://example.fr"], |
| 63 | + "optional": "https://example.com", |
| 64 | + "oneOrMore": "https://example.com", |
| 65 | + "sequence": ["https://example.com", "https://example.fr"], |
| 66 | + "zeroOrMore": "https://example.com" |
| 67 | + } |
| 68 | + pattern = json.loads(pattern.json(by_alias=True)) |
| 69 | + del pattern["optional"] |
| 70 | + for rule in rules: |
| 71 | + pattern[rule] = rule_values[rule] |
| 72 | + |
| 73 | + msg = ( |
| 74 | + "A pattern MUST contain exactly one of `alternates`, `optional`, " |
| 75 | + "`oneOrMore`, `sequence`, and `zeroOrMore` fields" |
| 76 | + ) |
| 77 | + with pytest.raises(ValidationError, match=msg): |
| 78 | + ProfilePattern(**pattern) |
| 79 | + |
| 80 | + |
| 81 | +@custom_given(Profile) |
| 82 | +def test_models_xapi_profile_selector_with_valid_model(profile): |
| 83 | + """Test given a valid profile, the `get_first_model` method of the model |
| 84 | + selector should return the corresponding model. |
| 85 | + """ |
| 86 | + profile = json.loads(profile.json()) |
| 87 | + model_selector = ModelSelector(module="ralph.models.xapi.profile") |
| 88 | + assert model_selector.get_first_model(profile) is Profile |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize("field", ["location", "selector"]) |
| 92 | +@custom_given(ProfileTemplateRule) |
| 93 | +def test_models_xapi_profile_template_rules_with_invalid_json_path(field, rule): |
| 94 | + """Test given a profile template rule with a `location` or `selector` containing an |
| 95 | + invalid JSONPath, the `ProfileTemplateRule` model should raise an exception. |
| 96 | + """ |
| 97 | + rule = json.loads(rule.json()) |
| 98 | + rule[field] = "" |
| 99 | + msg = "Invalid JSONPath: empty string is not a valid path" |
| 100 | + with pytest.raises(ValidationError, match=msg): |
| 101 | + ProfileTemplateRule(**rule) |
| 102 | + |
| 103 | + rule[field] = "not a JSONPath" |
| 104 | + msg = ( |
| 105 | + f"1 validation error for ProfileTemplateRule\n{field}\n Invalid JSONPath: " |
| 106 | + r"Parse error at 1:4 near token a \(ID\) \(type=value_error\)" |
| 107 | + ) |
| 108 | + with pytest.raises(ValidationError, match=msg): |
| 109 | + ProfileTemplateRule(**rule) |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("field", ["location", "selector"]) |
| 113 | +@custom_given(ProfileTemplateRule) |
| 114 | +def test_models_xapi_profile_template_rules_with_valid_json_path(field, rule): |
| 115 | + """Test given a profile template rule with a `location` or `selector` containing an |
| 116 | + valid JSONPath, the `ProfileTemplateRule` model should not raise exceptions. |
| 117 | + """ |
| 118 | + rule = json.loads(rule.json()) |
| 119 | + rule[field] = "$.context.extensions['http://example.com/extension']" |
| 120 | + try: |
| 121 | + ProfileTemplateRule(**rule) |
| 122 | + except ValidationError as err: |
| 123 | + pytest.fail( |
| 124 | + "A `ProfileTemplateRule` with a valid JSONPath should not raise exceptions:" |
| 125 | + f" {err}" |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +@custom_given(Profile) |
| 130 | +def test_models_xapi_profile_with_valid_json_schema(profile): |
| 131 | + """Test given a profile with an extension concept containing a valid JSONSchema, |
| 132 | + should not raise exceptions. |
| 133 | + """ |
| 134 | + profile = json.loads(profile.json(by_alias=True)) |
| 135 | + profile["concepts"] = [ |
| 136 | + { |
| 137 | + "id": "http://example.com", |
| 138 | + "type": "ContextExtension", |
| 139 | + "inScheme": "http://example.profile.com", |
| 140 | + "prefLabel": { |
| 141 | + "en-us": "Example context extension", |
| 142 | + }, |
| 143 | + "definition": { |
| 144 | + "en-us": "To use when an example happens", |
| 145 | + }, |
| 146 | + "inlineSchema": json.dumps( |
| 147 | + { |
| 148 | + "$id": "https://example.com/example.schema.json", |
| 149 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 150 | + "title": "Example", |
| 151 | + "type": "object", |
| 152 | + "properties": { |
| 153 | + "example": {"type": "string", "description": "The example."}, |
| 154 | + }, |
| 155 | + } |
| 156 | + ), |
| 157 | + } |
| 158 | + ] |
| 159 | + try: |
| 160 | + Profile(**profile) |
| 161 | + except ValidationError as err: |
| 162 | + pytest.fail( |
| 163 | + f"A profile including a valid JSONSchema should not raise exceptions: {err}" |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +@custom_given(Profile) |
| 168 | +def test_models_xapi_profile_with_invalid_json_schema(profile): |
| 169 | + """Test given a profile with an extension concept containing an invalid JSONSchema, |
| 170 | + should raise an exception. |
| 171 | + """ |
| 172 | + profile = json.loads(profile.json(by_alias=True)) |
| 173 | + profile["concepts"] = [ |
| 174 | + { |
| 175 | + "id": "http://example.com", |
| 176 | + "type": "ContextExtension", |
| 177 | + "inScheme": "http://example.profile.com", |
| 178 | + "prefLabel": { |
| 179 | + "en-us": "Example context extension", |
| 180 | + }, |
| 181 | + "definition": { |
| 182 | + "en-us": "To use when an example happens", |
| 183 | + }, |
| 184 | + "inlineSchema": json.dumps({"type": "example"}), |
| 185 | + } |
| 186 | + ] |
| 187 | + msg = "Invalid JSONSchema: 'example' is not valid under any of the given schemas" |
| 188 | + with pytest.raises(ValidationError, match=msg): |
| 189 | + Profile(**profile) |
0 commit comments