laya-ledgar / code /tests /test_common.py
VibeHash's picture
Release laya-ledgar, run ce-seed0
779ff0b verified
Raw History Blame Contribute Delete
1.87 kB
"""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()