File size: 1,413 Bytes
910dadd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json

from app.config import DATA_DIR
from app.tasks.pr_area import AREAS, TASK, parse_output, score


def test_dataset_loaded_and_labels_valid():
    records = [json.loads(line) for line in (DATA_DIR / "prs.jsonl").open()]
    assert len(records) > 800
    assert {r["area"] for r in records} <= set(AREAS)
    numbers = [r["number"] for r in records]
    assert len(numbers) == len(set(numbers))


def test_sample_and_truth_roundtrip():
    s = TASK.sample()
    truth = TASK.lookup_truth(s.input_id)
    assert truth and truth["area"] in AREAS
    assert s.text.startswith("Title: ")
    assert TASK.lookup_truth("pr-999999999") is None


def test_parse_output_variants():
    assert parse_output('{"area": "docs"}')["area"] == "docs"
    assert parse_output('Sure!\n{"area": "Infra"}\nDone.')["area"] == "infra"
    assert parse_output('{"area": "not-a-label"}')["area"] is None
    assert parse_output("no json here")["area"] is None
    assert parse_output('{"wrong_key": "docs"}')["area"] is None


def test_score():
    truth = {"area": "docs", "pr_number": 1}
    assert score(truth, parse_output('{"area": "docs"}')) == {
        "format_ok": 1.0, "exact_match": 1.0,
    }
    assert score(truth, parse_output('{"area": "infra"}')) == {
        "format_ok": 1.0, "exact_match": 0.0,
    }
    assert score(truth, parse_output("garbage")) == {
        "format_ok": 0.0, "exact_match": 0.0,
    }