File size: 3,284 Bytes
35d4b41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""The small public helpers the pipeline leans on.

Each of these is a single function that a node or a tool calls at run time: the lexicon
lookup, the conflict line checker, the session verdict, and the arbiter process entry.
"""

import io
import json
import os
import sys
from contextlib import redirect_stdout

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)

import unittest  # noqa: E402

from lingologos_thinkingpython import catalog, lexicon, session  # noqa: E402

from nodes import arbiter as arbiter_mod  # noqa: E402


class Lexicon(unittest.TestCase):
    def test_a_variety_is_known_or_not(self):
        self.assertTrue(lexicon.has("en"))
        self.assertTrue(lexicon.has("zh-cn"))
        self.assertFalse(lexicon.has("qq"))

    def test_a_label_comes_from_the_lexicon_or_the_fallback(self):
        self.assertEqual(lexicon.label("en", "verified"), "Verified")
        self.assertEqual(lexicon.label("xx", "no_such_key", "kept"), "kept")


class Conflicts(unittest.TestCase):
    def test_declared_pairs_are_read_back_as_lines(self):
        entries = catalog.load_entries()
        policies = catalog.load_policies()
        claims = {"collect": ["mutable-default", "unused-local"]}
        defects, pairs = conflict_report(claims, policies)
        self.assertEqual(defects, [])
        self.assertEqual(len(pairs), 1)

    def test_an_undeclared_pair_is_a_defect(self):
        defects, pairs = conflict_report({"thing": ["alpha", "beta"]},
                                         {"alpha|beta": "specificity"})
        self.assertNotIn("no policy for pair alpha|beta on thing", defects)
        self.assertEqual(pairs, [("alpha", "beta", "specificity")])
        defects, pairs = conflict_report({"thing": ["alpha", "gamma"]},
                                         {"alpha|beta": "specificity"})
        self.assertEqual(defects, ["no policy for pair alpha|gamma on thing"])
        self.assertEqual(pairs, [])


def conflict_report(claims, policies):
    return catalog.conflict_lines(claims, policies)


class SessionVerdict(unittest.TestCase):
    def test_a_green_suite_with_an_applied_pair_is_verified(self):
        state, reason = session.verdict({"applied": True}, {"status": "GREEN"})
        self.assertEqual((state, reason), ("VERIFIED", ""))

    def test_a_red_suite_never_reads_as_verified(self):
        state, reason = session.verdict({"applied": True}, {"status": "RED"})
        self.assertNotEqual(state, "VERIFIED")
        self.assertTrue(reason)


class ArbiterProcess(unittest.TestCase):
    def test_the_arbiter_prints_its_record_on_stdout(self):
        envelope = {"nodes": [{"node": "gate_checker", "status": "pass"},
                              {"node": "adversary", "status": "survived"}]}
        stdin, buffer = sys.stdin, io.StringIO()
        sys.stdin = io.StringIO(json.dumps(envelope))
        try:
            with redirect_stdout(buffer):
                code = arbiter_mod.main()
        finally:
            sys.stdin = stdin
        record = json.loads(buffer.getvalue().strip())
        self.assertEqual(code, 0)
        self.assertEqual(record["verdict"], "VERIFIED")
        self.assertEqual(record["assurance"], 3)


if __name__ == "__main__":
    unittest.main()