Datasets:
Download scripts/test_workqueue_counts.py from laion/academic-textbook-links: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/datasets/laion/academic-textbook-links/resolve/main/scripts/test_workqueue_counts.py
- Command line
-
hf download hf://datasets/laion/academic-textbook-links/scripts/test_workqueue_counts.py
-
curl -L -o test_workqueue_counts.py https://huggingface.co/datasets/laion/academic-textbook-links/resolve/main/scripts/test_workqueue_counts.py
10.6 kB
| """Regression checks for OpenAlex workqueue counts. | |
| Run from the repository root with: | |
| python3 -m unittest scripts.test_workqueue_counts | |
| """ | |
| from __future__ import annotations | |
| import csv | |
| import json | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| import workqueue_counts # noqa: E402 | |
| ROOT = Path(__file__).resolve().parents[1] | |
| COUNT_FIELDS = workqueue_counts.QUEUE_FIELDS | |
| CATALOG_FIELDS = [ | |
| "record_id", "work_id", "collection", "group", "title", "authors", | |
| "year", "work_type", "pages", "pdf_url", "source_url", "identifier", | |
| "status", "checked_at", "sha256", "verification_note", "source_notes", | |
| ] | |
| def _write_jsonl(path: Path, rows: list[dict]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") | |
| def _record(record_id: str, collection: str, digest: str, **changes) -> dict: | |
| row = dict.fromkeys(CATALOG_FIELDS, "") | |
| row.update({ | |
| "record_id": record_id, | |
| "work_id": "sha256:" + digest, | |
| "collection": collection, | |
| "group": "test", | |
| "title": record_id, | |
| "year": "2020", | |
| "work_type": "scholarly_book", | |
| "pages": "100", | |
| "pdf_url": "https://example.org/book.pdf", | |
| "source_url": "https://example.org/book", | |
| "status": "downloaded_and_screened", | |
| "checked_at": "2026-10-12T00:00:00Z", | |
| "sha256": digest, | |
| }) | |
| row.update(changes) | |
| return row | |
| class WorkqueueCountTests(unittest.TestCase): | |
| def test_real_export_has_one_row_per_openalex_subfield(self): | |
| with tempfile.TemporaryDirectory(prefix="workqueue-counts-real-") as tmp: | |
| # Rendering writes two planning outputs; copy only authoritative inputs. | |
| root = Path(tmp) | |
| for rel in ( | |
| "planning/subfield_jobs.jsonl", | |
| "planning/collection_subfield_map.json", | |
| "taxonomy/openalex_subfields.csv", | |
| "collections.json", | |
| "data/verified_links.jsonl", | |
| "data/legacy_candidates.jsonl", | |
| "planning/workqueue_state.json", | |
| ): | |
| source = ROOT / rel | |
| if source.exists(): | |
| destination = root / rel | |
| destination.parent.mkdir(parents=True, exist_ok=True) | |
| destination.write_bytes(source.read_bytes()) | |
| for source in (ROOT / "planning").glob("*search_progress.jsonl"): | |
| destination = root / "planning" / source.name | |
| destination.parent.mkdir(parents=True, exist_ok=True) | |
| destination.write_bytes(source.read_bytes()) | |
| summary = workqueue_counts.render(root, now="2026-10-12T12:00:00Z") | |
| with (root / "planning/openalex_workqueue.csv").open(newline="", encoding="utf-8") as f: | |
| rows = list(csv.DictReader(f)) | |
| self.assertEqual(list(rows[0]), COUNT_FIELDS) | |
| self.assertEqual(len(rows), 252) | |
| self.assertEqual(len({row["subfield_id"] for row in rows}), 252) | |
| self.assertTrue(all(all(isinstance(v, str) for v in row.values()) for row in rows)) | |
| self.assertEqual(summary["global_verified_entries"], 3530) | |
| self.assertEqual(summary["global_distinct_pdfs"], 3171) | |
| self.assertEqual(summary["unassigned_verified_entries"], 277) | |
| self.assertEqual(summary["zero_pdf_subfields"], 184) | |
| self.assertEqual(summary["subfield_pdf_assignments"], 3209) | |
| self.assertEqual(workqueue_counts.validate(root), summary) | |
| canonical = [ | |
| json.loads(line) | |
| for line in (ROOT / "data/verified_links.jsonl").read_text(encoding="utf-8").splitlines() | |
| if line.strip() | |
| ] | |
| self.assertEqual(len(canonical), 3530) | |
| for record in canonical: | |
| self.assertEqual(set(record), set(CATALOG_FIELDS)) | |
| self.assertTrue(all(isinstance(record[k], str) for k in CATALOG_FIELDS if k not in {"year", "pages"})) | |
| self.assertTrue(record["year"] is None or type(record["year"]) is int) | |
| self.assertTrue(type(record["pages"]) is int and record["pages"] > 0) | |
| self.assertEqual(record["status"], "downloaded_and_screened") | |
| self.assertRegex(record["sha256"], r"^[0-9a-f]{64}$") | |
| with (ROOT / "data/verified_links.csv").open(newline="", encoding="utf-8") as f: | |
| canonical_csv = list(csv.DictReader(f)) | |
| self.assertEqual(len(canonical_csv), 3530) | |
| self.assertEqual(list(canonical_csv[0]), CATALOG_FIELDS) | |
| self.assertTrue(all(all(isinstance(v, str) for v in r.values()) for r in canonical_csv)) | |
| def test_hash_dedup_assignment_and_unmapped_collection(self): | |
| with tempfile.TemporaryDirectory(prefix="workqueue-counts-fixture-") as tmp: | |
| root = Path(tmp) | |
| jobs = [ | |
| { | |
| "subfield_id": sid, "subfield_name": name, "domain_id": "1", | |
| "domain_name": "Test", "field_id": "10", "field_name": "Test Field", | |
| "topic_count": "1", "prompt_file": "test.txt", | |
| } | |
| for sid, name in (("100", "One"), ("200", "Two"), ("300", "Unmapped")) | |
| ] | |
| _write_jsonl(root / "planning/subfield_jobs.jsonl", jobs) | |
| definitions = [ | |
| {"collection": key, "group": "test", "path": "test/" + key} | |
| for key in ("a", "b", "c", "unmapped") | |
| ] | |
| (root / "collections.json").write_text(json.dumps(definitions), encoding="utf-8") | |
| taxonomy = root / "taxonomy/openalex_subfields.csv" | |
| taxonomy.parent.mkdir(parents=True) | |
| with taxonomy.open("w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=list(jobs[0])) | |
| writer.writeheader() | |
| writer.writerows(jobs) | |
| mapping = { | |
| "schema_version": 1, | |
| "mapping_basis": "editorial_collection_level_not_per_work_classification", | |
| "collections": { | |
| "a": {"subfield_ids": ["100"], "basis": "test", "rationale": "a"}, | |
| "b": {"subfield_ids": ["100"], "basis": "test", "rationale": "b"}, | |
| "c": {"subfield_ids": ["200"], "basis": "test", "rationale": "c"}, | |
| "unmapped": {"subfield_ids": [], "basis": "test", "rationale": "ambiguous"}, | |
| }, | |
| } | |
| (root / "planning/collection_subfield_map.json").parent.mkdir(parents=True, exist_ok=True) | |
| (root / "planning/collection_subfield_map.json").write_text(json.dumps(mapping), encoding="utf-8") | |
| _write_jsonl(root / "data/verified_links.jsonl", [ | |
| _record("a1", "a", "a" * 64), | |
| _record("b1", "b", "a" * 64), # same PDF in a second collection, same subfield | |
| _record("b2", "b", "b" * 64), | |
| _record("c1", "c", "a" * 64), # same PDF may count in another assigned subfield | |
| _record("u1", "unmapped", "c" * 64), | |
| _record("paper", "a", "e" * 64, work_type="research_article"), | |
| ]) | |
| _write_jsonl(root / "data/legacy_candidates.jsonl", []) | |
| (root / "planning/workqueue_state.json").write_text( | |
| json.dumps({"schema_version": 1, "tasks": {}}), encoding="utf-8" | |
| ) | |
| workqueue_counts.render(root, now="2026-10-12T12:00:00Z") | |
| with (root / "planning/openalex_workqueue.csv").open(newline="", encoding="utf-8") as f: | |
| rows = {r["subfield_id"]: r for r in csv.DictReader(f)} | |
| self.assertEqual(set(rows), {"100", "200", "300"}) | |
| self.assertEqual(rows["100"]["valid_pdf_count"], "3") | |
| self.assertEqual(rows["100"]["book_scope_pdf_count"], "2") | |
| self.assertEqual(rows["200"]["valid_pdf_count"], "1") | |
| self.assertEqual(rows["300"]["valid_pdf_count"], "0") | |
| self.assertEqual(rows["300"]["collection_ids"], "") | |
| self.assertEqual(rows["300"]["availability"], "available") | |
| summary = json.loads((root / "planning/workqueue_summary.json").read_text(encoding="utf-8")) | |
| self.assertEqual(summary["global_distinct_pdfs"], 4) | |
| self.assertEqual(summary["subfield_pdf_assignments"], 4) | |
| self.assertEqual(summary["unassigned_distinct_pdfs"], 1) | |
| self.assertEqual(summary["unassigned_collections"][0]["collection"], "unmapped") | |
| def test_invalid_verified_rows_fail_closed(self): | |
| with tempfile.TemporaryDirectory(prefix="workqueue-counts-invalid-") as tmp: | |
| root = Path(tmp) | |
| jobs = [{ | |
| "subfield_id": "100", "subfield_name": "One", "domain_id": "1", | |
| "domain_name": "Test", "field_id": "10", "field_name": "Test Field", | |
| "topic_count": "1", "prompt_file": "test.txt", | |
| }] | |
| _write_jsonl(root / "planning/subfield_jobs.jsonl", jobs) | |
| (root / "collections.json").write_text(json.dumps([{"collection": "a"}]), encoding="utf-8") | |
| taxonomy = root / "taxonomy/openalex_subfields.csv" | |
| taxonomy.parent.mkdir(parents=True) | |
| with taxonomy.open("w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=list(jobs[0])) | |
| writer.writeheader() | |
| writer.writerows(jobs) | |
| (root / "planning/collection_subfield_map.json").write_text(json.dumps({ | |
| "schema_version": 1, | |
| "collections": {"a": {"subfield_ids": ["100"], "basis": "test", "rationale": "test"}}, | |
| }), encoding="utf-8") | |
| _write_jsonl(root / "data/verified_links.jsonl", [ | |
| _record("bad", "a", "f" * 64, status="legacy_not_rechecked"), | |
| ]) | |
| with self.assertRaises(ValueError): | |
| workqueue_counts.render(root) | |
| def test_public_inputs_preserve_mixed_medical_collection_as_unmapped(self): | |
| mapping = json.loads((ROOT / "planning/collection_subfield_map.json").read_text(encoding="utf-8")) | |
| self.assertEqual(mapping["mapping_basis"], "editorial_collection_level_not_per_work_classification") | |
| self.assertEqual(mapping["collections"]["medical_sciences"]["subfield_ids"], []) | |
| if __name__ == "__main__": | |
| unittest.main() | |