code / tests /test_published.py
anonymous
[code] Reproduction bundle.
2e511b5
Raw
History Blame Contribute Delete
3.45 kB
import json
from pathlib import Path
import pytest
from legex import published
def _write_gold(tmp_path: Path, cc: str, records: list[dict]) -> Path:
path = tmp_path / cc / f"goldenset_{cc}.jsonl"
path.parent.mkdir(parents=True)
path.write_text("\n".join(json.dumps(r) for r in records) + "\n", encoding="utf-8")
return path
def _record(case_id: str, annotator_id: str, **labels) -> dict:
rec = {"case_id": case_id, "link": None, "full_text": "text"}
rec.update({f: None for f in published.SCHEMA_FIELDS})
rec.update(labels)
rec.update({"comment": None, "original_input": "{}", "annotator_id": annotator_id})
return rec
@pytest.fixture
def gold_dir(tmp_path: Path) -> Path:
_write_gold(tmp_path, "ch", [
_record("1A_1/2024", "aaaa000000", legal_subject_judgement="Rental",
plaintiff_loosing_share=0.5, plaintiffs_all_count=1),
_record("1A_2/2024", "aaaa000000", legal_subject_judgement="Contract",
court_cost_awarded_nominal=500),
# reannotations, appended after the primary rows
_record("1A_1/2024", "bbbb111111", legal_subject_judgement="Rental_Agreement",
plaintiff_loosing_share=0),
# empty reannotation row: dropped by the annotator-label loader
_record("1A_2/2024", "bbbb111111"),
])
return tmp_path
def test_label_fields_are_the_11_evaluated_fields() -> None:
assert len(published.LABEL_FIELDS) == 11
assert "Currency_dispute_value_nominal" not in published.LABEL_FIELDS
assert published.LABEL_FIELDS[0] == "legal_subject_judgement"
def test_load_gold_labels_takes_first_row_per_case(gold_dir: Path) -> None:
columns, gold = published.load_gold_labels(gold_dir, "ch")
assert columns == list(published.LABEL_FIELDS)
assert set(gold) == {"1A_1/2024", "1A_2/2024"}
# primary row wins over the appended reannotation; values are normalised strings
assert gold["1A_1/2024"]["legal_subject_judgement"] == "Rental"
assert gold["1A_1/2024"]["plaintiff_loosing_share"] == "0.5"
assert gold["1A_1/2024"]["plaintiffs_all_count"] == "1"
assert gold["1A_2/2024"]["court_cost_awarded_nominal"] == "500"
assert gold["1A_2/2024"]["trial_start_date"] == ""
def test_load_annotator_labels_roles_and_filtering(gold_dir: Path) -> None:
labels = published.load_annotator_labels(gold_dir, ["ch"])
keys = set(labels)
# first row per case = primary; reannotation keyed by its hash; the
# all-empty reannotation row is dropped
assert (published.PRIMARY, "ch", "1a_1_2024") in keys
assert (published.PRIMARY, "ch", "1a_2_2024") in keys
assert ("bbbb111111", "ch", "1a_1_2024") in keys
assert ("bbbb111111", "ch", "1a_2_2024") not in keys
assert labels[("bbbb111111", "ch", "1a_1_2024")]["plaintiff_loosing_share"] == "0"
def test_inference_file_mapping(tmp_path: Path) -> None:
assert published.inference_file(tmp_path, "ch", "harvey-2").name == "inference_harvey_2.jsonl"
assert published.inference_file(tmp_path, "ch", "gemini/gemini-3.1-flash-lite").name == \
"inference_gemini.jsonl"
assert published.inference_file(tmp_path, "ch", "legora-1").name == "inference_legora_1.jsonl"
with pytest.raises(KeyError):
published.inference_file(tmp_path, "ch", "claude-fable-5")
def test_countries_with_gold(gold_dir: Path) -> None:
assert published.countries_with_gold(gold_dir) == ["ch"]