File size: 4,587 Bytes
a490f0e | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | from __future__ import annotations
import hashlib
import json
import sys
import tempfile
import unittest
from pathlib import Path
WORKFLOWS = Path(__file__).resolve().parents[1] / "workflows"
sys.path.insert(0, str(WORKFLOWS))
from preprocess_text import run_preprocessing # noqa: E402
from validate_manifest import validate_manifest # noqa: E402
def approved_manifest(content: bytes) -> dict[str, object]:
return {
"schema_version": "1.0",
"dataset_id": "synthetic-test@1",
"decision": {
"status": "approved",
"reviewed_by": "test-reviewer",
"reviewed_at": "2026-10-11T00:00:00Z",
"notes": ["Synthetic unit-test fixture only."],
},
"sources": [
{
"source_id": "synthetic-one",
"title": "Synthetic unit-test text",
"publisher": "DATA LAB tests",
"canonical_url": "https://example.invalid/synthetic-one",
"version": "1",
"retrieved_at": "2026-10-11T00:00:00Z",
"content_path": "input.txt",
"sha256": hashlib.sha256(content).hexdigest(),
"license": {
"identifier": "CC0-1.0",
"url": "https://creativecommons.org/publicdomain/zero/1.0/",
"permission_status": "approved",
"redistribution": "allowed",
"attribution": "",
},
"privacy": {
"review_status": "passed",
"contains_personal_data": False,
"consent_or_lawful_basis": "Synthetic text created by the test.",
"notes": [],
},
"use": {
"purpose": "Workflow unit test",
"allowed": True,
"restrictions": [],
},
}
],
}
class ManifestTests(unittest.TestCase):
def test_approved_manifest_is_valid(self) -> None:
self.assertEqual(validate_manifest(approved_manifest(b"hello\n")), [])
def test_traversal_path_is_rejected(self) -> None:
manifest = approved_manifest(b"hello\n")
manifest["sources"][0]["content_path"] = "../outside.txt" # type: ignore[index]
self.assertTrue(any("safe" in error for error in validate_manifest(manifest)))
def test_approval_requires_privacy_gate(self) -> None:
manifest = approved_manifest(b"hello\n")
manifest["sources"][0]["privacy"]["review_status"] = "pending" # type: ignore[index]
self.assertTrue(any("privacy" in error for error in validate_manifest(manifest)))
class PreprocessingTests(unittest.TestCase):
def test_output_is_deterministic_and_contains_no_absolute_path(self) -> None:
content = "Synthetic text. \r\n\r\n\r\nSecond line.\r\n".encode()
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
input_root = root / "input"
input_root.mkdir()
(input_root / "input.txt").write_bytes(content)
manifest_path = root / "manifest.json"
manifest_path.write_text(json.dumps(approved_manifest(content)), encoding="utf-8")
report_a = run_preprocessing(manifest_path, input_root, root / "out-a")
report_b = run_preprocessing(manifest_path, input_root, root / "out-b")
records_a = (root / "out-a" / "records.jsonl").read_bytes()
records_b = (root / "out-b" / "records.jsonl").read_bytes()
self.assertEqual(records_a, records_b)
self.assertEqual(report_a, report_b)
self.assertNotIn(str(root).encode(), records_a)
self.assertEqual(report_a["counts"]["accepted_records"], 1)
def test_proposed_manifest_cannot_run(self) -> None:
content = b"synthetic\n"
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
input_root = root / "input"
input_root.mkdir()
(input_root / "input.txt").write_bytes(content)
manifest = approved_manifest(content)
manifest["decision"]["status"] = "proposed" # type: ignore[index]
manifest_path = root / "manifest.json"
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
with self.assertRaisesRegex(ValueError, "approved"):
run_preprocessing(manifest_path, input_root, root / "out")
if __name__ == "__main__":
unittest.main()
|