Skip to content

Commit

Permalink
more caching tests for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dsschult committed Feb 20, 2024
1 parent 0b8120f commit 2b2d6e0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/sigal/plugins/extended_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def _save_cache(cache_path, cache):
pickle.dump(cache, cache_file)
logger.debug("Stored cache with %d entries", len(cache))
except Exception as e:
logger.warn("Could not store cache: %s", e)
logger.warning("Could not store cache: %s", e)
os.remove(cache_path)


Expand Down
87 changes: 64 additions & 23 deletions tests/test_extended_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def test_store_metadata(settings, tmpdir):
gal = Gallery(settings, ncpu=1)
extended_caching.store_metadata(gal)

cachePath = os.path.join(settings["destination"], ".metadata_cache")
cache_path = os.path.join(settings["destination"], ".metadata_cache")

assert os.path.isfile(cachePath)
assert os.path.isfile(cache_path)

with open(cachePath, "rb") as cacheFile:
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)

# test exif
Expand Down Expand Up @@ -50,25 +50,36 @@ def test_store_metadata(settings, tmpdir):
# test if file disappears
gal.albums["exifTest"].medias.append(Image("foooo.jpg", "exifTest", settings))
extended_caching.store_metadata(gal)
with open(cachePath, "rb") as cacheFile:
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)
assert "exifTest/foooo.jpg" not in cache


def test_load_metadata(settings, tmpdir):
settings["destination"] = str(tmpdir)
gal1 = Gallery(settings, ncpu=1)
gal2 = Gallery(settings, ncpu=1)
extended_caching.store_metadata(gal1)
for album in gal2.albums.values():
extended_caching.load_metadata(album)
break # only need to load one
assert gal1.metadata_cache == gal2.metadata_cache


def test_restore_cache(settings, tmpdir):
settings["destination"] = str(tmpdir)
gal1 = Gallery(settings, ncpu=1)
gal2 = Gallery(settings, ncpu=1)
extended_caching.store_metadata(gal1)
cachePath = os.path.join(settings["destination"], ".metadata_cache")
extended_caching._restore_cache(cachePath, gal2)
cache_path = os.path.join(settings["destination"], ".metadata_cache")
extended_caching._restore_cache(cache_path, gal2)
assert gal1.metadata_cache == gal2.metadata_cache

# test bad cache
with open(cachePath, "w") as f:
with open(cache_path, "w") as f:
f.write("bad pickle file")

extended_caching._restore_cache(cachePath, gal2)
extended_caching._restore_cache(cache_path, gal2)
assert gal2.metadata_cache == {}


Expand All @@ -80,15 +91,15 @@ def test_store_metadata_local(settings, tmpdir):

for album in gal.albums.values():
if album.metadata_cache:
cachePath = os.path.join(album.dst_path, ".metadata_cache")
assert os.path.isfile(cachePath)
with open(cachePath, "rb") as cacheFile:
cache_path = os.path.join(album.dst_path, ".metadata_cache")
assert os.path.isfile(cache_path)
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)

# test exif
cachePath = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
assert os.path.isfile(cachePath)
with open(cachePath, "rb") as cacheFile:
cache_path = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
assert os.path.isfile(cache_path)
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)

album = gal.albums["exifTest"]
Expand All @@ -108,9 +119,9 @@ def test_store_metadata_local(settings, tmpdir):
assert cache_img["file_metadata"] == album.medias[2].file_metadata

# test iptc and md
cachePath = os.path.join(settings["destination"], "iptcTest", ".metadata_cache")
assert os.path.isfile(cachePath)
with open(cachePath, "rb") as cacheFile:
cache_path = os.path.join(settings["destination"], "iptcTest", ".metadata_cache")
assert os.path.isfile(cache_path)
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)

album = gal.albums["iptcTest"]
Expand All @@ -126,8 +137,8 @@ def test_store_metadata_local(settings, tmpdir):
# test if file disappears
gal.albums["exifTest"].medias.append(Image("foooo.jpg", "exifTest", settings))
extended_caching.store_metadata(gal)
cachePath = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
with open(cachePath, "rb") as cacheFile:
cache_path = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
with open(cache_path, "rb") as cacheFile:
cache = pickle.load(cacheFile)
assert "foooo.jpg" not in cache

Expand All @@ -138,20 +149,34 @@ def test_restore_cache_local(settings, tmpdir):
gal1 = Gallery(settings, ncpu=1)
gal2 = Gallery(settings, ncpu=1)
extended_caching.store_metadata(gal1)
cachePath = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
extended_caching._restore_cache(cachePath, gal2.albums["exifTest"])
cache_path = os.path.join(settings["destination"], "exifTest", ".metadata_cache")
extended_caching._restore_cache(cache_path, gal2.albums["exifTest"])
assert not hasattr(gal1, "metadata_cache")
assert not hasattr(gal2, "metadata_cache")
assert gal1.albums["exifTest"].metadata_cache == gal2.albums["exifTest"].metadata_cache

# test bad cache
with open(cachePath, "w") as f:
with open(cache_path, "w") as f:
f.write("bad pickle file")

extended_caching._restore_cache(cachePath, gal2.albums["exifTest"])
extended_caching._restore_cache(cache_path, gal2.albums["exifTest"])
assert gal2.albums["exifTest"].metadata_cache == {}


def test_load_metadata_local(settings, tmpdir):
settings["destination"] = str(tmpdir)
settings['extended_caching_options'] = {'global_cache': False}
gal1 = Gallery(settings, ncpu=1)
gal2 = Gallery(settings, ncpu=1)
extended_caching.store_metadata(gal1)
for album in gal2.albums.values():
extended_caching.load_metadata(album)
assert not hasattr(gal1, "metadata_cache")
assert not hasattr(gal2, "metadata_cache")
for al1, al2 in zip(gal1.albums.values(), gal2.albums.values()):
assert al1.metadata_cache == al2.metadata_cache


def test_load_exif(settings, tmpdir):
settings["destination"] = str(tmpdir)
gal1 = Gallery(settings, ncpu=1)
Expand Down Expand Up @@ -209,3 +234,19 @@ def test_load_metadata_missing(settings, tmpdir):
extended_caching.load_metadata(gal.albums["dir1/test2"])
assert gal.albums["dir1/test2"].medias[1].exif == "Bar"
assert gal.albums["dir1/test2"].medias[1].markdown_metadata != "Bar"


def test_empty_cache(settings, tmpdir):
cache_path = os.path.join(tmpdir, ".metadata_cache")
with open(cache_path, 'w') as f:
f.write("bad pickle file")
extended_caching._save_cache(cache_path, {})
assert not os.path.exists(cache_path)


def test_uncachable(settings, tmpdir):
cache_path = os.path.join(tmpdir, ".metadata_cache")
with open(cache_path, 'w') as f:
f.write("bad pickle file")
extended_caching._save_cache(cache_path, {'func': lambda x: x+1})
assert not os.path.exists(cache_path)

0 comments on commit 2b2d6e0

Please sign in to comment.