| from pathlib import Path |
|
|
| import pytest |
| from openpyxl import Workbook, load_workbook |
|
|
| from legex.models.base import Case |
| from legex.utils import ( |
| classified_csv_path, |
| model_filename_slug, |
| random_sample, |
| write_goldenset_xlsx, |
| ) |
|
|
|
|
| def _case(case_id: str = "x", link: str = "http://x", full_text: str | None = None) -> Case: |
| return Case(case_id=case_id, link=link, full_text=full_text, jurisdiction="ch") |
|
|
|
|
| def test_model_filename_slug() -> None: |
| assert model_filename_slug("gemini/gemini-3.1-flash-lite") == "gemini_gemini-3.1-flash-lite" |
|
|
|
|
| def test_classified_csv_path_includes_model_slug() -> None: |
| pred = classified_csv_path("de", "v1", "full_text", "gemini/gemini-3.1-flash-lite") |
| assert pred.name == "Goldenset_Deutschland_v1_full_text_gemini_gemini-3.1-flash-lite.csv" |
|
|
|
|
| def test_random_sample_is_deterministic_and_sized() -> None: |
| cases = [_case(case_id=f"c{i}") for i in range(100)] |
| a = random_sample(cases, n=10, seed=42) |
| b = random_sample(cases, n=10, seed=42) |
| assert [c.case_id for c in a] == [c.case_id for c in b] |
| assert len(random_sample(cases, n=130, seed=0)) == 100 |
|
|
|
|
| def test_write_goldenset_xlsx_fills_a_b_c_preserves_rest(tmp_path: Path) -> None: |
| template = tmp_path / "Vorlage.xlsx" |
| output = tmp_path / "out.xlsx" |
| wb = Workbook() |
| wb.remove(wb.active) |
| gs = wb.create_sheet("GOLDENSET") |
| gs.append(["case_id", "link", "full_text", "extra"]) |
| for _ in range(5): |
| gs.append([None, None, None, None]) |
| wb.create_sheet("OTHER")["A1"] = "preserved" |
| wb.save(template) |
|
|
| cases = [ |
| _case(case_id=f"4A_{i}/2024", link=f"http://b/{i}", full_text=f"body {i}") |
| for i in range(3) |
| ] |
| write_goldenset_xlsx(cases, template, output) |
|
|
| out = load_workbook(output) |
| assert [out["GOLDENSET"][f"A{r}"].value for r in (2, 3, 4)] == ["4A_0/2024", "4A_1/2024", "4A_2/2024"] |
| assert out["GOLDENSET"]["B2"].value == "http://b/0" |
| assert out["GOLDENSET"]["C2"].value == "body 0" |
| assert out["GOLDENSET"]["D2"].value is None |
| assert out["GOLDENSET"]["A5"].value is None |
| assert out["OTHER"]["A1"].value == "preserved" |
|
|
|
|
| def test_write_goldenset_xlsx_missing_sheet_raises(tmp_path: Path) -> None: |
| template, output = tmp_path / "bad.xlsx", tmp_path / "out.xlsx" |
| wb = Workbook() |
| wb.active.title = "NOT_GOLDENSET" |
| wb.save(template) |
| with pytest.raises(ValueError, match="GOLDENSET"): |
| write_goldenset_xlsx([], template, output) |
|
|