5a324f6554
Infra: cache_to_file, cache_to_sqlite, http_download_file, http_get_json, http_post_json, read_file_with_encoding, safe_extract_zip, scan_directory, setup_logger, normalize_zip_filenames. Tipos: 30+ tipos core (agent_action, context, task, message, parse_result...), 6 tipos datascience (entity_candidate, extraction_result...), 2 tipos infra. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
"""Tests para cache_to_file."""
|
|
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from .cache_to_file import cache_to_file
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp_path):
|
|
return cache_to_file(str(tmp_path))
|
|
|
|
|
|
def test_set_y_get_basico(store):
|
|
store.set("hello", {"x": 42})
|
|
assert store.get("hello") == {"x": 42}
|
|
|
|
|
|
def test_ttl_expirado_retorna_none(store):
|
|
store.set("temp", "val", ttl=0.05)
|
|
time.sleep(0.1)
|
|
assert store.get("temp") is None
|
|
|
|
|
|
def test_archivo_meta_con_metadata_correcta(tmp_path):
|
|
s = cache_to_file(str(tmp_path), "ns")
|
|
s.set("mykey", "myval", ttl=60)
|
|
ns_dir = os.path.join(str(tmp_path), "ns")
|
|
meta_files = [f for f in os.listdir(ns_dir) if f.endswith(".meta")]
|
|
assert len(meta_files) == 1
|
|
with open(os.path.join(ns_dir, meta_files[0])) as f:
|
|
meta = json.load(f)
|
|
assert meta["original_key"] == "mykey"
|
|
assert meta["expires_at"] is not None
|
|
assert meta["created_at"] > 0
|
|
|
|
|
|
def test_clear_elimina_directorio_del_namespace(tmp_path):
|
|
s = cache_to_file(str(tmp_path), "mynamespace")
|
|
s.set("a", 1)
|
|
s.set("b", 2)
|
|
removed = s.clear()
|
|
assert removed == 2
|
|
assert s.get("a") is None
|
|
assert s.get("b") is None
|
|
|
|
|
|
def test_key_con_caracteres_especiales_hash_seguro(store):
|
|
key = "https://example.com/path?q=1&r=2 <special>#hash"
|
|
store.set(key, "safe")
|
|
assert store.get(key) == "safe"
|