Download tests/test_package_apis.py from tailot/lingologos-thinkingpython: direct link, hf CLI and curl.
- Browser
- Download file 3.28 kB
-
https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/tests/test_package_apis.py
- Command line
-
hf download hf://datasets/tailot/lingologos-thinkingpython/tests/test_package_apis.py
-
curl -L -o test_package_apis.py https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/tests/test_package_apis.py
3.28 kB
| """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() | |