From 09ff3b9b5325be1c86a91fc32a0d710cd1fda474 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sun, 2 Mar 2014 23:05:15 +0100 Subject: [PATCH] Update tests + small fixes. --- sigal/gallery.py | 35 +++++++----- sigal/writer.py | 3 +- tests/test_gallery.py | 127 +++++++++++++++++++++++------------------- 3 files changed, 95 insertions(+), 70 deletions(-) diff --git a/sigal/gallery.py b/sigal/gallery.py index a175767..475e490 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -70,7 +70,7 @@ class Media(UnicodeMixin): @property def big(self): if self.settings['keep_orig']: - return get_orig(self.settings, self.file_path) + return get_orig(self.settings, self.filename) else: return None @@ -119,6 +119,7 @@ class Album(UnicodeMixin): def __init__(self, path, settings, dirnames, filenames, gallery): self.path = path + self.name = path.split(os.path.sep)[-1] self.gallery = gallery self.settings = settings self.orig_path = None @@ -144,7 +145,7 @@ class Album(UnicodeMixin): # optionally add index.html to the URLs self.url_ext = self.output_file if settings['index_in_url'] else '' - self.url = self.path + '/' + self.url_ext + self.url = self.name + '/' + self.url_ext self.index_url = os.path.relpath(settings['destination'], self.dst_path) + '/' + self.url_ext @@ -204,7 +205,9 @@ class Album(UnicodeMixin): @property def albums(self): - return [self.gallery.albums[path] for path in self.subdirs] + root_path = self.path if self.path != '.' else '' + return [self.gallery.albums[join(root_path, path)] + for path in self.subdirs] @property def thumbnail(self): @@ -212,9 +215,10 @@ class Album(UnicodeMixin): # stop if it is already set and a valid file if self._thumbnail and isfile(join(self.src_path, self._thumbnail)): - self.logger.debug("Thumbnail for %s : %s", self.src_path, - self._thumbnail) - return self._thumbnail + thumbnail = join(self.name, get_thumb(self.settings, + self._thumbnail)) + self.logger.debug("Thumbnail for %r : %s", self, self._thumbnail) + return thumbnail else: # find and return the first landscape image for f in self.medias: @@ -222,22 +226,27 @@ class Album(UnicodeMixin): if ext in Image.extensions: im = PILImage.open(f.src_path) if im.size[0] > im.size[1]: - self._thumbnail = join(self.path, f.thumbnail) + self._thumbnail = join(self.name, f.thumbnail) + self.logger.debug( + "Use 1st landscape image as thumbnail for %r : %s", + self, self._thumbnail) return self._thumbnail # else simply return the 1st media file if not self._thumbnail and self.medias: - self._thumbnail = join(self.path, self.medias[0].thumbnail) + self._thumbnail = join(self.name, self.medias[0].thumbnail) + self.logger.debug("Use the 1st image as thumbnail for %r : %s", + self, self._thumbnail) return self._thumbnail # use the thumbnail of their sub-directories if not self._thumbnail: for path, album in self.gallery.get_albums(self.path): if album.thumbnail: - self._thumbnail = join( - os.path.relpath(path, self.path), album.thumbnail) - self.logger.debug("Found thumbnail for %s : %s", - self.src_path, self._thumbnail) + self._thumbnail = join(self.name, album.thumbnail) + self.logger.debug( + "Using thumbnail from sub-directory for %r : %s", + self, self._thumbnail) return self._thumbnail self.logger.error('Thumbnail not found for %r', self) @@ -259,7 +268,7 @@ class Album(UnicodeMixin): break url = os.path.relpath(path, self.path) + '/' + self.url_ext - breadcrumb.append((url, self.gallery[path].title)) + breadcrumb.append((url, self.gallery.albums[path].title)) breadcrumb.reverse() return breadcrumb diff --git a/sigal/writer.py b/sigal/writer.py index 77c66bb..b582a7b 100644 --- a/sigal/writer.py +++ b/sigal/writer.py @@ -106,7 +106,8 @@ class Writer(object): 'url': os.path.relpath(self.theme_path, album.dst_path)}, } - for attr in ('albums', 'breadcrumb', 'index_url', 'medias', 'zip'): + for attr in ('albums', 'breadcrumb', 'index_url', 'medias', 'zip', + 'title'): ctx[attr] = getattr(album, attr) return ctx diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 7852c65..b690a39 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -3,7 +3,8 @@ import os import pytest -from sigal.gallery import Gallery, PathsDb, get_metadata +from os.path import join +from sigal.gallery import Album, Media, Image, Video, Gallery from sigal.settings import read_settings CURRENT_DIR = os.path.dirname(__file__) @@ -12,23 +13,31 @@ SAMPLE_DIR = os.path.join(CURRENT_DIR, 'sample') REF = { 'dir1': { 'title': 'An example gallery', - 'thumbnail': 'test1/11.jpg', + 'name': 'dir1', + 'thumbnail': 'dir1/test1/thumbnails/11.tn.jpg', + 'subdirs': ['test1', 'test2'], 'medias': [], }, 'dir1/test1': { 'title': 'An example sub-category', - 'thumbnail': '11.jpg', + 'name': 'test1', + 'thumbnail': 'test1/thumbnails/11.tn.jpg', + 'subdirs': [], 'medias': ['11.jpg', 'archlinux-kiss-1024x640.png', 'flickr_jerquiaga_2394751088_cc-by-nc.jpg'], }, 'dir1/test2': { 'title': 'Test2', - 'thumbnail': '21.jpg', + 'name': 'test2', + 'thumbnail': 'test2/thumbnails/21.tn.jpg', + 'subdirs': [], 'medias': ['21.jpg', '22.jpg'], }, 'dir2': { 'title': 'Another example gallery with a very long name', - 'thumbnail': 'm57_the_ring_nebula-587px.jpg', + 'name': 'dir2', + 'thumbnail': 'dir2/thumbnails/m57_the_ring_nebula-587px.tn.jpg', + 'subdirs': [], 'medias': ['exo20101028-b-full.jpg', 'm57_the_ring_nebula-587px.jpg', 'Hubble ultra deep field.jpg', @@ -36,86 +45,93 @@ REF = { }, u'accentué': { 'title': u'Accentué', - 'thumbnail': u'hélicoïde.jpg', + 'name': u'accentué', + 'thumbnail': u'accentué/thumbnails/hélicoïde.tn.jpg', + 'subdirs': [], 'medias': [u'hélicoïde.jpg', 'superdupont_source_wikipedia_en.jpg'], }, 'video': { 'title': 'Video', - 'thumbnail': 'stallman software-freedom-day-low.ogv', + 'name': 'video', + 'thumbnail': 'video/thumbnails/stallman software-freedom-day-low.tn.jpg', + 'subdirs': [], 'medias': ['stallman software-freedom-day-low.ogv'] } } @pytest.fixture(scope='module') -def paths(): - """Read the sample config file and build the PathsDb object.""" +def settings(): + """Read the sample config file.""" + return read_settings(os.path.join(SAMPLE_DIR, 'sigal.conf.py')) - default_conf = os.path.join(SAMPLE_DIR, 'sigal.conf.py') - settings = read_settings(default_conf) - return PathsDb(os.path.join(SAMPLE_DIR, 'pictures'), - settings['img_ext_list'], settings['vid_ext_list']) +def test_media(settings): + m = Media('11.jpg', 'dir1/test1', settings) + path = join('dir1', 'test1') + file_path = join(path, '11.jpg') + thumb = join('thumbnails', '11.tn.jpg') -@pytest.fixture(scope='module') -def db(paths): - paths.build() - return paths.db - - -def test_filelist(db): - assert set(db.keys()) == set([ - 'paths_list', 'skipped_dir', '.', 'dir1', 'dir2', 'dir1/test1', - 'dir1/test2', u'accentué', 'video']) + assert m.filename == '11.jpg' + assert m.file_path == file_path + assert m.src_path == join(settings['source'], file_path) + assert m.dst_path == join(settings['destination'], file_path) + assert m.thumb_name == thumb + assert m.thumb_path == join(settings['destination'], path, thumb) - assert set(db['paths_list']) == set([ - '.', 'dir1', 'dir1/test1', 'dir1/test2', 'dir2', u'accentué', 'video']) + assert repr(m) == "('{}')".format(file_path) + assert str(m) == file_path - assert set(db['skipped_dir']) == set(['empty', 'dir1/empty']) - assert db['.']['medias'] == [] - assert set(db['.']['subdir']) == set([u'accentué', 'dir1', 'dir2', - 'video']) +def test_media_orig(settings): + settings['keep_orig'] = False + m = Media('11.jpg', 'dir1/test1', settings) + assert m.big is None -def test_title(db): - for p in REF.keys(): - assert db[p]['title'] == REF[p]['title'] + settings['keep_orig'] = True + m = Media('11.jpg', 'dir1/test1', settings) + assert m.big == 'original/11.jpg' -def test_thumbnail(db): - for p in REF.keys(): - assert db[p]['thumbnail'] == REF[p]['thumbnail'] - +def test_image(settings, tmpdir): + settings['destination'] = str(tmpdir) + m = Image('11.jpg', 'dir1/test1', settings) + assert m.exif['datetime'] == u'Sunday, 22. January 2006' -def test_medialist(db): - for p in REF.keys(): - assert set(db[p]['medias']) == set(REF[p]['medias']) + os.makedirs(join(settings['destination'], 'dir1', 'test1', 'thumbnails')) + assert m.thumbnail == join('thumbnails', '11.tn.jpg') + assert os.path.isfile(m.thumb_path) -def test_get_subdir(paths): - assert set(paths.get_subdirs('dir1/test1')) == set() - assert set(paths.get_subdirs('dir1')) == set(['dir1/test1', 'dir1/test2']) - assert set(paths.get_subdirs('.')) == set([ - 'dir1', 'dir2', 'dir1/test1', 'dir1/test2', u'accentué', 'video']) +def test_video(settings, tmpdir): + settings['destination'] = str(tmpdir) + m = Video('stallman software-freedom-day-low.ogv', 'video', settings) + file_path = join('video', 'stallman software-freedom-day-low.webm') + assert m.file_path == file_path + assert m.dst_path == join(settings['destination'], file_path) + os.makedirs(join(settings['destination'], 'video', 'thumbnails')) + assert m.thumbnail == join('thumbnails', + 'stallman software-freedom-day-low.tn.jpg') + assert os.path.isfile(m.thumb_path) -def test_get_metadata(): - "Test the get_metadata function." - m = get_metadata(os.path.join(SAMPLE_DIR, 'pictures', 'dir1')) - assert m['title'] == REF['dir1']['title'] - assert m['thumbnail'] == '' +@pytest.mark.parametrize("path,album", REF.items()) +def test_album(path, album, settings, tmpdir): + settings['destination'] = str(tmpdir) + gal = Gallery(settings, ncpu=1) + a = Album(path, settings, album['subdirs'], album['medias'], gal) - m = get_metadata(os.path.join(SAMPLE_DIR, 'pictures', 'dir2')) - assert m['title'] == REF['dir2']['title'] - assert m['thumbnail'] == REF['dir2']['thumbnail'] + assert a.title == album['title'] + assert a.name == album['name'] + assert a.subdirs == album['subdirs'] + assert a.thumbnail == album['thumbnail'] + assert [m.filename for m in a.medias] == album['medias'] -def test_gallery(tmpdir): +def test_gallery(settings, tmpdir): "Test the Gallery class." - default_conf = os.path.join(SAMPLE_DIR, 'sigal.conf.py') - settings = read_settings(default_conf) settings['destination'] = str(tmpdir) gal = Gallery(settings, ncpu=1) gal.build() @@ -127,4 +143,3 @@ def test_gallery(tmpdir): html = f.read() assert 'Sigal test gallery' in html -