1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
"""Tests for schema utilities."""
from __future__ import annotations
import json
from pathlib import Path
from typing import TYPE_CHECKING, Any
import pytest
from ansible_compat.schema import JsonSchemaError, json_path, validate
if TYPE_CHECKING:
from ansible_compat.types import JSON
expected_results = [
JsonSchemaError(
message="False is not of type 'string'",
data_path="environment.a",
json_path="$.environment.a",
schema_path="properties.environment.additionalProperties.type",
relative_schema='{"type": "string"}',
expected="string",
validator="type",
found="False",
),
JsonSchemaError(
message="True is not of type 'string'",
data_path="environment.b",
json_path="$.environment.b",
schema_path="properties.environment.additionalProperties.type",
relative_schema='{"type": "string"}',
expected="string",
validator="type",
found="True",
),
]
def json_from_asset(file_name: str) -> JSON:
"""Load a json file from disk."""
file = Path(__file__).parent / file_name
with file.open(encoding="utf-8") as f:
return json.load(f) # type: ignore[no-any-return]
def jsonify(data: Any) -> JSON: # noqa: ANN401
"""Convert object in JSON data structure."""
return json.loads(json.dumps(data, default=vars, sort_keys=True)) # type: ignore[no-any-return]
@pytest.mark.parametrize("index", range(1))
def test_schema(index: int) -> None:
"""Test the schema validator."""
schema = json_from_asset(f"assets/validate{index}_schema.json")
data = json_from_asset(f"assets/validate{index}_data.json")
expected = json_from_asset(f"assets/validate{index}_expected.json")
# ensure we produce consistent results between runs
for _ in range(1, 100):
found_errors = validate(schema=schema, data=data)
# ensure returned results are already sorted, as we assume our class
# knows how to sort itself
assert sorted(found_errors) == found_errors, "multiple errors not sorted"
found_errors_json = jsonify(found_errors)
assert (
found_errors_json == expected
), f"inconsistent returns: {found_errors_json}"
def test_json_path() -> None:
"""Test json_path function."""
assert json_path(["a", 1, "b"]) == "$.a[1].b"
|