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