From a7adf57249ec646f1152265610d9edbb53418731 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 12 Mar 2013 13:57:52 +0100 Subject: [PATCH 1/3] Fix a bug when swapping the width and height in settings. --- sigal/settings.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sigal/settings.py b/sigal/settings.py index 78e18ea..3ee5aff 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -56,8 +56,8 @@ def read_settings(filename=None): """Read settings from a config file in the source_dir root.""" logger = logging.getLogger(__name__) - settings = _DEFAULT_CONFIG.copy() + if filename: tempdict = {} execfile(filename, tempdict) @@ -65,10 +65,9 @@ def read_settings(filename=None): if k not in ['__builtins__']) for key in ('img_size', 'thumb_size'): - size = settings[key] - if size[1] > size[0]: - size[0], size[1] = size[1], size[0] - settings[key] = size + w, h = settings[key] + if h > w: + settings[key] = (h, w) logger.warning("The %s setting should be specified with the " "largest value first.", key) From e33761ae15532d0090c4fbae656380e835338ae6 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 12 Mar 2013 13:58:28 +0100 Subject: [PATCH 2/3] Test the settings and gallery modules with pytest. --- tests/test_gallery.py | 110 ++++++++++++++++++++--------------------- tests/test_settings.py | 55 +++++++++++---------- 2 files changed, 84 insertions(+), 81 deletions(-) diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 0ed7c65..a50d470 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -1,11 +1,7 @@ # -*- coding:utf-8 -*- import os - -try: - import unittest2 as unittest -except ImportError: - import unittest # NOQA +import pytest from sigal.gallery import PathsDb, get_metadata from sigal.settings import read_settings @@ -40,62 +36,64 @@ REF = { } -class TestPathsDb(unittest.TestCase): - "Test the PathsDb class." +@pytest.fixture(scope='module') +def paths(): + """Read the sample config file and build the PathsDb object.""" - @classmethod - def setUp(cls): - """Read the sample config file and build the PathsDb object.""" - - default_conf = os.path.join(SAMPLE_DIR, 'sigal.conf.py') - settings = read_settings(default_conf) - cls.paths = PathsDb(SAMPLE_DIR, settings['ext_list']) - cls.paths.build() - cls.db = cls.paths.db - - def test_filelist(self): - self.assertItemsEqual(self.db.keys(), - ['paths_list', 'skipped_dir', '.', 'dir1', - 'dir2', 'dir1/test1', 'dir1/test2']) - - self.assertListEqual(self.db['paths_list'], - ['.', 'dir1', 'dir1/test1', 'dir1/test2', 'dir2']) - self.assertItemsEqual(self.db['skipped_dir'], ['empty', 'dir1/empty']) - - self.assertListEqual(self.db['.']['img'], []) - self.assertItemsEqual(self.db['.']['subdir'], ['dir1', 'dir2']) - - def test_title(self): - for p in REF.keys(): - self.assertEqual(self.db[p]['title'], REF[p]['title']) - - def test_thumbnail(self): - for p in REF.keys(): - self.assertEqual(self.db[p]['thumbnail'], REF[p]['thumbnail']) - - def test_imglist(self): - for p in REF.keys(): - self.assertItemsEqual(self.db[p]['img'], REF[p]['img']) - - def test_get_subdir(self): - self.assertItemsEqual(self.paths.get_subdirs('dir1'), - ['dir1/test1', 'dir1/test2']) - self.assertItemsEqual(self.paths.get_subdirs('.'), - ['dir1', 'dir2', 'dir1/test1', 'dir1/test2']) + default_conf = os.path.join(SAMPLE_DIR, 'sigal.conf.py') + settings = read_settings(default_conf) + return PathsDb(SAMPLE_DIR, settings['ext_list']) -class TestMetadata(unittest.TestCase): - "Test the Gallery class." +@pytest.fixture(scope='module') +def db(paths): + paths.build() + return paths.db - def test_dir1(self): - m = get_metadata(os.path.join(SAMPLE_DIR, 'dir1')) - self.assertEqual(m['title'], REF['dir1']['title']) - self.assertEqual(m['thumbnail'], '') - def test_dir2(self): - m = get_metadata(os.path.join(SAMPLE_DIR, 'dir2')) - self.assertEqual(m['title'], REF['dir2']['title']) - self.assertEqual(m['thumbnail'], REF['dir2']['thumbnail']) +def test_filelist(db): + assert set(db.keys()) == set(['paths_list', 'skipped_dir', '.', 'dir1', + 'dir2', 'dir1/test1', 'dir1/test2']) + + assert db['paths_list'] == ['.', 'dir1', 'dir1/test1', 'dir1/test2', + 'dir2'] + + assert set(db['skipped_dir']) == set(['empty', 'dir1/empty']) + assert db['.']['img'] == [] + assert set(db['.']['subdir']) == set(['dir1', 'dir2']) + + +def test_title(db): + for p in REF.keys(): + assert db[p]['title'] == REF[p]['title'] + + +def test_thumbnail(db): + for p in REF.keys(): + assert db[p]['thumbnail'] == REF[p]['thumbnail'] + + +def test_imglist(db): + for p in REF.keys(): + assert set(db[p]['img']) == set(REF[p]['img']) + + +def test_get_subdir(paths): + assert set(paths.get_subdirs('dir1')) == set(['dir1/test1', 'dir1/test2']) + assert set(paths.get_subdirs('.')) == set(['dir1', 'dir2', 'dir1/test1', + 'dir1/test2']) + + +def test_get_metadata(): + "Test the get_metadata function." + + m = get_metadata(os.path.join(SAMPLE_DIR, 'dir1')) + assert m['title'] == REF['dir1']['title'] + assert m['thumbnail'] == '' + + m = get_metadata(os.path.join(SAMPLE_DIR, 'dir2')) + assert m['title'] == REF['dir2']['title'] + assert m['thumbnail'] == REF['dir2']['thumbnail'] # class TestGallery(unittest.TestCase): diff --git a/tests/test_settings.py b/tests/test_settings.py index f358557..b08047d 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,37 +1,42 @@ # -*- coding:utf-8 -*- import os - -try: - import unittest2 as unittest -except ImportError: - import unittest # NOQA +import pytest from sigal.settings import read_settings, get_thumb -class TestSettings(unittest.TestCase): - "Read a settings file and check that the configuration is well done." +@pytest.fixture(scope='module') +def settings(): + """Read the sample config file.""" + path = os.path.abspath(os.path.dirname(__file__)) + return read_settings(os.path.join(path, 'sample', 'sigal.conf.py')) - def setUp(self): - "Read the sample config file" - self.path = os.path.abspath(os.path.dirname(__file__)) - default_conf = os.path.join(self.path, 'sample', 'sigal.conf.py') - self.settings = read_settings(default_conf) - def test_sizes(self): - "Test that image sizes are correctly read" - self.assertTupleEqual(self.settings['img_size'], (640, 480)) - self.assertTupleEqual(self.settings['thumb_size'], (200, 150)) +def test_read_settings(settings): + """Test that the settings are correctly read.""" + assert settings['img_size'] == (640, 480) + assert settings['thumb_size'] == (200, 150) + assert settings['thumb_suffix'] == '.tn' - def test_settings(self): - self.assertEqual(self.settings['thumb_suffix'], '.tn') - def test_thumb(self): - """Test the get_thumb function.""" +def test_thumb(settings): + """Test the get_thumb function.""" + tests = [('example.jpg', 'thumbnails/example.tn.jpg'), + ('test/example.jpg', 'test/thumbnails/example.tn.jpg'), + ('test/t/example.jpg', 'test/t/thumbnails/example.tn.jpg')] + for src, ref in tests: + assert get_thumb(settings, src) == ref - tests = [('example.jpg', 'thumbnails/example.tn.jpg'), - ('test/example.jpg', 'test/thumbnails/example.tn.jpg'), - ('test/t/example.jpg', 'test/t/thumbnails/example.tn.jpg')] - for src, ref in tests: - self.assertEqual(get_thumb(self.settings, src), ref) + +def test_img_sizes(tmpdir): + """Test that image size is swaped if needed.""" + + conf = tmpdir.join('sigal.conf.py') + conf.write("""# -*- coding: utf-8 -*- + +thumb_size = (150, 200) +""") + + settings = read_settings(str(conf)) + assert settings['thumb_size'] == (200, 150) From d7e169095bc025c529db542ed9142d48e5d17b0b Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 12 Mar 2013 22:02:12 +0100 Subject: [PATCH 3/3] Update travis config for pytest. --- .travis.yml | 4 ++-- requirements.txt | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index cabc374..2157ea5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,6 @@ before_install: - sudo apt-get update -qq - sudo apt-get install -qq libfreetype6-dev libjpeg8-dev zlib1g-dev install: - - pip install nose unittest2 --use-mirrors + - pip install pytest --use-mirrors - pip install . --use-mirrors -script: nosetests +script: pytest diff --git a/requirements.txt b/requirements.txt index 38259c2..45cc767 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ clint Jinja2 Markdown Pillow +pytest