code / tests /test_cleaning_scope.py
anonymous
[code] Reproduction bundle.
2e511b5
Raw
History Blame Contribute Delete
3.97 kB
"""Model-scoped scan/apply: other models' files and changelog history stay untouched."""
import json
import openpyxl
import pytest
from openpyxl import Workbook
import legex.evaluation.cleaning as cleaning
from legex.config import settings
from legex.evaluation.comparison import is_label_column
def test_inference_date_is_not_a_label():
assert is_label_column("inference_date") is False
@pytest.fixture
def env(tmp_path, monkeypatch):
monkeypatch.setattr(settings, "data_dir", tmp_path)
monkeypatch.setattr(cleaning, "CHANGELOG", tmp_path / "changelog.jsonl")
(tmp_path / "zz").mkdir()
gs = Workbook()
ws = gs.active
ws.title = "GOLDENSET"
ws.append(["case_id", "link", "full_text", "legal_subject_judgement", "plaintiffs_all_count"])
ws.append(["C-1", "", "text", "Contract_Law", "1"])
gs.save(tmp_path / "zz" / "Goldenset_Testland.xlsx")
def record(model, subject):
return {"case_id": "C-1", "legal_subject_judgement": subject,
"plaintiffs_all_count": "1", "model": model, "error": None}
files = {}
for model, subject in (("modx", "Contract_Law"), ("mody", "Tort_Law")):
p = tmp_path / "zz" / f"Goldenset_Testland_v3_full_text_{model}.jsonl"
p.write_text(json.dumps(record(model, subject)) + "\n", encoding="utf-8")
files[model] = p
return tmp_path, files
def test_inference_files_model_filter(env):
tmp_path, files = env
assert cleaning._inference_files() == sorted(files.values())
assert cleaning._inference_files({"modx"}) == [files["modx"]]
def test_scan_folds_in_conflicts_with_prefill(env):
tmp_path, files = env
conflicts = tmp_path / "conflicts.jsonl"
conflicts.write_text(
json.dumps({"model": "modx", "country": "zz", "case_id": "C-1",
"field": "legal_subject_judgement", "kept": "Contract_Law",
"alternative": "Property_Law"}) + "\n"
+ json.dumps({"model": "mody", "country": "zz", "case_id": "C-1",
"field": "legal_subject_judgement", "kept": "Tort_Law",
"alternative": "IP_Law"}) + "\n",
encoding="utf-8",
)
out = tmp_path / "review.xlsx"
n = cleaning.scan(out, models={"modx"}, conflicts=conflicts)
ws = openpyxl.load_workbook(out).active
rows = list(ws.iter_rows(values_only=True))
header, data = rows[0], rows[1:]
assert n == len(data) == 1
row = dict(zip(header, data[0]))
assert row["model"] == "modx"
assert row["current_value"] == "Contract_Law"
assert row["corrected_value"] == "Contract_Law"
assert "Property_Law" in row["reason"]
def test_apply_scoped_leaves_other_models_alone(env):
tmp_path, files = env
before_y = files["mody"].read_bytes()
cleaning.CHANGELOG.write_text(
json.dumps({"model": "mody", "country": "zz", "case_id": "C-1",
"field": "plaintiffs_all_count", "before": "one", "after": "1",
"kind": "reviewed"}) + "\n",
encoding="utf-8",
)
xlsx = tmp_path / "review.xlsx"
wb = Workbook()
ws = wb.active
ws.append(cleaning._XLSX_HEADER)
ws.append(["modx", "zz", "C-1", "legal_subject_judgement", "Contract_Law",
"duplicate-run conflict; alternative: 'Property_Law'", "", "tp", "missed",
"Property_Law"])
wb.save(xlsx)
cleaning.apply(xlsx, models={"modx"})
assert files["mody"].read_bytes() == before_y
recs = [json.loads(l) for l in files["modx"].read_text(encoding="utf-8").splitlines()]
assert recs[0]["legal_subject_judgement"] == "Property_Law"
assert json.loads(recs[0]["original_input"]) == {"legal_subject_judgement": "Contract_Law"}
log_rows = [json.loads(l) for l in cleaning.CHANGELOG.read_text(encoding="utf-8").splitlines()]
assert {(r["model"], r["field"]) for r in log_rows} == {
("modx", "legal_subject_judgement"), ("mody", "plaintiffs_all_count"),
}