Download expansion_v2/experiments/contextual-discourse/source/test_runtime_validation.py from Giuliocc/doxa-methods: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/datasets/Giuliocc/doxa-methods/resolve/main/expansion_v2/experiments/contextual-discourse/source/test_runtime_validation.py
- Command line
-
hf download hf://datasets/Giuliocc/doxa-methods/expansion_v2/experiments/contextual-discourse/source/test_runtime_validation.py
-
curl -L -o test_runtime_validation.py https://huggingface.co/datasets/Giuliocc/doxa-methods/resolve/main/expansion_v2/experiments/contextual-discourse/source/test_runtime_validation.py
3.2 kB
| """Direct-helper regressions: malformed requests never become char context.""" | |
| import unittest | |
| from contracts import sha256 | |
| from inference import DiscourseModel | |
| class RuntimeInputValidation(unittest.TestCase): | |
| def setUp(self): | |
| # A boundary-only engine has no tokenizer/session. Any accidental | |
| # inference during an invalid request therefore fails this test. | |
| self.engine = object.__new__(DiscourseModel) | |
| self.engine.audit = False | |
| self.engine.manifest = {"method": "doxa-contextual-discourse-v0.2.0", "model_id": "authored-boundary-test", | |
| "revision": "0" * 40, "publication_status": "qualified_release", "scope": "Source contract only"} | |
| def assert_invalid(self, **changes): | |
| arguments = {"target": "A source sentence.", "before": ["A preceding source sentence."], "after": [], **changes} | |
| result = self.engine.check(**arguments) | |
| self.assertEqual(result["prediction_status"], "invalid_input") | |
| self.assertTrue(all(row["model_score"] is None and row["suggested_decision"] is None for row in result["results"])) | |
| def test_neighbor_strings_none_and_nonstring_members_are_invalid(self): | |
| for value in ("a source string is not an array", None, [None], [12], {"text": "a source"}): | |
| with self.subTest(value=value): | |
| self.assert_invalid(before=value) | |
| self.assert_invalid(after=value) | |
| def test_invalid_or_duplicate_task_arrays_are_invalid(self): | |
| for value in ("BASIL_lexical_bias", None, [], [None], [""], ["BASIL_lexical_bias", "BASIL_lexical_bias"], ["BASIL_lexical_bias", {}]): | |
| with self.subTest(value=value): | |
| self.assert_invalid(tasks=value) | |
| def test_anchor_integer_types_are_strict(self): | |
| text = "A source sentence." | |
| anchor = {"document_id": "source", "start": 0, "end": len(text), "offset_unit": "unicode_codepoint", "sha256": sha256(text)} | |
| for value in (False, 0., "0", -1): | |
| self.assert_invalid(source_anchor={**anchor, "start": value}) | |
| for value in (True, float(len(text)), str(len(text)), 0): | |
| self.assert_invalid(source_anchor={**anchor, "end": value}) | |
| for value in ([], {}, "source", {**anchor, "document_id": 1}, {**anchor, "sha256": "not a hash"}): | |
| self.assert_invalid(source_anchor=value) | |
| def test_malformed_unicode_and_domain_metadata_are_invalid(self): | |
| self.assert_invalid(target="A malformed \ud800 source") | |
| self.assert_invalid(before=["A malformed \udc00 context"]) | |
| self.assert_invalid(applied_rubric=7) | |
| self.assert_invalid(language=None) | |
| self.assert_invalid(genre=[]) | |
| def test_whitespace_context_is_missing_and_unknown_tasks_stay_unsupported(self): | |
| result = self.engine.check("A source sentence.", [" \n"], []) | |
| self.assertTrue(all(row["prediction_status"] == "insufficient_context" for row in result["results"])) | |
| result = self.engine.check("A source sentence.", [], [], tasks=["comparative_double_standard"]) | |
| self.assertEqual(result["results"][0]["prediction_status"], "unsupported_criterion") | |
| if __name__ == "__main__": | |
| unittest.main() | |