File size: 2,547 Bytes
6f5156a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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  # n >= len → all


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  # other cols untouched
    assert out["GOLDENSET"]["A5"].value is None  # unused rows untouched
    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)