import importlib.util import sys from pathlib import Path from openpyxl import Workbook # The converter is a standalone script; it lives at submission/ in the working # repo and at the bundle root in the published code repo. _ROOT = Path(__file__).resolve().parents[1] _SCRIPT = next( p for p in (_ROOT / "submission" / "convert_goldenset_to_jsonl.py", _ROOT / "convert_goldenset_to_jsonl.py") if p.exists() ) _spec = importlib.util.spec_from_file_location("convert_goldenset_to_jsonl", _SCRIPT) converter = importlib.util.module_from_spec(_spec) sys.modules["convert_goldenset_to_jsonl"] = converter _spec.loader.exec_module(converter) def _workbook(rows: list[dict]) -> Path: header = ["case_id", "Link", "full_text", *converter.SCHEMA_FIELDS, "comment", "original_input"] wb = Workbook() ws = wb.active ws.title = "GOLDENSET" ws.append(header) for row in rows: ws.append([row.get(h) for h in header]) return wb def _convert(tmp_path: Path, rows: list[dict]) -> list[dict]: path = tmp_path / "Goldenset_Test_final.xlsx" _workbook(rows).save(path) return converter.convert_workbook(path, fallback={}) def test_scrape_only_rows_are_dropped(tmp_path: Path) -> None: records = _convert(tmp_path, [ {"case_id": "A1", "Link": "http://x", "full_text": "t"}, {"case_id": "A2", "full_text": "t", "legal_subject_judgement": "Contract"}, ]) assert [r["case_id"] for r in records] == ["A2"] def test_reviewed_rows_without_legal_subject_are_kept(tmp_path: Path) -> None: records = _convert(tmp_path, [ # reviewed: real values but no legal subject (the marker convention fails # here) — kept, because its cells enter the scoring denominators {"case_id": "B1", "full_text": "t", "trial_end_date": "2024-01-31", "plaintiffs_all_count": 2, "legal_subject_judgement": "None"}, # only 'None' markers: contributes no scoreable cell -> dropped, exactly # like the scoring pipeline's row-inclusion rule {"case_id": "B2", "full_text": "t", "legal_subject_judgement": "None"}, ]) assert [r["case_id"] for r in records] == ["B1"] # the 'None' marker publishes as null assert records[0]["legal_subject_judgement"] is None assert records[0]["trial_end_date"] == "2024-01-31" def test_zero_values_keep_a_row(tmp_path: Path) -> None: records = _convert(tmp_path, [ {"case_id": "C1", "full_text": "t", "plaintiff_loosing_share": 0}, ]) assert [r["case_id"] for r in records] == ["C1"] assert records[0]["plaintiff_loosing_share"] == 0 def test_currency_only_rows_are_dropped_like_scoring(tmp_path: Path) -> None: # scoring ignores Currency_* for row inclusion; the converter must match records = _convert(tmp_path, [ {"case_id": "D1", "full_text": "t", "Currency_dispute_value_nominal": "CHF"}, ]) assert records == [] def test_nonpecuniary_is_normalised_to_the_schema_literal(tmp_path: Path) -> None: records = _convert(tmp_path, [ {"case_id": "E1", "full_text": "t", "legal_subject_judgement": "Claim", "dispute_value_nominal": "Nonpecuniary"}, ]) assert records[0]["dispute_value_nominal"] == "nonpecuniary"