91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from dpg_map.cache import (
|
|
CacheStats,
|
|
DiskCacheConfig,
|
|
DiskCacheMetadata,
|
|
MemoryCacheConfig,
|
|
clear_disk_cache_path,
|
|
disk_cache_size_bytes,
|
|
plan_disk_prune,
|
|
tile_cache_path,
|
|
write_disk_metadata,
|
|
)
|
|
|
|
|
|
def test_cache_stats_dataclass_construction() -> None:
|
|
stats = CacheStats(
|
|
memory_tiles=3,
|
|
memory_max_tiles=512,
|
|
memory_hits=10,
|
|
memory_misses=2,
|
|
disk_bytes=1024,
|
|
disk_max_bytes=None,
|
|
disk_hits=7,
|
|
disk_misses=1,
|
|
disk_path=Path("/tmp/dpg-map-cache"),
|
|
)
|
|
|
|
assert stats.memory_tiles == 3
|
|
assert stats.disk_max_bytes is None
|
|
assert stats.disk_path == Path("/tmp/dpg-map-cache")
|
|
|
|
|
|
def test_initial_cache_config_dataclasses() -> None:
|
|
memory_config = MemoryCacheConfig()
|
|
disk_config = DiskCacheConfig()
|
|
|
|
assert memory_config.max_tiles == 512
|
|
assert disk_config.max_bytes == 2_000_000_000
|
|
|
|
|
|
def test_disk_cache_path_generation(tmp_path: Path) -> None:
|
|
path = tile_cache_path(tmp_path, "osm", 4, 8, 9, "jpg")
|
|
|
|
assert path == tmp_path / "osm" / "4" / "8" / "9.jpg"
|
|
|
|
|
|
def test_disk_cache_prune_ordering(tmp_path: Path) -> None:
|
|
first = tile_cache_path(tmp_path, "osm", 1, 1, 1)
|
|
second = tile_cache_path(tmp_path, "osm", 1, 1, 2)
|
|
protected = tile_cache_path(tmp_path, "osm", 1, 1, 3)
|
|
|
|
for path, accessed_at in [(first, 1.0), (second, 2.0), (protected, 0.0)]:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b"abcde")
|
|
write_disk_metadata(
|
|
path.with_suffix(".json"),
|
|
DiskCacheMetadata(
|
|
url=str(path),
|
|
downloaded_at=accessed_at,
|
|
last_accessed_at=accessed_at,
|
|
size_bytes=5,
|
|
),
|
|
)
|
|
|
|
planned = plan_disk_prune(tmp_path, 5, protected_paths={protected})
|
|
|
|
assert planned == [first, second]
|
|
|
|
|
|
def test_provider_scoped_disk_cache_clear(tmp_path: Path) -> None:
|
|
osm = tile_cache_path(tmp_path, "osm", 1, 1, 1)
|
|
custom = tile_cache_path(tmp_path, "custom", 1, 1, 1)
|
|
for path in (osm, custom):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b"abcde")
|
|
write_disk_metadata(
|
|
path.with_suffix(".json"),
|
|
DiskCacheMetadata(url=str(path), last_accessed_at=1.0, size_bytes=5),
|
|
)
|
|
|
|
assert disk_cache_size_bytes(tmp_path, provider="osm") == 5
|
|
|
|
clear_disk_cache_path(tmp_path, provider="osm")
|
|
|
|
assert not osm.exists()
|
|
assert custom.exists()
|
|
assert disk_cache_size_bytes(tmp_path) == 5
|