added
This commit is contained in:
1 parent
aca6452f20
commit
6524457c7c
65 files changed
+2381
-192
No files matched your search
@@ -0,0 +1,92 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "copy_future_map_tagged_files.py"
|
||||
SPEC = importlib.util.spec_from_file_location("copy_future_map_tagged_files", SCRIPT)
|
||||
MODULE = importlib.util.module_from_spec(SPEC)
|
||||
assert SPEC and SPEC.loader
|
||||
sys.modules[SPEC.name] = MODULE
|
||||
SPEC.loader.exec_module(MODULE)
|
||||
|
||||
|
||||
class CopyFutureMapTaggedFilesTests(unittest.TestCase):
|
||||
def test_copies_only_exact_tag_unrestricted_records_and_media(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temp_directory:
|
||||
root = Path(temp_directory)
|
||||
source_hrecords = root / "source" / "hrecords"
|
||||
source_files = root / "source" / "files"
|
||||
intake_root = root / "intake"
|
||||
source_hrecords.mkdir(parents=True)
|
||||
source_files.mkdir(parents=True)
|
||||
|
||||
matching = {
|
||||
"HubID": "1001",
|
||||
"HubTags": ["Future Map"],
|
||||
"Contacts": ["[email protected]"],
|
||||
"Image": "chart.png",
|
||||
}
|
||||
matching_text = json.dumps(matching, indent=2)
|
||||
(source_hrecords / "1001.json").write_text(matching_text, encoding="utf-8")
|
||||
(source_hrecords / "1002.json").write_text(
|
||||
json.dumps({"HubTags": ["future map"]}), encoding="utf-8"
|
||||
)
|
||||
(source_hrecords / "1003.json").write_text(
|
||||
json.dumps({"HubTags": ["Future Map"], "PublicationRestrictions": ["private"]}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
public_record = {
|
||||
"HubID": "1004",
|
||||
"HubTags": ["Future Map"],
|
||||
"PublicationRestrictions": "public",
|
||||
}
|
||||
public_text = json.dumps(public_record, indent=2)
|
||||
(source_hrecords / "1004.json").write_text(public_text, encoding="utf-8")
|
||||
(source_hrecords / "1005.json").write_text(
|
||||
json.dumps({"HubTags": ["Future Map"], "PublicationRestrictions": "internal"}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
(source_hrecords / "broken.json").write_text('{"HubTags": [', encoding="utf-8")
|
||||
(source_files / "chart.png").write_bytes(b"png")
|
||||
|
||||
intake_hrecords = intake_root / "hrecords"
|
||||
intake_files = intake_root / "files"
|
||||
intake_hrecords.mkdir(parents=True)
|
||||
intake_files.mkdir(parents=True)
|
||||
(intake_hrecords / "old.json").write_text("{}", encoding="utf-8")
|
||||
(intake_files / "old.png").write_bytes(b"old")
|
||||
|
||||
stats = MODULE.copy_tagged_files(
|
||||
source_hrecords,
|
||||
source_files,
|
||||
intake_root,
|
||||
"Future Map",
|
||||
preserve_unreadable_names={"broken.json"},
|
||||
)
|
||||
|
||||
self.assertEqual(matching_text, (intake_hrecords / "1001.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(public_text, (intake_hrecords / "1004.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(b"png", (intake_files / "chart.png").read_bytes())
|
||||
self.assertFalse((intake_hrecords / "1002.json").exists())
|
||||
self.assertFalse((intake_hrecords / "1003.json").exists())
|
||||
self.assertFalse((intake_hrecords / "1005.json").exists())
|
||||
self.assertEqual('{"HubTags": [', (intake_hrecords / "broken.json").read_text(encoding="utf-8"))
|
||||
self.assertFalse((intake_hrecords / "old.json").exists())
|
||||
self.assertFalse((intake_files / "old.png").exists())
|
||||
self.assertEqual(4, stats.matched_records)
|
||||
self.assertEqual(3, stats.copied_records)
|
||||
self.assertEqual(2, stats.restricted_records)
|
||||
self.assertEqual(1, len(stats.malformed_records))
|
||||
self.assertEqual(1, stats.preserved_unreadable_records)
|
||||
self.assertEqual(1, stats.removed_records)
|
||||
self.assertEqual(1, stats.removed_files)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -205,6 +206,28 @@ class PipelineTests(unittest.TestCase):
|
||||
signal_index = [json.loads(line) for line in (output / "signal-index.jsonl").read_text(encoding="utf-8").splitlines()]
|
||||
self.assertEqual(signal_index[0]["page_path"].split("/")[0], "signals")
|
||||
|
||||
def test_sync_preserves_last_known_good_output_for_unreadable_source(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
source = root / "source"
|
||||
output = root / "output"
|
||||
shutil.copytree(FIXTURES, source)
|
||||
sync_args = (
|
||||
"sync", "--source-hrecords", str(source / "hrecords"),
|
||||
"--source-files", str(source / "files"), "--output-root", str(output),
|
||||
)
|
||||
initial = self.run_pipeline(*sync_args)
|
||||
self.assertEqual(initial.returncode, 0, initial.stdout + initial.stderr)
|
||||
record_path = output / "hrecords" / "1001.json"
|
||||
original = record_path.read_bytes()
|
||||
|
||||
(source / "hrecords" / "1001.json").write_text('{"HubTags": [', encoding="utf-8")
|
||||
rerun = self.run_pipeline(*sync_args)
|
||||
|
||||
self.assertEqual(rerun.returncode, 0, rerun.stdout + rerun.stderr)
|
||||
self.assertIn("Skipped unreadable records: 1", rerun.stdout)
|
||||
self.assertEqual(original, record_path.read_bytes())
|
||||
|
||||
def test_apply_reasoning_and_preserve_it_on_unchanged_sync(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
output = Path(temporary)
|
||||
@@ -273,7 +296,7 @@ class PipelineTests(unittest.TestCase):
|
||||
"relevance": 0.8, "review_status": "machine-proposed"
|
||||
}],
|
||||
"confidence": "medium", "novelty": "reinforcing",
|
||||
"corpus_documents_consulted": ["critical-states"], "generated_by": "fixture-agent"
|
||||
"corpus_documents_consulted": ["critical-states", "critical-states"], "generated_by": "fixture-agent"
|
||||
}
|
||||
patch_path = output / "patch.json"
|
||||
patch_path.write_text(json.dumps(patch), encoding="utf-8")
|
||||
@@ -282,9 +305,11 @@ class PipelineTests(unittest.TestCase):
|
||||
"--patch", str(patch_path), "--output-root", str(output),
|
||||
)
|
||||
self.assertEqual(applied.returncode, 0, applied.stdout + applied.stderr)
|
||||
self.assertIn("removed 1 duplicate entry", applied.stdout)
|
||||
self.assertEqual(self.run_pipeline(*sync_args).returncode, 0)
|
||||
record = json.loads((output / "hrecords" / "1001.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(record["reasoning"]["status"], "complete")
|
||||
self.assertEqual(record["reasoning"]["corpus_documents_consulted"], ["critical-states"])
|
||||
self.assertEqual(record["reasoning"]["related_signals"][0]["signal_id"], "fm-signal-1002")
|
||||
self.assertEqual(record["source_assessment"]["status"], "sufficient")
|
||||
source_page = (output / "signals" / "fm-signal-1001.md").read_text(encoding="utf-8")
|
||||
|
||||
Reference in new issue
Block a user