Browse Source

more coverage improvements

pull/443/head
David Schultz 4 years ago
parent
commit
78ecbfe668
  1. 2
      sigal/plugins/extended_caching.py
  2. 44
      tests/test_extended_caching.py
  3. 3
      tests/test_image.py

2
sigal/plugins/extended_caching.py

@ -101,7 +101,7 @@ def _restore_cache(gallery):
else: else:
gallery.metadataCache = {} gallery.metadataCache = {}
except Exception as e: except Exception as e:
logger.warn("Could not load cache: %s", e) logger.warning("Could not load cache: %s", e)
gallery.metadataCache = {} gallery.metadataCache = {}

44
tests/test_extended_caching.py

@ -1,7 +1,7 @@
import os import os
import pickle import pickle
from sigal.gallery import Gallery from sigal.gallery import Gallery, Image
from sigal.plugins import extended_caching from sigal.plugins import extended_caching
CURRENT_DIR = os.path.dirname(__file__) CURRENT_DIR = os.path.dirname(__file__)
@ -47,6 +47,13 @@ def test_save_cache(settings, tmpdir):
cache_img = cache["iptcTest/2.jpg"] cache_img = cache["iptcTest/2.jpg"]
assert cache_img["markdown_metadata"] == album.medias[1].markdown_metadata assert cache_img["markdown_metadata"] == album.medias[1].markdown_metadata
# test if file disappears
gal.albums["exifTest"].medias.append(Image("foooo.jpg", "exifTest", settings))
extended_caching.save_cache(gal)
with open(cachePath, "rb") as cacheFile:
cache = pickle.load(cacheFile)
assert "exifTest/foooo.jpg" not in cache
def test_restore_cache(settings, tmpdir): def test_restore_cache(settings, tmpdir):
settings['destination'] = str(tmpdir) settings['destination'] = str(tmpdir)
@ -56,11 +63,20 @@ def test_restore_cache(settings, tmpdir):
extended_caching._restore_cache(gal2) extended_caching._restore_cache(gal2)
assert gal1.metadataCache == gal2.metadataCache assert gal1.metadataCache == gal2.metadataCache
# test bad cache
cachePath = os.path.join(settings['destination'], ".metadata_cache")
with open(cachePath, 'w') as f:
f.write('bad pickle file')
extended_caching._restore_cache(gal2)
assert gal2.metadataCache == {}
def test_load_exif(settings, tmpdir): def test_load_exif(settings, tmpdir):
settings['destination'] = str(tmpdir) settings['destination'] = str(tmpdir)
gal1 = Gallery(settings, ncpu=1) gal1 = Gallery(settings, ncpu=1)
gal1.albums["exifTest"].medias[2].exif = "blafoo" gal1.albums["exifTest"].medias[2].exif = "blafoo"
# set mod_date in future, to force these values
gal1.metadataCache = { gal1.metadataCache = {
"exifTest/21.jpg": {"exif": "Foo", "mod_date": 100000000000}, "exifTest/21.jpg": {"exif": "Foo", "mod_date": 100000000000},
"exifTest/22.jpg": {"exif": "Bar", "mod_date": 100000000000}, "exifTest/22.jpg": {"exif": "Bar", "mod_date": 100000000000},
@ -80,3 +96,29 @@ def test_load_exif(settings, tmpdir):
assert gal2.albums["exifTest"].medias[0].exif == "Foo" assert gal2.albums["exifTest"].medias[0].exif == "Foo"
assert gal2.albums["exifTest"].medias[1].exif == "Bar" assert gal2.albums["exifTest"].medias[1].exif == "Bar"
assert gal2.albums["exifTest"].medias[2].exif == "blafoo" assert gal2.albums["exifTest"].medias[2].exif == "blafoo"
def test_load_metadata_missing(settings, tmpdir):
settings['destination'] = str(tmpdir)
gal = Gallery(settings, ncpu=1)
extended_caching.save_cache(gal)
assert gal.metadataCache
# test if file disappears
gal.albums["exifTest"].medias.append(Image("foooo.jpg", "exifTest", settings))
# set mod_date to -1 to force cache update
gal.metadataCache = {
"exifTest/_index": {"mod_date": -1,},
"exifTest/21.jpg": {"exif": "Foo", "mod_date": -1},
"exifTest/foooo.jpg": {"exif": "Foo"},
"dir1/test2/22.jpg": {"exif": "Bar", "mod_date": 100000000000, "meta_mod_date": -1, "markdown_metadata": "Bar"},
}
# errors should all be caught
extended_caching.load_metadata(gal.albums["exifTest"])
assert gal.albums["exifTest"].medias[0].exif != "Foo"
assert gal.albums["exifTest"].medias[-1].exif != "Foo"
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"

3
tests/test_image.py

@ -242,7 +242,8 @@ def test_get_iptc_data(caplog):
assert ['IPTC Error in'] == [log.message[:13] for log in caplog.records] assert ['IPTC Error in'] == [log.message[:13] for log in caplog.records]
def test_get_image_metadata_bad(caplog): def test_get_image_metadata_exceptions():
# image does not exist
test_image = 'bad_image.jpg' test_image = 'bad_image.jpg'
src_file = os.path.join(CURRENT_DIR, 'sample', test_image) src_file = os.path.join(CURRENT_DIR, 'sample', test_image)
data = get_image_metadata(src_file) data = get_image_metadata(src_file)

Loading…
Cancel
Save