File size: 2,156 Bytes
4e09985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""How Banking77 rows become Laya rows. No download: the inputs here are made up."""

from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from banking77 import data

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


class OptionTextTests(unittest.TestCase):
    def test_label_names_read_as_words(self):
        self.assertEqual(data.option_text("card_arrival"), "card arrival")
        self.assertEqual(data.option_text("Refund_not_showing_up"), "refund not showing up")

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


class RowTests(unittest.TestCase):
    def test_one_question_with_every_option_in_label_order(self):
        rows = data.rows([{"text": "Where is my card?", "label": 2}], NAMES)
        q = rows[0]["questions"][data.QUESTION_ID]
        self.assertEqual(q["type"], "choice")
        self.assertEqual(q["criteria"], ["activate my card", "refund not showing up", "card arrival"])
        self.assertEqual(rows[0]["state"], "Where is my card?")
        self.assertEqual(rows[0]["expected"][data.QUESTION_ID], "card arrival")

    def test_jsonl_round_trip(self):
        rows = data.rows([{"text": "Refund?", "label": 1}, {"text": "Activate it", "label": 0}], NAMES)
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "x.jsonl"
            data.write_jsonl(path, rows)
            self.assertEqual(data.read_jsonl(path), rows)


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 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(path)


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