88 lines
3.4 KiB
Python
88 lines
3.4 KiB
Python
"""Tests for cover cache previews and pruning."""
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
|
|
import covers
|
|
|
|
|
|
class PruneTests(unittest.TestCase):
|
|
def test_prunes_oldest_files_until_limit(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
paths = []
|
|
for index, size in enumerate((10, 10, 10)):
|
|
path = os.path.join(tmp, f"{index}.jpg")
|
|
with open(path, "wb") as fh:
|
|
fh.write(b"x" * size)
|
|
os.utime(path, (100 + index, 100 + index))
|
|
paths.append(path)
|
|
marker = os.path.join(tmp, ".missing")
|
|
with open(marker, "w", encoding="utf-8") as fh:
|
|
fh.write("0")
|
|
|
|
result = covers.prune_cache(tmp, 15)
|
|
|
|
self.assertEqual(result["removed"], 2)
|
|
self.assertEqual(result["freed"], 20)
|
|
self.assertEqual(result["bytes"], 10)
|
|
self.assertTrue(os.path.isfile(paths[2]))
|
|
self.assertFalse(os.path.isfile(paths[1]))
|
|
self.assertTrue(os.path.isfile(marker))
|
|
|
|
def test_cleanup_negative_markers(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
old = os.path.join(tmp, "1", ".missing")
|
|
new = os.path.join(tmp, "2", ".missing")
|
|
os.makedirs(os.path.dirname(old))
|
|
os.makedirs(os.path.dirname(new))
|
|
with open(old, "w", encoding="utf-8") as fh:
|
|
fh.write("0")
|
|
with open(new, "w", encoding="utf-8") as fh:
|
|
fh.write("0")
|
|
os.utime(old, (100, 100))
|
|
self.assertEqual(covers.cleanup_negative_markers(tmp, 3600), 1)
|
|
self.assertFalse(os.path.isfile(old))
|
|
self.assertTrue(os.path.isfile(new))
|
|
|
|
def test_stats(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with open(os.path.join(tmp, "a.jpg"), "wb") as fh:
|
|
fh.write(b"1234")
|
|
with open(os.path.join(tmp, covers.PREVIEW_NAME), "wb") as fh:
|
|
fh.write(b"12")
|
|
stats = covers.cache_stats(tmp)
|
|
self.assertEqual(stats, {"files": 2, "previews": 1, "bytes": 6})
|
|
self.assertEqual(covers.prune_cache(os.path.join(tmp, "nope"), 1)["removed"], 0)
|
|
|
|
|
|
class PreviewTests(unittest.TestCase):
|
|
@unittest.skipUnless(covers.can_optimize(), "Pillow is not installed")
|
|
def test_make_preview_resizes(self):
|
|
from PIL import Image
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "cover.png")
|
|
dst = os.path.join(tmp, "preview.webp")
|
|
Image.new("RGB", (800, 1200), (10, 20, 30)).save(src)
|
|
self.assertTrue(covers.make_preview(src, dst, max_width=360))
|
|
with Image.open(dst) as preview:
|
|
self.assertEqual(preview.width, 360)
|
|
self.assertEqual(preview.height, 540)
|
|
self.assertEqual(preview.format, "WEBP")
|
|
|
|
def test_invalid_source(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "bad.jpg")
|
|
dst = os.path.join(tmp, "preview.webp")
|
|
with open(src, "wb") as fh:
|
|
fh.write(b"not an image")
|
|
self.assertFalse(covers.make_preview(src, dst))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|