|
| 1 | +"""Tests for AQL — the Assertion Query Language.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from wup.aql import AQLEngine, AQLError, CheckAQL, parse_rule, register_aql |
| 11 | +from wup.bus import EventBus |
| 12 | + |
| 13 | + |
| 14 | +def _sample(tmp_path: Path) -> Path: |
| 15 | + f = tmp_path / "sample.json" |
| 16 | + f.write_text(json.dumps({ |
| 17 | + "name": "subactor", |
| 18 | + "version": "1.2.0", |
| 19 | + "services": ["a", "b"], |
| 20 | + "testql": {"probe_interval_s": 60}, |
| 21 | + "flag": True, |
| 22 | + "empty": None, |
| 23 | + })) |
| 24 | + return f |
| 25 | + |
| 26 | + |
| 27 | +def _check(tmp_path: Path, rule: str) -> bool: |
| 28 | + """True when the rule passes (no violations).""" |
| 29 | + return not AQLEngine(tmp_path).check_file(_sample(tmp_path), [rule]) |
| 30 | + |
| 31 | + |
| 32 | +# --- parsing -------------------------------------------------------------- |
| 33 | + |
| 34 | +def test_parse_exists() -> None: |
| 35 | + r = parse_rule("json .version exists") |
| 36 | + assert (r.selector, r.path, r.op) == ("json", ".version", "exists") |
| 37 | + |
| 38 | + |
| 39 | +def test_parse_length_and_severity() -> None: |
| 40 | + r = parse_rule("json .services length > 0 severity high") |
| 41 | + assert (r.op, r.length_op, r.value, r.severity) == ("length", ">", "0", "high") |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("bad", [ |
| 45 | + "", "widget .x exists", "json", "json .x", "json .x bogus", |
| 46 | + "json .x length foo", "json .x type banana", "json .x = ", |
| 47 | + "json .x severity nope", |
| 48 | +]) |
| 49 | +def test_parse_errors(bad: str) -> None: |
| 50 | + with pytest.raises(AQLError): |
| 51 | + parse_rule(bad) |
| 52 | + |
| 53 | + |
| 54 | +# --- evaluation ----------------------------------------------------------- |
| 55 | + |
| 56 | +@pytest.mark.parametrize("rule", [ |
| 57 | + "json .version exists", |
| 58 | + "json .missing missing", |
| 59 | + "json .services length > 0", |
| 60 | + "json .services length = 2", |
| 61 | + "json .testql.probe_interval_s >= 60", |
| 62 | + "json .name = subactor", |
| 63 | + "json .name ~ sub", |
| 64 | + "json .name !~ zzz", |
| 65 | + "json .version matches ^\\d+\\.\\d+", |
| 66 | + "json .services type array", |
| 67 | + "json .testql type object", |
| 68 | + "json .flag type bool", |
| 69 | + "json .name type string", |
| 70 | + "json .testql.probe_interval_s type number", |
| 71 | + "keys .testql ~ probe_interval_s", |
| 72 | + "keys .testql length = 1", |
| 73 | + "text ~ subactor", |
| 74 | +]) |
| 75 | +def test_passing_rules(tmp_path: Path, rule: str) -> None: |
| 76 | + assert _check(tmp_path, rule) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("rule", [ |
| 80 | + "json .missing exists", |
| 81 | + "json .version missing", |
| 82 | + "json .services length > 5", |
| 83 | + "json .name = other", |
| 84 | + "json .name ~ zzz", |
| 85 | + "json .version matches ^zzz", |
| 86 | + "json .services type object", |
| 87 | + "json .missing type string", |
| 88 | + "keys .testql ~ nope", |
| 89 | + "text ~ nonexistent-token", |
| 90 | +]) |
| 91 | +def test_failing_rules(tmp_path: Path, rule: str) -> None: |
| 92 | + assert not _check(tmp_path, rule) |
| 93 | + |
| 94 | + |
| 95 | +def test_violation_carries_severity(tmp_path: Path) -> None: |
| 96 | + v = AQLEngine(tmp_path).check_file(_sample(tmp_path), ["json .missing exists severity critical"]) |
| 97 | + assert len(v) == 1 and v[0].severity == "critical" and v[0].detector == "aql" |
| 98 | + |
| 99 | + |
| 100 | +def test_nested_and_indexed_paths(tmp_path: Path) -> None: |
| 101 | + assert _check(tmp_path, "json .services[0] = a") |
| 102 | + assert _check(tmp_path, "json .services[1] = b") |
| 103 | + |
| 104 | + |
| 105 | +def test_missing_file(tmp_path: Path) -> None: |
| 106 | + v = AQLEngine(tmp_path).check_file(tmp_path / "nope.json", ["json .x exists"]) |
| 107 | + assert len(v) == 1 and v[0].anomaly_type == "error" |
| 108 | + |
| 109 | + |
| 110 | +def test_yaml_file(tmp_path: Path) -> None: |
| 111 | + y = tmp_path / "wup.yaml" |
| 112 | + y.write_text("project:\n name: demo\ntestql:\n probe_interval_s: 0\n") |
| 113 | + assert not AQLEngine(tmp_path).check_file(y, ["yaml .project.name = demo", "yaml .testql.probe_interval_s >= 0"]) |
| 114 | + |
| 115 | + |
| 116 | +def test_bus_integration(tmp_path: Path) -> None: |
| 117 | + bus = EventBus() |
| 118 | + register_aql(bus, tmp_path) |
| 119 | + result = bus.query(CheckAQL(file=str(_sample(tmp_path)), rules=["json .version exists"])) |
| 120 | + assert result == [] # no violations |
0 commit comments