File size: 1,019 Bytes
1864088
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tempfile
import unittest
from pathlib import Path

from hf_doc_translation.cache import TranslationCache, segment_cache_key
from hf_doc_translation.config import load_config


class CacheTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.config = load_config("transformers-ja", Path(__file__).parents[1] / "configs")

    def test_key_changes_with_source(self):
        self.assertNotEqual(segment_cache_key("one", self.config), segment_cache_key("two", self.config))

    def test_json_record_round_trip(self):
        with tempfile.TemporaryDirectory() as directory:
            cache = TranslationCache(Path(directory), self.config)
            key = segment_cache_key("hello", self.config)
            record = {"source_hash": key, "model_revision": self.config.model_revision, "translated": "こんにちは", "validation": {"passed": True}}
            cache.put(key, record)
            self.assertEqual(cache.get(key), record)


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