File size: 1,874 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
"""The shared file helpers: where things go, and that files round-trip unchanged."""

from __future__ import annotations

import os
import tempfile
import unittest
from pathlib import Path
from unittest import mock

from layatasks import common


class HomeTests(unittest.TestCase):
    def test_each_task_gets_its_own_folder_under_the_home(self):
        with mock.patch.dict(os.environ, {"LAYA_TASKS_HOME": "/tmp/somewhere"}):
            self.assertEqual(common.data_dir("clinc150"), Path("/tmp/somewhere/clinc150/data/v1"))
            self.assertEqual(common.run_dir("ledgar", "seed0"), Path("/tmp/somewhere/ledgar/runs/seed0"))


class FileTests(unittest.TestCase):
    def test_jsonl_round_trip_keeps_non_ascii_text(self):
        rows = [{"state": "café déjà vu", "n": 1}, {"state": "plain", "n": 2}]
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "x.jsonl"
            common.write_jsonl(path, rows)
            self.assertEqual(common.read_jsonl(path), rows)
            self.assertFalse(path.with_suffix(".jsonl.tmp").exists())

    def test_json_round_trip(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "x.json"
            common.write_json(path, {"a": [1, 2]})
            self.assertEqual(common.read_json(path), {"a": [1, 2]})

    def test_sha256_of_known_bytes(self):
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "abc"
            path.write_bytes(b"abc")
            self.assertEqual(common.sha256_file(path),
                             "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")


class NormaliseTests(unittest.TestCase):
    def test_case_and_spacing_are_ignored(self):
        self.assertEqual(common.normalise("  Where IS\tmy card? "), "where is my card?")


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