"""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 #hash" store.set(key, "safe") assert store.get(key) == "safe"