~ubuntu-branches/ubuntu/quantal/lazygal/quantal

« back to all changes in this revision

Viewing changes to lazygaltest/test_generators.py

  • Committer: Bazaar Package Importer
  • Author(s): Michal Čihař
  • Date: 2011-03-21 15:55:53 UTC
  • mfrom: (1.1.6 upstream) (2.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110321155553-2qmag1i39n54hjv3
Tags: 0.6-2
* Update home page.
* Suggest gstreamer for video support.
* Require python 2.6 or newer (Closes: #619032).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Lazygal, a lazy satic web gallery generator.
 
2
# Copyright (C) 2011 Alexandre Rossi <alexandre.rossi@gmail.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program; if not, write to the Free Software Foundation, Inc.,
 
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 
 
18
 
 
19
import unittest
 
20
import os
 
21
import datetime
 
22
 
 
23
from __init__ import LazygalTestGen
 
24
from lazygal.generators import WebalbumDir
 
25
from lazygal.sourcetree import Directory
 
26
from lazygal import pyexiv2api as pyexiv2
 
27
 
 
28
 
 
29
class TestGenerators(LazygalTestGen):
 
30
 
 
31
    def test_genfile_filelayout(self):
 
32
        source_subgal = self.setup_subgal('subgal', ['subgal_img.jpg'])
 
33
 
 
34
        dest_path = self.get_working_path()
 
35
 
 
36
        self.album.generate(dest_path)
 
37
 
 
38
        # Check root dir contents
 
39
        self.assertTrue(os.path.isdir(dest_path))
 
40
        for fn in ('index.html', 'index_medium.html'):
 
41
            self.assertTrue(os.path.isfile(os.path.join(dest_path, fn)))
 
42
 
 
43
        # Check subgal dir contents
 
44
        dest_subgal_path = os.path.join(dest_path, 'subgal')
 
45
        self.assertTrue(os.path.isdir(dest_subgal_path))
 
46
        for fn in ('index.html', 'index_medium.html',
 
47
                   'subgal_img.html', 'subgal_img_medium.html',
 
48
                   'subgal_img_thumb.jpg', 'subgal_img_small.jpg',
 
49
                   'subgal_img_medium.jpg'):
 
50
            self.assertTrue(os.path.isfile(os.path.join(dest_subgal_path, fn)))
 
51
 
 
52
    def test_filecleanup(self):
 
53
        '''
 
54
        Files that are not part of what was generated or updated shall be
 
55
        spotted.
 
56
        '''
 
57
        pics = [ 'img%d.jpg' % i for i in range(0, 8)]
 
58
        source_subgal = self.setup_subgal('subgal', pics)
 
59
 
 
60
        dest_path = self.get_working_path()
 
61
 
 
62
        self.album.generate(dest_path)
 
63
 
 
64
        # add a thumbs that should not be there
 
65
        self.add_img(dest_path, 'extra_thumb.jpg')
 
66
        self.add_img(os.path.join(dest_path, 'subgal'), 'extra_thumb2.jpg')
 
67
 
 
68
        # remove a pic in source_dir
 
69
        os.unlink(os.path.join(self.source_dir, 'subgal', 'img6.jpg'))
 
70
 
 
71
        # new objects to probe filesystem
 
72
        pics.remove('img6.jpg')
 
73
        source_subgal = Directory(os.path.join(self.source_dir, 'subgal'),
 
74
                                  [], pics, self.album)
 
75
        dest_subgal = WebalbumDir(source_subgal, [], self.album, dest_path)
 
76
        expected = map(lambda fn:\
 
77
                             unicode(os.path.join(dest_path, 'subgal', fn)),
 
78
                       ['extra_thumb2.jpg',
 
79
                        'img6_thumb.jpg',
 
80
                        'img6_small.jpg', 'img6_medium.jpg',
 
81
                        'img6.html', 'img6_medium.html',
 
82
                       ]
 
83
                      )
 
84
        self.assertEqual(sorted(dest_subgal.list_foreign_files()),
 
85
                         sorted(expected))
 
86
 
 
87
        source_gal = Directory(self.source_dir, [source_subgal], [], self.album)
 
88
        dest_gal = WebalbumDir(source_gal, [dest_subgal], self.album, dest_path)
 
89
        self.assertEqual(sorted(dest_gal.list_foreign_files()),
 
90
                         [os.path.join(dest_path, 'extra_thumb.jpg')])
 
91
 
 
92
    def test_originals_symlinks(self):
 
93
        img_path = self.add_img(self.source_dir, 'symlink_target.jpg')
 
94
 
 
95
        dest_dir = self.get_working_path()
 
96
        self.album.set_original(original=True, orig_symlink=True)
 
97
        self.album.generate(dest_dir)
 
98
 
 
99
        symlink = os.path.join(dest_dir, os.path.basename(img_path))
 
100
        # Test if the original in the webgal is a symlink
 
101
        self.assertEqual(os.path.islink(symlink), True)
 
102
        # Test if that symlink point to the image in the source_dir
 
103
        self.assertEqual(os.path.realpath(symlink), img_path)
 
104
 
 
105
    def test_metadata_osize_copy(self):
 
106
        img_path = self.add_img(self.source_dir, 'md_filled.jpg')
 
107
 
 
108
        # Add some metadata
 
109
        gps_data = pyexiv2.ImageMetadata(self.get_sample_path('sample-with-gps.jpg'))
 
110
        gps_data.read()
 
111
        source_image = pyexiv2.ImageMetadata(img_path)
 
112
        source_image.read()
 
113
        gps_data.copy(source_image)
 
114
        dummy_comment = 'nice photo'
 
115
        source_image['Exif.Photo.UserComment'] = dummy_comment
 
116
        dummy_date = datetime.datetime(2011, 2, 3, 12, 51, 43)
 
117
        source_image['Exif.Photo.DateTimeDigitized'] = dummy_date
 
118
        assert 'Exif.GPSInfo.GPSLongitude' in source_image.exif_keys
 
119
        assert 'Exif.GPSInfo.GPSLatitude' in source_image.exif_keys
 
120
        source_image.write()
 
121
 
 
122
        # Generate album
 
123
        dest_dir = self.get_working_path()
 
124
        self.album.generate(dest_dir)
 
125
 
 
126
        dest_img_path = os.path.join(dest_dir, 'md_filled_small.jpg')
 
127
        dest_image = pyexiv2.ImageMetadata(dest_img_path)
 
128
        dest_image.read()
 
129
 
 
130
        # Check that metadata is still here for reduced pictures.
 
131
        self.assertEqual(dest_image['Exif.Photo.UserComment'].value,
 
132
                         dummy_comment)
 
133
        self.assertEqual(dest_image['Exif.Photo.DateTimeDigitized'].value,
 
134
                         dummy_date)
 
135
 
 
136
        # Check that blacklised tags are not present anymore in the reduced
 
137
        # picture.
 
138
        def lat(): return dest_image['Exif.GPSInfo.GPSLongitude'].value
 
139
        self.assertRaises(KeyError, lat)
 
140
 
 
141
        def long(): return dest_image['Exif.GPSInfo.GPSLatitude'].value
 
142
        self.assertRaises(KeyError, long)
 
143
 
 
144
    def test_feed(self):
 
145
        img_path = self.add_img(self.source_dir, 'img01.jpg')
 
146
        dest_dir = self.get_working_path()
 
147
        self.album.generate(dest_dir, 'http://example.com/album/')
 
148
 
 
149
        self.assertEqual(os.path.isfile(os.path.join(dest_dir, 'index.xml')),
 
150
                         True)
 
151
 
 
152
 
 
153
class TestSpecialGens(LazygalTestGen):
 
154
 
 
155
    def setUp(self):
 
156
        super(TestSpecialGens, self).setUp(False)
 
157
        self.dest_path = os.path.join(self.tmpdir, 'dst')
 
158
 
 
159
    def test_paginate(self):
 
160
        '''
 
161
        It shall be possible to split big galleries on mutiple index pages.
 
162
        '''
 
163
        self.setup_album({'thumbs_per_page': 4})
 
164
 
 
165
        pics = [ 'img%d.jpg' % i for i in range(0, 9)]
 
166
        source_subgal = self.setup_subgal('subgal', pics)
 
167
 
 
168
        self.album.generate(self.dest_path)
 
169
        # FIXME: Check dest dir contents, test only catches uncaught exceptions
 
170
        # for now...
 
171
 
 
172
    def test_flatten(self):
 
173
        self.setup_album({'dir_flattening_depth': 1})
 
174
 
 
175
        source_subgal = self.setup_subgal('subgal', ['subgal_img.jpg'])
 
176
        self.album.generate(self.dest_path)
 
177
        # FIXME: Check dest dir contents, test only catches uncaught exceptions
 
178
        # for now...
 
179
 
 
180
    def test_flattenpaginate(self):
 
181
        self.setup_album({'thumbs_per_page': 4, 'dir_flattening_depth': 1,})
 
182
 
 
183
        pics = [ 'img%d.jpg' % i for i in range(0, 9)]
 
184
        source_subgal = self.setup_subgal('subgal', pics)
 
185
 
 
186
        self.album.generate(self.dest_path)
 
187
        # FIXME: Check dest dir contents, test only catches uncaught exceptions
 
188
        # for now...
 
189
 
 
190
 
 
191
if __name__ == '__main__':
 
192
    unittest.main()
 
193
 
 
194
 
 
195
# vim: ts=4 sw=4 expandtab