pyrtan / sourcev0.14.0 · main

pyrtan / tests / test_conditions.py

2,060 bytes · last changed 2026-10-25 in Initial public release

1import pytest
2
3from pyrtan.conditions import ConditionError, holds, validate
4from pyrtan.template import MISSING
5
6
7@pytest.mark.parametrize(
8 "cond,value,expected",
9 [
10 ({"eq": 200}, 200, True),
11 ({"eq": 200}, "200", True),
12 ({"eq": "story"}, "story", True),
13 ({"eq": True}, 1, False),
14 ({"ne": 200}, 404, True),
15 ({"gte": 500}, 503, True),
16 ({"gte": 500}, 499, False),
17 ({"lt": 10}, "9", True),
18 ({"between": [200, 299]}, 204, True),
19 ({"between": [200, 299]}, 300, False),
20 ({"matches": "^https://"}, "https://x", True),
21 ({"matches": "^https://"}, "http://x", False),
22 ({"contains": "err"}, "an error", True),
23 ({"contains": 2}, [1, 2], True),
24 ({"contains": "k"}, {"k": 1}, True),
25 ({"in": [403, 429]}, 429, True),
26 ({"in": ["a", "b"]}, "c", False),
27 ({"exists": True}, 0, True),
28 ({"exists": True}, None, False),
29 ({"exists": False}, MISSING, True),
30 ({"empty": True}, [], True),
31 ({"empty": True}, "", True),
32 ({"empty": True}, MISSING, True),
33 ({"empty": False}, [1], True),
34 ({"type": "list"}, [], True),
35 ({"type": "number"}, 4.2, True),
36 ({"type": "number"}, True, False),
37 ({"type": "int"}, "3", False),
38 ({"startswith": "ab"}, "abc", True),
39 ({"endswith": "bc"}, "abc", True),
40 ({"gte": 1, "lte": 5}, 3, True),
41 ({"gte": 1, "lte": 5}, 6, False),
42 ({"eq": 1}, MISSING, False),
43 ],
44)
45def test_holds(cond, value, expected):
46 assert holds(cond, value) is expected
47
48
49def test_validate_rejects_unknown_and_bad():
50 with pytest.raises(ConditionError):
51 validate({"equals": 1})
52 with pytest.raises(ConditionError):
53 validate({})
54 with pytest.raises(ConditionError):
55 validate({"matches": "("})
56 with pytest.raises(ConditionError):
57 validate({"between": [1]})
58 with pytest.raises(ConditionError):
59 validate({"type": "thing"})
60 validate({"eq": 1, "type": "int"})