1
|
|
|
# -*- coding: utf-8 |
2
|
|
|
# Project imports |
3
|
|
|
from __future__ import unicode_literals |
4
|
|
|
import os |
5
|
|
|
import sys |
6
|
|
|
|
7
|
|
|
from datetime import datetime |
8
|
|
|
import shutil |
9
|
|
|
import tempfile |
10
|
|
|
import time |
11
|
|
|
|
12
|
|
|
from nose.plugins.skip import SkipTest |
13
|
|
|
|
14
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))) |
15
|
|
|
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) |
16
|
|
|
|
17
|
|
|
import helper |
18
|
|
|
from elodie.media.media import Media |
19
|
|
|
from elodie.media.photo import Photo |
20
|
|
|
|
21
|
|
|
os.environ['TZ'] = 'GMT' |
22
|
|
|
|
23
|
|
|
def test_photo_extensions(): |
24
|
|
|
photo = Photo() |
25
|
|
|
extensions = photo.extensions |
26
|
|
|
|
27
|
|
|
assert 'arw' in extensions |
28
|
|
|
assert 'cr2' in extensions |
29
|
|
|
assert 'dng' in extensions |
30
|
|
|
assert 'gif' in extensions |
31
|
|
|
assert 'heic' in extensions |
32
|
|
|
assert 'jpg' in extensions |
33
|
|
|
assert 'jpeg' in extensions |
34
|
|
|
assert 'nef' in extensions |
35
|
|
|
assert 'rw2' in extensions |
36
|
|
|
|
37
|
|
|
valid_extensions = Photo.get_valid_extensions() |
38
|
|
|
|
39
|
|
|
assert extensions == valid_extensions, valid_extensions |
40
|
|
|
|
41
|
|
|
def test_empty_album(): |
42
|
|
|
photo = Photo(helper.get_file('plain.jpg')) |
43
|
|
|
assert photo.get_album() is None |
44
|
|
|
|
45
|
|
|
def test_has_album(): |
46
|
|
|
photo = Photo(helper.get_file('with-album.jpg')) |
47
|
|
|
album = photo.get_album() |
48
|
|
|
|
49
|
|
|
assert album == 'Test Album', album |
50
|
|
|
|
51
|
|
|
def test_is_valid(): |
52
|
|
|
photo = Photo(helper.get_file('plain.jpg')) |
53
|
|
|
|
54
|
|
|
assert photo.is_valid() |
55
|
|
|
|
56
|
|
|
def test_is_not_valid(): |
57
|
|
|
photo = Photo(helper.get_file('text.txt')) |
58
|
|
|
|
59
|
|
|
assert not photo.is_valid() |
60
|
|
|
|
61
|
|
|
def test_get_metadata_of_invalid_photo(): |
62
|
|
|
photo = Photo(helper.get_file('invalid.jpg')) |
63
|
|
|
metadata = photo.get_metadata() |
64
|
|
|
|
65
|
|
|
assert metadata is None |
66
|
|
|
|
67
|
|
|
def test_get_coordinate_default(): |
68
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
69
|
|
|
coordinate = photo.get_coordinate() |
70
|
|
|
|
71
|
|
|
assert helper.isclose(coordinate,37.3667027222), coordinate |
72
|
|
|
|
73
|
|
|
def test_get_coordinate_latitude(): |
74
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
75
|
|
|
coordinate = photo.get_coordinate('latitude') |
76
|
|
|
|
77
|
|
|
assert helper.isclose(coordinate,37.3667027222), coordinate |
78
|
|
|
|
79
|
|
|
def test_get_coordinate_latitude_minus(): |
80
|
|
|
photo = Photo(helper.get_file('with-location-inv.jpg')) |
81
|
|
|
coordinate = photo.get_coordinate('latitude') |
82
|
|
|
|
83
|
|
|
assert helper.isclose(coordinate,-37.3667027222), coordinate |
84
|
|
|
|
85
|
|
|
def test_get_coordinate_longitude(): |
86
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
87
|
|
|
coordinate = photo.get_coordinate('longitude') |
88
|
|
|
|
89
|
|
|
assert helper.isclose(coordinate,-122.033383611), coordinate |
90
|
|
|
|
91
|
|
|
def test_get_coordinate_longitude_plus(): |
92
|
|
|
photo = Photo(helper.get_file('with-location-inv.jpg')) |
93
|
|
|
coordinate = photo.get_coordinate('longitude') |
94
|
|
|
|
95
|
|
|
assert helper.isclose(coordinate,122.033383611), coordinate |
96
|
|
|
|
97
|
|
|
def test_get_coordinates_without_exif(): |
98
|
|
|
photo = Photo(helper.get_file('no-exif.jpg')) |
99
|
|
|
latitude = photo.get_coordinate('latitude') |
100
|
|
|
longitude = photo.get_coordinate('longitude') |
101
|
|
|
|
102
|
|
|
assert latitude is None, latitude |
103
|
|
|
assert longitude is None, longitude |
104
|
|
|
|
105
|
|
|
def test_get_coordinates_with_zero_coordinate(): |
106
|
|
|
photo = Photo(helper.get_file('with-location-zero-coordinate.jpg')) |
107
|
|
|
latitude = photo.get_coordinate('latitude') |
108
|
|
|
longitude = photo.get_coordinate('longitude') |
109
|
|
|
|
110
|
|
|
assert helper.isclose(latitude,51.55325), latitude |
111
|
|
|
assert helper.isclose(longitude,-0.00417777777778), longitude |
112
|
|
|
|
113
|
|
|
def test_get_date_taken(): |
114
|
|
|
photo = Photo(helper.get_file('plain.jpg')) |
115
|
|
|
date_taken = photo.get_date_taken() |
116
|
|
|
|
117
|
|
|
#assert date_taken == (2015, 12, 5, 0, 59, 26, 5, 339, 0), date_taken |
118
|
|
|
assert date_taken == helper.time_convert((2015, 12, 5, 0, 59, 26, 5, 339, 0)), date_taken |
119
|
|
|
|
120
|
|
|
def test_get_date_taken_without_exif(): |
121
|
|
|
source = helper.get_file('no-exif.jpg') |
122
|
|
|
photo = Photo(source) |
123
|
|
|
date_taken = photo.get_date_taken() |
124
|
|
|
|
125
|
|
|
date_taken_from_file = time.gmtime(min(os.path.getmtime(source), os.path.getctime(source))) |
126
|
|
|
|
127
|
|
|
assert date_taken == date_taken_from_file, date_taken |
128
|
|
|
|
129
|
|
|
def test_get_camera_make(): |
130
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
131
|
|
|
make = photo.get_camera_make() |
132
|
|
|
|
133
|
|
|
assert make == 'Canon', make |
134
|
|
|
|
135
|
|
|
def test_get_camera_make_not_set(): |
136
|
|
|
photo = Photo(helper.get_file('no-exif.jpg')) |
137
|
|
|
make = photo.get_camera_make() |
138
|
|
|
|
139
|
|
|
assert make is None, make |
140
|
|
|
|
141
|
|
|
def test_get_camera_model(): |
142
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
143
|
|
|
model = photo.get_camera_model() |
144
|
|
|
|
145
|
|
|
assert model == 'Canon EOS REBEL T2i', model |
146
|
|
|
|
147
|
|
|
def test_get_camera_model_not_set(): |
148
|
|
|
photo = Photo(helper.get_file('no-exif.jpg')) |
149
|
|
|
model = photo.get_camera_model() |
150
|
|
|
|
151
|
|
|
assert model is None, model |
152
|
|
|
|
153
|
|
|
def test_is_valid(): |
154
|
|
|
photo = Photo(helper.get_file('with-location.jpg')) |
155
|
|
|
|
156
|
|
|
assert photo.is_valid() |
157
|
|
|
|
158
|
|
|
def test_is_not_valid(): |
159
|
|
|
photo = Photo(helper.get_file('text.txt')) |
160
|
|
|
|
161
|
|
|
assert not photo.is_valid() |
162
|
|
|
|
163
|
|
|
def test_is_valid_fallback_using_pillow(): |
164
|
|
|
photo = Photo(helper.get_file('imghdr-error.jpg')) |
165
|
|
|
|
166
|
|
|
assert photo.is_valid() |
167
|
|
|
|
168
|
|
|
def test_pillow_not_loaded(): |
169
|
|
|
photo = Photo(helper.get_file('imghdr-error.jpg')) |
170
|
|
|
photo.pillow = None |
171
|
|
|
|
172
|
|
|
assert photo.is_valid() == False |
173
|
|
|
|
174
|
|
View Code Duplication |
def test_set_album(): |
|
|
|
|
175
|
|
|
temporary_folder, folder = helper.create_working_folder() |
176
|
|
|
|
177
|
|
|
origin = '%s/photo.jpg' % folder |
178
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
179
|
|
|
|
180
|
|
|
photo = Photo(origin) |
181
|
|
|
metadata = photo.get_metadata() |
182
|
|
|
|
183
|
|
|
assert metadata['album'] is None, metadata['album'] |
184
|
|
|
|
185
|
|
|
status = photo.set_album('Test Album') |
186
|
|
|
|
187
|
|
|
assert status == True, status |
188
|
|
|
|
189
|
|
|
photo_new = Photo(origin) |
190
|
|
|
metadata_new = photo_new.get_metadata() |
191
|
|
|
|
192
|
|
|
shutil.rmtree(folder) |
193
|
|
|
|
194
|
|
|
assert metadata_new['album'] == 'Test Album', metadata_new['album'] |
195
|
|
|
|
196
|
|
|
def test_set_date_taken_with_missing_datetimeoriginal(): |
197
|
|
|
# When datetimeoriginal (or other key) is missing we have to add it gh-74 |
198
|
|
|
# https://github.com/jmathai/elodie/issues/74 |
199
|
|
|
temporary_folder, folder = helper.create_working_folder() |
200
|
|
|
|
201
|
|
|
origin = '%s/photo.jpg' % folder |
202
|
|
|
shutil.copyfile(helper.get_file('no-exif.jpg'), origin) |
203
|
|
|
|
204
|
|
|
photo = Photo(origin) |
205
|
|
|
status = photo.set_date_taken(datetime(2013, 9, 30, 7, 6, 5)) |
206
|
|
|
|
207
|
|
|
assert status == True, status |
208
|
|
|
|
209
|
|
|
photo_new = Photo(origin) |
210
|
|
|
metadata = photo_new.get_metadata() |
211
|
|
|
|
212
|
|
|
date_taken = metadata['date_taken'] |
213
|
|
|
|
214
|
|
|
shutil.rmtree(folder) |
215
|
|
|
|
216
|
|
|
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken'] |
217
|
|
|
assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken'] |
218
|
|
|
|
219
|
|
View Code Duplication |
def test_set_date_taken(): |
|
|
|
|
220
|
|
|
temporary_folder, folder = helper.create_working_folder() |
221
|
|
|
|
222
|
|
|
origin = '%s/photo.jpg' % folder |
223
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
224
|
|
|
|
225
|
|
|
photo = Photo(origin) |
226
|
|
|
status = photo.set_date_taken(datetime(2013, 9, 30, 7, 6, 5)) |
227
|
|
|
|
228
|
|
|
assert status == True, status |
229
|
|
|
|
230
|
|
|
photo_new = Photo(origin) |
231
|
|
|
metadata = photo_new.get_metadata() |
232
|
|
|
|
233
|
|
|
date_taken = metadata['date_taken'] |
234
|
|
|
|
235
|
|
|
shutil.rmtree(folder) |
236
|
|
|
|
237
|
|
|
#assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken'] |
238
|
|
|
assert date_taken == helper.time_convert((2013, 9, 30, 7, 6, 5, 0, 273, 0)), metadata['date_taken'] |
239
|
|
|
|
240
|
|
View Code Duplication |
def test_set_location(): |
|
|
|
|
241
|
|
|
temporary_folder, folder = helper.create_working_folder() |
242
|
|
|
|
243
|
|
|
origin = '%s/photo.jpg' % folder |
244
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
245
|
|
|
|
246
|
|
|
photo = Photo(origin) |
247
|
|
|
origin_metadata = photo.get_metadata() |
248
|
|
|
|
249
|
|
|
# Verify that original photo has different location info that what we |
250
|
|
|
# will be setting and checking |
251
|
|
|
assert not helper.isclose(origin_metadata['latitude'], 11.1111111111), origin_metadata['latitude'] |
252
|
|
|
assert not helper.isclose(origin_metadata['longitude'], 99.9999999999), origin_metadata['longitude'] |
253
|
|
|
|
254
|
|
|
status = photo.set_location(11.1111111111, 99.9999999999) |
255
|
|
|
|
256
|
|
|
assert status == True, status |
257
|
|
|
|
258
|
|
|
photo_new = Photo(origin) |
259
|
|
|
metadata = photo_new.get_metadata() |
260
|
|
|
|
261
|
|
|
shutil.rmtree(folder) |
262
|
|
|
|
263
|
|
|
assert helper.isclose(metadata['latitude'], 11.1111111111), metadata['latitude'] |
264
|
|
|
assert helper.isclose(metadata['longitude'], 99.9999999999), metadata['longitude'] |
265
|
|
|
|
266
|
|
View Code Duplication |
def test_set_location_minus(): |
|
|
|
|
267
|
|
|
temporary_folder, folder = helper.create_working_folder() |
268
|
|
|
|
269
|
|
|
origin = '%s/photo.jpg' % folder |
270
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
271
|
|
|
|
272
|
|
|
photo = Photo(origin) |
273
|
|
|
origin_metadata = photo.get_metadata() |
274
|
|
|
|
275
|
|
|
# Verify that original photo has different location info that what we |
276
|
|
|
# will be setting and checking |
277
|
|
|
assert not helper.isclose(origin_metadata['latitude'], 11.1111111111), origin_metadata['latitude'] |
278
|
|
|
assert not helper.isclose(origin_metadata['longitude'], 99.9999999999), origin_metadata['longitude'] |
279
|
|
|
|
280
|
|
|
status = photo.set_location(-11.1111111111, -99.9999999999) |
281
|
|
|
|
282
|
|
|
assert status == True, status |
283
|
|
|
|
284
|
|
|
photo_new = Photo(origin) |
285
|
|
|
metadata = photo_new.get_metadata() |
286
|
|
|
|
287
|
|
|
shutil.rmtree(folder) |
288
|
|
|
|
289
|
|
|
assert helper.isclose(metadata['latitude'], -11.1111111111), metadata['latitude'] |
290
|
|
|
assert helper.isclose(metadata['longitude'], -99.9999999999), metadata['longitude'] |
291
|
|
|
|
292
|
|
View Code Duplication |
def test_set_title(): |
|
|
|
|
293
|
|
|
temporary_folder, folder = helper.create_working_folder() |
294
|
|
|
|
295
|
|
|
origin = '%s/photo.jpg' % folder |
296
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
297
|
|
|
|
298
|
|
|
photo = Photo(origin) |
299
|
|
|
origin_metadata = photo.get_metadata() |
300
|
|
|
|
301
|
|
|
status = photo.set_title('my photo title') |
302
|
|
|
|
303
|
|
|
assert status == True, status |
304
|
|
|
|
305
|
|
|
photo_new = Photo(origin) |
306
|
|
|
metadata = photo_new.get_metadata() |
307
|
|
|
|
308
|
|
|
shutil.rmtree(folder) |
309
|
|
|
|
310
|
|
|
assert metadata['title'] == 'my photo title', metadata['title'] |
311
|
|
|
|
312
|
|
View Code Duplication |
def test_set_title_non_ascii(): |
|
|
|
|
313
|
|
|
temporary_folder, folder = helper.create_working_folder() |
314
|
|
|
|
315
|
|
|
origin = '%s/photo.jpg' % folder |
316
|
|
|
shutil.copyfile(helper.get_file('plain.jpg'), origin) |
317
|
|
|
|
318
|
|
|
photo = Photo(origin) |
319
|
|
|
origin_metadata = photo.get_metadata() |
320
|
|
|
|
321
|
|
|
unicode_title = u'形声字 / 形聲字' |
322
|
|
|
|
323
|
|
|
status = photo.set_title(unicode_title) |
324
|
|
|
assert status == True, status |
325
|
|
|
|
326
|
|
|
photo_new = Photo(origin) |
327
|
|
|
metadata = photo_new.get_metadata() |
328
|
|
|
|
329
|
|
|
shutil.rmtree(folder) |
330
|
|
|
|
331
|
|
|
assert metadata['title'] == unicode_title, metadata['title'] |
332
|
|
|
|
333
|
|
|
# This is a test generator that will test reading and writing to |
334
|
|
|
# various RAW formats. Each sample file has a different date which |
335
|
|
|
# is the only information which needs to be added to run the tests |
336
|
|
|
# for that file type. |
337
|
|
|
# https://nose.readthedocs.io/en/latest/writing_tests.html#test-generators |
338
|
|
|
def test_various_types(): |
339
|
|
|
types = Photo.extensions |
340
|
|
|
#extensions = ('arw', 'cr2', 'dng', 'gif', 'jpeg', 'jpg', 'nef', 'rw2') |
341
|
|
|
dates = { |
342
|
|
|
'arw': (2007, 4, 8, 17, 41, 18, 6, 98, 0), |
343
|
|
|
'cr2': (2005, 10, 29, 16, 14, 44, 5, 302, 0), |
344
|
|
|
'dng': (2009, 10, 20, 9, 10, 46, 1, 293, 0), |
345
|
|
|
'heic': (2019, 5, 26, 10, 33, 20, 6, 146, 0), |
346
|
|
|
'nef': (2008, 10, 24, 9, 12, 56, 4, 298, 0), |
347
|
|
|
'rw2': (2014, 11, 19, 23, 7, 44, 2, 323, 0) |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
for type in types: |
351
|
|
|
if type in dates: |
352
|
|
|
yield (_test_photo_type_get, type, dates[type]) |
353
|
|
|
yield (_test_photo_type_set, type, dates[type]) |
354
|
|
|
|
355
|
|
|
def _test_photo_type_get(type, date): |
356
|
|
|
temporary_folder, folder = helper.create_working_folder() |
357
|
|
|
|
358
|
|
|
photo_name = 'photo.{}'.format(type) |
359
|
|
|
photo_file = helper.get_file(photo_name) |
360
|
|
|
origin = '{}/{}'.format(folder, photo_name) |
361
|
|
|
|
362
|
|
|
if not photo_file: |
363
|
|
|
photo_file = helper.download_file(photo_name, folder) |
364
|
|
|
if not photo_file or not os.path.isfile(photo_file): |
365
|
|
|
raise SkipTest('{} file not downlaoded'.format(type)) |
366
|
|
|
|
367
|
|
|
# downloading for each test is costly so we save it in the working directory |
368
|
|
|
file_path_save_as = helper.get_file_path(photo_name) |
369
|
|
|
if os.path.isfile(photo_file): |
370
|
|
|
shutil.copyfile(photo_file, file_path_save_as) |
371
|
|
|
|
372
|
|
|
shutil.copyfile(photo_file, origin) |
373
|
|
|
|
374
|
|
|
photo = Photo(origin) |
375
|
|
|
metadata = photo.get_metadata() |
376
|
|
|
|
377
|
|
|
shutil.rmtree(folder) |
378
|
|
|
|
379
|
|
|
assert metadata['date_taken'] == helper.time_convert(date), '{} date {}'.format(type, metadata['date_taken']) |
380
|
|
|
|
381
|
|
|
def _test_photo_type_set(type, date): |
382
|
|
|
temporary_folder, folder = helper.create_working_folder() |
383
|
|
|
|
384
|
|
|
photo_name = 'photo.{}'.format(type) |
385
|
|
|
photo_file = helper.get_file(photo_name) |
386
|
|
|
origin = '{}/{}'.format(folder, photo_name) |
387
|
|
|
|
388
|
|
|
if not photo_file: |
389
|
|
|
photo_file = helper.download_file(photo_name, folder) |
390
|
|
|
if not photo_file or not os.path.isfile(photo_file): |
391
|
|
|
raise SkipTest('{} file not downlaoded'.format(type)) |
392
|
|
|
|
393
|
|
|
shutil.copyfile(photo_file, origin) |
394
|
|
|
|
395
|
|
|
photo = Photo(origin) |
396
|
|
|
origin_metadata = photo.get_metadata() |
397
|
|
|
|
398
|
|
|
status = photo.set_location(11.1111111111, 99.9999999999) |
399
|
|
|
|
400
|
|
|
assert status == True, status |
401
|
|
|
|
402
|
|
|
photo_new = Photo(origin) |
403
|
|
|
metadata = photo_new.get_metadata() |
404
|
|
|
|
405
|
|
|
shutil.rmtree(folder) |
406
|
|
|
|
407
|
|
|
assert metadata['date_taken'] == helper.time_convert(date), '{} date {}'.format(type, metadata['date_taken']) |
408
|
|
|
assert helper.isclose(metadata['latitude'], 11.1111111111), '{} lat {}'.format(type, metadata['latitude']) |
409
|
|
|
assert helper.isclose(metadata['longitude'], 99.9999999999), '{} lon {}'.format(type, metadata['latitude']) |
410
|
|
|
|