File size: 3,401 Bytes
779ff0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
"""How source records become Laya rows, and the test lock. No download: the inputs here are made up."""

from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from layatasks import data
from layatasks.task import TASKS, Task, load

NAMES = ["card_arrival", "Refund_not_showing_up", "oos"]


def made_up(test_sha256=None) -> Task:
    return Task(name="made-up", title="Made up", repo="someone/data", revision="0" * 40, license="CC0",
                attribution="nobody", files={}, text_column="text", label_column="label", question_id="q",
                instructions="Which one?", option_text=lambda n: n.replace("_", " ").lower(),
                test_sha256=test_sha256)


class QuestionTests(unittest.TestCase):
    def test_one_question_with_every_option_in_label_order(self):
        rows = made_up().rows([{"text": "Where is my card?", "label": 0}], NAMES)
        q = rows[0]["questions"]["q"]
        self.assertEqual(q, {"type": "choice", "instructions": "Which one?",
                             "criteria": ["card arrival", "refund not showing up", "oos"]})
        self.assertEqual(rows[0]["state"], "Where is my card?")
        self.assertEqual(rows[0]["expected"], {"q": "card arrival"})

    def test_labels_that_read_the_same_are_refused(self):
        with self.assertRaises(ValueError):
            made_up().question(["card_arrival", "Card_arrival"])


class OverlapTests(unittest.TestCase):
    def test_counts_copies_ignoring_case_and_spacing(self):
        self.assertEqual(data.overlap(["Where is  my card?", "hi"], ["where is my card?", "bye"]), 1)


class CountTests(unittest.TestCase):
    def test_every_label_is_listed_even_when_absent(self):
        counts = data.label_counts([{"label": 0}, {"label": 0}, {"label": 2}], NAMES, "label")
        self.assertEqual(counts, {"card_arrival": 2, "Refund_not_showing_up": 0, "oos": 1})


class LockTests(unittest.TestCase):
    def test_a_changed_test_file_is_refused(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "test.jsonl"
            path.write_text("not the locked test\n", encoding="utf-8")
            with self.assertRaises(RuntimeError):
                data.check_test_lock(made_up("0" * 64), path)

    def test_an_unlocked_task_reports_the_hash(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "test.jsonl"
            path.write_bytes(b"abc")
            self.assertEqual(data.check_test_lock(made_up(None), path),
                             "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")


class RegistryTests(unittest.TestCase):
    def test_every_task_module_is_pinned_and_locked(self):
        for name in TASKS:
            task = load(name).TASK
            self.assertEqual(task.name, name)
            self.assertRegex(task.revision, r"^[0-9a-f]{40}$")
            self.assertEqual(set(task.files), {"train", "test"})
            for _path, sha in task.files.values():
                self.assertRegex(sha, r"^[0-9a-f]{64}$")
            self.assertRegex(task.test_sha256 or "", r"^[0-9a-f]{64}$", "%s has no locked test" % name)
            self.assertEqual(task.release_name, "laya-" + name)

    def test_unknown_task_is_refused(self):
        with self.assertRaises(ValueError):
            load("nope")


if __name__ == "__main__":
    unittest.main()