| """Inference JSONL I/O: faithful round-trip and CSV-scoring equivalence.""" |
| import csv |
| import json |
|
|
| from legex.evaluation import normalise |
| from legex.utils import inference_record, read_inference_jsonl, write_inference_jsonl |
|
|
| COLUMNS = ["case_id", "link", "legal_subject_judgement", "dispute_value_nominal", |
| "trial_end_date", "plaintiffs_all_count", "model", "error"] |
|
|
| ROWS = [ |
| {"case_id": "C-1", "link": "", "legal_subject_judgement": "Civil_Law", |
| "dispute_value_nominal": "nonpecuniary", "trial_end_date": "2023-05-04", |
| "plaintiffs_all_count": "2", "model": "harvey", "error": ""}, |
| |
| {"case_id": "C-2", "link": "", "legal_subject_judgement": "", |
| "dispute_value_nominal": "None (the amount is not stated) N/A", |
| "trial_end_date": "", "plaintiffs_all_count": "", "model": "harvey", |
| "error": "श्रेणी"}, |
| ] |
|
|
|
|
| def test_empty_becomes_null_nonempty_stays_string(tmp_path): |
| rec = inference_record(ROWS[1], COLUMNS) |
| assert rec["link"] is None and rec["trial_end_date"] is None |
| assert rec["dispute_value_nominal"] == "None (the amount is not stated) N/A" |
| assert rec["error"] == "श्रेणी" |
|
|
|
|
| def test_roundtripnormalises_identically_to_csv(tmp_path): |
| """Reading JSONL and reading the equivalent CSV must yield identical scored cells.""" |
| jsonl = tmp_path / "inf.jsonl" |
| write_inference_jsonl(jsonl, ROWS, COLUMNS) |
|
|
| csv_path = tmp_path / "inf.csv" |
| with csv_path.open("w", encoding="utf-8", newline="") as f: |
| w = csv.DictWriter(f, fieldnames=COLUMNS) |
| w.writeheader() |
| w.writerows(ROWS) |
|
|
| def scored(rows): |
| return [{k: normalise(v) for k, v in r.items()} for r in rows] |
|
|
| with csv_path.open(encoding="utf-8", newline="") as f: |
| csv_rows = list(csv.DictReader(f)) |
| assert scored(read_inference_jsonl(jsonl)) == scored(csv_rows) |
|
|
|
|
| def test_jsonl_is_one_object_per_line(tmp_path): |
| p = tmp_path / "inf.jsonl" |
| write_inference_jsonl(p, ROWS, COLUMNS) |
| lines = p.read_text(encoding="utf-8").splitlines() |
| assert len(lines) == len(ROWS) |
| assert all(json.loads(line) for line in lines) |
|
|