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