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