owenrusk.dev

casebench

test cases straight onto the board.

git clone https://owenrusk.dev/casebench.git

casebench / tests/test_casefile.py -rw-r--r-- · 2406 bytes

 1 import tempfile
 2 import unittest
 3 from pathlib import Path
 4 
 5 from casebench.casefile import CaseError, load
 6 
 7 from .helpers import UMBRELLA
 8 
 9 
10 class CaseFileTest(unittest.TestCase):
11     def folder(self, case_yaml: str, files: dict[str, str] | None = None) -> Path:
12         tmp = tempfile.TemporaryDirectory()
13         self.addCleanup(tmp.cleanup)
14         root = Path(tmp.name)
15         (root / "case.yaml").write_text(case_yaml, encoding="utf-8")
16         for name, text in (files or {}).items():
17             (root / name).write_text(text, encoding="utf-8")
18         return root
19 
20     def test_example(self):
21         case = load(UMBRELLA)
22         self.assertEqual(case.id, "test-lost-umbrella")
23         self.assertEqual([item.id for item in case.items], ["ticket", "log", "umbrella"])
24         self.assertIn("No. 114", case.items[0].text)
25         (image,) = case.items[2].images
26         self.assertEqual((image.name, image.mime), ("umbrella.svg", "image/svg+xml"))
27 
28     def test_image_it_cant_serve(self):
29         root = self.folder(
30             "id: x\ntitle: X\nitems:\n  - id: a\n    title: A\n    text: a.md\n    images: [a.tiff]\n",
31             {"a.md": "a", "a.tiff": "not really"},
32         )
33         with self.assertRaisesRegex(CaseError, "can't serve a.tiff"):
34             load(root)
35 
36     def test_no_case_yaml(self):
37         tmp = tempfile.TemporaryDirectory()
38         self.addCleanup(tmp.cleanup)
39         with self.assertRaisesRegex(CaseError, "no case.yaml"):
40             load(Path(tmp.name))
41 
42     def test_missing_text_file(self):
43         root = self.folder("id: x\ntitle: X\nitems:\n  - id: a\n    title: A\n    text: a.md\n")
44         with self.assertRaisesRegex(CaseError, r"items\[0\]: no file a.md"):
45             load(root)
46 
47     def test_bad_id(self):
48         root = self.folder("id: Not An Id\ntitle: X\nitems:\n  - id: a\n    title: A\n    text: a.md\n", {"a.md": "a"})
49         with self.assertRaisesRegex(CaseError, "bad id"):
50             load(root)
51 
52     def test_item_twice(self):
53         item = "  - id: a\n    title: A\n    text: a.md\n"
54         root = self.folder("id: x\ntitle: X\nitems:\n" + item + item, {"a.md": "a"})
55         with self.assertRaisesRegex(CaseError, "a twice"):
56             load(root)
57 
58     def test_no_items(self):
59         with self.assertRaisesRegex(CaseError, "no items"):
60             load(self.folder("id: x\ntitle: X\n"))
61 
62 
63 if __name__ == "__main__":
64     unittest.main()