This commit is contained in:
1 parent
b2ab347129
commit
adc832c042
14 files changed
+336
-24
No files matched your search
@@ -0,0 +1,72 @@
|
||||
"""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_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()
|
||||
Reference in new issue
Block a user