Passed
Push — master ( 9ddb09...7fc5b5 )
by Jaisen
01:14
created

elodie/tests/filesystem_test.py (26 issues)

1
from __future__ import absolute_import
2
# Project imports
3
import mock
4
import os
5
import re
6
import shutil
7
import sys
8
import time
9
from datetime import datetime
10
from datetime import timedelta
11
from tempfile import gettempdir
12
13
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
14
15
from . import helper
16
from elodie.config import load_config
17
from elodie.filesystem import FileSystem
18
from elodie.media.text import Text
19
from elodie.media.media import Media
20
from elodie.media.photo import Photo
21
from elodie.media.video import Video
22
from nose.plugins.skip import SkipTest
23
24
os.environ['TZ'] = 'GMT'
25
26
27 View Code Duplication
def test_create_directory_success():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
28
    filesystem = FileSystem()
29
    folder = os.path.join(helper.temp_dir(), helper.random_string(10))
30
    status = filesystem.create_directory(folder)
31
32
    # Needs to be a subdirectory
33
    assert helper.temp_dir() != folder
34
35
    assert status == True
36
    assert os.path.isdir(folder) == True
37
    assert os.path.exists(folder) == True
38
39
    # Clean up
40
    shutil.rmtree(folder)
41
42
43 View Code Duplication
def test_create_directory_recursive_success():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
44
    filesystem = FileSystem()
45
    folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
46
    status = filesystem.create_directory(folder)
47
48
    # Needs to be a subdirectory
49
    assert helper.temp_dir() != folder
50
51
    assert status == True
52
    assert os.path.isdir(folder) == True
53
    assert os.path.exists(folder) == True
54
55
    shutil.rmtree(folder)
56
57
@mock.patch('elodie.filesystem.os.makedirs')
58
def test_create_directory_invalid_permissions(mock_makedirs):
59
    if os.name == 'nt':
60
       raise SkipTest("It isn't implemented on Windows")
61
62
    # Mock the case where makedirs raises an OSError because the user does
63
    # not have permission to create the given directory.
64
    mock_makedirs.side_effect = OSError()
65
66
    filesystem = FileSystem()
67
    status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')
68
69
    assert status == False
70
71
def test_delete_directory_if_empty():
72
    filesystem = FileSystem()
73
    folder = os.path.join(helper.temp_dir(), helper.random_string(10))
74
    os.makedirs(folder)
75
76
    assert os.path.isdir(folder) == True
77
    assert os.path.exists(folder) == True
78
79
    filesystem.delete_directory_if_empty(folder)
80
81
    assert os.path.isdir(folder) == False
82
    assert os.path.exists(folder) == False
83
84
def test_delete_directory_if_empty_when_not_empty():
85
    filesystem = FileSystem()
86
    folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
87
    os.makedirs(folder)
88
    parent_folder = os.path.dirname(folder)
89
90
    assert os.path.isdir(folder) == True
91
    assert os.path.exists(folder) == True
92
    assert os.path.isdir(parent_folder) == True
93
    assert os.path.exists(parent_folder) == True
94
95
    filesystem.delete_directory_if_empty(parent_folder)
96
97
    assert os.path.isdir(folder) == True
98
    assert os.path.exists(folder) == True
99
    assert os.path.isdir(parent_folder) == True
100
    assert os.path.exists(parent_folder) == True
101
102
    shutil.rmtree(parent_folder)
103
104
def test_get_all_files_success():
105
    filesystem = FileSystem()
106
    folder = helper.populate_folder(5)
107
108
    files = set()
109
    files.update(filesystem.get_all_files(folder))
110
    shutil.rmtree(folder)
111
112
    length = len(files)
113
    assert length == 5, files
114
115
def test_get_all_files_by_extension():
116
    filesystem = FileSystem()
117
    folder = helper.populate_folder(5)
118
119
    files = set()
120
    files.update(filesystem.get_all_files(folder))
121
    length = len(files)
122
    assert length == 5, length
123
124
    files = set()
125
    files.update(filesystem.get_all_files(folder, 'jpg'))
126
    length = len(files)
127
    assert length == 3, length
128
129
    files = set()
130
    files.update(filesystem.get_all_files(folder, 'txt'))
131
    length = len(files)
132
    assert length == 2, length
133
134
    files = set()
135
    files.update(filesystem.get_all_files(folder, 'gif'))
136
    length = len(files)
137
    assert length == 0, length
138
139
    shutil.rmtree(folder)
140
141
def test_get_all_files_with_only_invalid_file():
142
    filesystem = FileSystem()
143
    folder = helper.populate_folder(0, include_invalid=True)
144
145
    files = set()
146
    files.update(filesystem.get_all_files(folder))
147
    shutil.rmtree(folder)
148
149
    length = len(files)
150
    assert length == 0, length
151
152
def test_get_all_files_with_invalid_file():
153
    filesystem = FileSystem()
154
    folder = helper.populate_folder(5, include_invalid=True)
155
156
    files = set()
157
    files.update(filesystem.get_all_files(folder))
158
    shutil.rmtree(folder)
159
160
    length = len(files)
161
    assert length == 5, length
162
163
def test_get_all_files_for_loop():
164
    filesystem = FileSystem()
165
    folder = helper.populate_folder(5)
166
167
    files = set()
168
    files.update()
169
    counter = 0
170
    for file in filesystem.get_all_files(folder):
171
        counter += 1
172
    shutil.rmtree(folder)
173
174
    assert counter == 5, counter
175
176
def test_get_current_directory():
177
    filesystem = FileSystem()
178
    assert os.getcwd() == filesystem.get_current_directory()
179
180
def test_get_file_name_definition_default():
181
    filesystem = FileSystem()
182
    name_template, definition = filesystem.get_file_name_definition()
183
184
    assert name_template == '%date-%original_name-%title.%extension', name_template
185
    assert definition == [[('date', '%Y-%m-%d_%H-%M-%S')], [('original_name', '')], [('title', '')], [('extension', '')]], definition #noqa
186
187
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-filename' % gettempdir())
188
def test_get_file_name_definition_custom():
189
    with open('%s/config.ini-custom-filename' % gettempdir(), 'w') as f:
190
        f.write("""
191
[File]
192
date=%Y-%m-%b
193
name=%date-%original_name.%extension
194
        """)
195
    if hasattr(load_config, 'config'):
196
        del load_config.config
197
198
    filesystem = FileSystem()
199
    name_template, definition = filesystem.get_file_name_definition()
200
201
    if hasattr(load_config, 'config'):
202
        del load_config.config
203
204
    assert name_template == '%date-%original_name.%extension', name_template
205
    assert definition == [[('date', '%Y-%m-%b')], [('original_name', '')], [('extension', '')]], definition #noqa
206
207
def test_get_file_name_plain():
208
    filesystem = FileSystem()
209
    media = Photo(helper.get_file('plain.jpg'))
210
    file_name = filesystem.get_file_name(media)
211
212
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
213
214
def test_get_file_name_with_title():
215
    filesystem = FileSystem()
216
    media = Photo(helper.get_file('with-title.jpg'))
217
    file_name = filesystem.get_file_name(media)
218
219
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
220
221
def test_get_file_name_with_original_name_exif():
222
    filesystem = FileSystem()
223
    media = Photo(helper.get_file('with-filename-in-exif.jpg'))
224
    file_name = filesystem.get_file_name(media)
225
226
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar.jpg'), file_name
227
228
def test_get_file_name_with_original_name_title_exif():
229
    filesystem = FileSystem()
230
    media = Photo(helper.get_file('with-filename-and-title-in-exif.jpg'))
231
    file_name = filesystem.get_file_name(media)
232
233
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar-foobar-title.jpg'), file_name
234
235
def test_get_file_name_with_uppercase_and_spaces():
236
    filesystem = FileSystem()
237
    media = Photo(helper.get_file('Plain With Spaces And Uppercase 123.jpg'))
238
    file_name = filesystem.get_file_name(media)
239
240
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain-with-spaces-and-uppercase-123.jpg'), file_name
241
242 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
243
def test_get_file_name_custom():
244
    with open('%s/config.ini-filename-custom' % gettempdir(), 'w') as f:
245
        f.write("""
246
[File]
247
date=%Y-%m-%b
248
name=%date-%original_name.%extension
249
        """)
250
    if hasattr(load_config, 'config'):
251
        del load_config.config
252
253
    filesystem = FileSystem()
254
    media = Photo(helper.get_file('plain.jpg'))
255
    file_name = filesystem.get_file_name(media)
256
257
    if hasattr(load_config, 'config'):
258
        del load_config.config
259
260
    assert file_name == helper.path_tz_fix('2015-12-dec-plain.jpg'), file_name
261
262 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-title' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
263
def test_get_file_name_custom_with_title():
264
    with open('%s/config.ini-filename-custom-with-title' % gettempdir(), 'w') as f:
265
        f.write("""
266
[File]
267
date=%Y-%m-%d
268
name=%date-%original_name-%title.%extension
269
        """)
270
    if hasattr(load_config, 'config'):
271
        del load_config.config
272
273
    filesystem = FileSystem()
274
    media = Photo(helper.get_file('with-title.jpg'))
275
    file_name = filesystem.get_file_name(media)
276
277
    if hasattr(load_config, 'config'):
278
        del load_config.config
279
280
    assert file_name == helper.path_tz_fix('2015-12-05-with-title-some-title.jpg'), file_name
281
282 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-empty-value' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
283
def test_get_file_name_custom_with_empty_value():
284
    with open('%s/config.ini-filename-custom-with-empty-value' % gettempdir(), 'w') as f:
285
        f.write("""
286
[File]
287
date=%Y-%m-%d
288
name=%date-%original_name-%title.%extension
289
        """)
290
    if hasattr(load_config, 'config'):
291
        del load_config.config
292
293
    filesystem = FileSystem()
294
    media = Photo(helper.get_file('plain.jpg'))
295
    file_name = filesystem.get_file_name(media)
296
297
    if hasattr(load_config, 'config'):
298
        del load_config.config
299
300
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
301
302
def test_get_folder_path_plain():
303
    filesystem = FileSystem()
304
    media = Photo(helper.get_file('plain.jpg'))
305
    path = filesystem.get_folder_path(media.get_metadata())
306
307
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
308
309
def test_get_folder_path_with_title():
310
    filesystem = FileSystem()
311
    media = Photo(helper.get_file('with-title.jpg'))
312
    path = filesystem.get_folder_path(media.get_metadata())
313
314
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
315
316
def test_get_folder_path_with_location():
317
    filesystem = FileSystem()
318
    media = Photo(helper.get_file('with-location.jpg'))
319
    path = filesystem.get_folder_path(media.get_metadata())
320
321
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
322
323 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
324
def test_get_folder_path_with_camera_make_and_model():
325
    with open('%s/config.ini-original-with-camera-make-and-model' % gettempdir(), 'w') as f:
326
        f.write("""
327
[Directory]
328
full_path=%camera_make/%camera_model
329
        """)
330
    if hasattr(load_config, 'config'):
331
        del load_config.config
332
    filesystem = FileSystem()
333
    media = Photo(helper.get_file('plain.jpg'))
334
    path = filesystem.get_folder_path(media.get_metadata())
335
    if hasattr(load_config, 'config'):
336
        del load_config.config
337
338
    assert path == os.path.join('Canon', 'Canon EOS REBEL T2i'), path
339
340 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
341
def test_get_folder_path_with_camera_make_and_model_fallback():
342
    with open('%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir(), 'w') as f:
343
        f.write("""
344
[Directory]
345
full_path=%camera_make|"nomake"/%camera_model|"nomodel"
346
        """)
347
    if hasattr(load_config, 'config'):
348
        del load_config.config
349
    filesystem = FileSystem()
350
    media = Photo(helper.get_file('no-exif.jpg'))
351
    path = filesystem.get_folder_path(media.get_metadata())
352
    if hasattr(load_config, 'config'):
353
        del load_config.config
354
355
    assert path == os.path.join('nomake', 'nomodel'), path
356
357 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-int-in-component-path' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
358
def test_get_folder_path_with_int_in_config_component():
359
    # gh-239
360
    with open('%s/config.ini-int-in-component-path' % gettempdir(), 'w') as f:
361
        f.write("""
362
[Directory]
363
date=%Y
364
full_path=%date
365
        """)
366
    if hasattr(load_config, 'config'):
367
        del load_config.config
368
    filesystem = FileSystem()
369
    media = Photo(helper.get_file('plain.jpg'))
370
    path = filesystem.get_folder_path(media.get_metadata())
371
    if hasattr(load_config, 'config'):
372
        del load_config.config
373
374
    assert path == os.path.join('2015'), path
375
376
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-and-album' % gettempdir())
377
def test_get_folder_path_with_combined_date_and_album():
378
    # gh-239
379
    with open('%s/config.ini-combined-date-and-album' % gettempdir(), 'w') as f:
380
        f.write("""
381
[Directory]
382
date=%Y-%m-%b
383
custom=%date %album
384
full_path=%custom
385
        """)
386
    if hasattr(load_config, 'config'):
387
        del load_config.config
388
    filesystem = FileSystem()
389
    media = Photo(helper.get_file('with-album.jpg'))
390
    path = filesystem.get_folder_path(media.get_metadata())
391
    if hasattr(load_config, 'config'):
392
        del load_config.config
393
394
    assert path == '2015-12-Dec Test Album', path
395
396 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-album-location-fallback' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
397
def test_get_folder_path_with_album_and_location_fallback():
398
    # gh-279
399
    with open('%s/config.ini-combined-date-album-location-fallback' % gettempdir(), 'w') as f:
400
        f.write("""
401
[Directory]
402
date=%Y-%m-%b
403
custom=%album
404
full_path=%custom|%city
405
        """)
406
    if hasattr(load_config, 'config'):
407
        del load_config.config
408
    filesystem = FileSystem()
409
410
    # Test with no location
411
    media = Photo(helper.get_file('plain.jpg'))
412
    path_plain = filesystem.get_folder_path(media.get_metadata())
413
414
    # Test with City
415
    media = Photo(helper.get_file('with-location.jpg'))
416
    path_city = filesystem.get_folder_path(media.get_metadata())
417
    if hasattr(load_config, 'config'):
418
        del load_config.config
419
420
    assert path_plain == 'Unknown Location', path_plain
421
    assert path_city == 'Sunnyvale', path_city
422
423
424
def test_get_folder_path_with_int_in_source_path():
425
    # gh-239
426
    filesystem = FileSystem()
427
    temporary_folder, folder = helper.create_working_folder('int')
428
429
    origin = os.path.join(folder,'plain.jpg')
430
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
431
432
    media = Photo(origin)
433
    path = filesystem.get_folder_path(media.get_metadata())
434
435
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
436
437 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-default-unknown-location' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
438
def test_get_folder_path_with_original_default_unknown_location():
439
    with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f:
440
        f.write('')
441
    if hasattr(load_config, 'config'):
442
        del load_config.config
443
    filesystem = FileSystem()
444
    media = Photo(helper.get_file('plain.jpg'))
445
    path = filesystem.get_folder_path(media.get_metadata())
446
    if hasattr(load_config, 'config'):
447
        del load_config.config
448
449
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
450
451 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-path' % gettempdir())
452
def test_get_folder_path_with_custom_path():
453
    with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f:
454
        f.write("""
455
[MapQuest]
456
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
457
458
[Directory]
459
date=%Y-%m-%d
460
location=%country-%state-%city
461
full_path=%date/%location
462
        """)
463
    if hasattr(load_config, 'config'):
464
        del load_config.config
465
    filesystem = FileSystem()
466
    media = Photo(helper.get_file('with-location.jpg'))
467
    path = filesystem.get_folder_path(media.get_metadata())
468
    if hasattr(load_config, 'config'):
469
        del load_config.config
470
471
    assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
472
473 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
474
def test_get_folder_path_with_fallback_folder():
475
    with open('%s/config.ini-fallback' % gettempdir(), 'w') as f:
476
        f.write("""
477
[Directory]
478
year=%Y
479
month=%m
480
full_path=%year/%month/%album|%"No Album Fool"/%month
481
        """)
482
#full_path=%year/%album|"No Album"
483
    if hasattr(load_config, 'config'):
484
        del load_config.config
485
    filesystem = FileSystem()
486
    media = Photo(helper.get_file('plain.jpg'))
487
    path = filesystem.get_folder_path(media.get_metadata())
488
    if hasattr(load_config, 'config'):
489
        del load_config.config
490
491
    assert path == os.path.join('2015','12','No Album Fool','12'), path
492
493 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
494
def test_get_folder_path_with_with_more_than_two_levels():
495
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
496
        f.write("""
497
[MapQuest]
498
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
499
500
[Directory]
501
year=%Y
502
month=%m
503
location=%city, %state
504
full_path=%year/%month/%location
505
        """)
506
507
    if hasattr(load_config, 'config'):
508
        del load_config.config
509
510
    filesystem = FileSystem()
511
    media = Photo(helper.get_file('with-location.jpg'))
512
    path = filesystem.get_folder_path(media.get_metadata())
513
    if hasattr(load_config, 'config'):
514
        del load_config.config
515
516
    assert path == os.path.join('2015','12','Sunnyvale, California'), path
517
518 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
519
def test_get_folder_path_with_with_only_one_level():
520
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
521
        f.write("""
522
[Directory]
523
year=%Y
524
full_path=%year
525
        """)
526
527
    if hasattr(load_config, 'config'):
528
        del load_config.config
529
530
    filesystem = FileSystem()
531
    media = Photo(helper.get_file('plain.jpg'))
532
    path = filesystem.get_folder_path(media.get_metadata())
533
    if hasattr(load_config, 'config'):
534
        del load_config.config
535
536
    assert path == os.path.join('2015'), path
537
538
def test_get_folder_path_with_location_and_title():
539
    filesystem = FileSystem()
540
    media = Photo(helper.get_file('with-location-and-title.jpg'))
541
    path = filesystem.get_folder_path(media.get_metadata())
542
543
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
544
545 View Code Duplication
def test_parse_folder_name_default():
546
    if hasattr(load_config, 'config'):
547
        del load_config.config
548
    filesystem = FileSystem()
549
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
550
    mask = '%city'
551
    location_parts = re.findall('(%[^%]+)', mask)
552
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
553
    if hasattr(load_config, 'config'):
554
        del load_config.config
555
556
    assert path == 'Sunnyvale', path
557
558 View Code Duplication
def test_parse_folder_name_multiple():
559
    if hasattr(load_config, 'config'):
560
        del load_config.config
561
    filesystem = FileSystem()
562
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
563
    mask = '%city-%state-%country'
564
    location_parts = re.findall('(%[^%]+)', mask)
565
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
566
    if hasattr(load_config, 'config'):
567
        del load_config.config
568
569
    assert path == 'Sunnyvale-California-United States of America', path
570
571 View Code Duplication
def test_parse_folder_name_static_chars():
572
    if hasattr(load_config, 'config'):
573
        del load_config.config
574
    filesystem = FileSystem()
575
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
576
    mask = '%city-is-the-city'
577
    location_parts = re.findall('(%[^%]+)', mask)
578
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
579
    if hasattr(load_config, 'config'):
580
        del load_config.config
581
582
    assert path == 'Sunnyvale-is-the-city', path
583
584 View Code Duplication
def test_parse_folder_name_key_not_found():
585
    if hasattr(load_config, 'config'):
586
        del load_config.config
587
    filesystem = FileSystem()
588
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
589
    mask = '%city'
590
    location_parts = re.findall('(%[^%]+)', mask)
591
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
592
    if hasattr(load_config, 'config'):
593
        del load_config.config
594
595
    assert path == 'California', path
596
597 View Code Duplication
def test_parse_folder_name_key_not_found_with_static_chars():
598
    if hasattr(load_config, 'config'):
599
        del load_config.config
600
    filesystem = FileSystem()
601
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
602
    mask = '%city-is-not-found'
603
    location_parts = re.findall('(%[^%]+)', mask)
604
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
605
    if hasattr(load_config, 'config'):
606
        del load_config.config
607
608
    assert path == 'California', path
609
610 View Code Duplication
def test_parse_folder_name_multiple_keys_not_found():
611
    if hasattr(load_config, 'config'):
612
        del load_config.config
613
    filesystem = FileSystem()
614
    place_name = {'default': u'United States of America', 'country': u'United States of America'}
615
    mask = '%city-%state'
616
    location_parts = re.findall('(%[^%]+)', mask)
617
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
618
    if hasattr(load_config, 'config'):
619
        del load_config.config
620
621
    assert path == 'United States of America', path
622
623
def test_process_file_invalid():
624
    filesystem = FileSystem()
625
    temporary_folder, folder = helper.create_working_folder()
626
627
    origin = os.path.join(folder,'photo.jpg')
628
    shutil.copyfile(helper.get_file('invalid.jpg'), origin)
629
630
    media = Photo(origin)
631
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
632
633
    assert destination is None
634
635 View Code Duplication
def test_process_file_plain():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
636
    filesystem = FileSystem()
637
    temporary_folder, folder = helper.create_working_folder()
638
639
    origin = os.path.join(folder,'photo.jpg')
640
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
641
642
    origin_checksum_preprocess = helper.checksum(origin)
643
    media = Photo(origin)
644
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
645
646
    origin_checksum = helper.checksum(origin)
647
    destination_checksum = helper.checksum(destination)
648
649
    shutil.rmtree(folder)
650
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
651
652
    assert origin_checksum_preprocess is not None
653
    assert origin_checksum is not None
654
    assert destination_checksum is not None
655
    assert origin_checksum_preprocess == origin_checksum
656
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
657
658 View Code Duplication
def test_process_file_with_title():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
659
    filesystem = FileSystem()
660
    temporary_folder, folder = helper.create_working_folder()
661
662
    origin = '%s/photo.jpg' % folder
663
    shutil.copyfile(helper.get_file('with-title.jpg'), origin)
664
665
    origin_checksum_preprocess = helper.checksum(origin)
666
    media = Photo(origin)
667
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
668
669
    origin_checksum = helper.checksum(origin)
670
    destination_checksum = helper.checksum(destination)
671
672
    shutil.rmtree(folder)
673
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
674
675
    assert origin_checksum_preprocess is not None
676
    assert origin_checksum is not None
677
    assert destination_checksum is not None
678
    assert origin_checksum_preprocess == origin_checksum
679
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
680
681 View Code Duplication
def test_process_file_with_location():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
682
    filesystem = FileSystem()
683
    temporary_folder, folder = helper.create_working_folder()
684
685
    origin = os.path.join(folder,'photo.jpg')
686
    shutil.copyfile(helper.get_file('with-location.jpg'), origin)
687
688
    origin_checksum_preprocess = helper.checksum(origin)
689
    media = Photo(origin)
690
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
691
692
    origin_checksum = helper.checksum(origin)
693
    destination_checksum = helper.checksum(destination)
694
695
    shutil.rmtree(folder)
696
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
697
698
    assert origin_checksum_preprocess is not None
699
    assert origin_checksum is not None
700
    assert destination_checksum is not None
701
    assert origin_checksum_preprocess == origin_checksum
702
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
703
704
def test_process_file_validate_original_checksum():
705
    filesystem = FileSystem()
706
    temporary_folder, folder = helper.create_working_folder()
707
708
    origin = os.path.join(folder,'photo.jpg')
709
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
710
711
    origin_checksum_preprocess = helper.checksum(origin)
712
    media = Photo(origin)
713
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
714
715
    origin_checksum = helper.checksum(origin)
716
    destination_checksum = helper.checksum(destination)
717
718
    shutil.rmtree(folder)
719
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
720
721
    assert origin_checksum_preprocess is not None, origin_checksum_preprocess
722
    assert origin_checksum is not None, origin_checksum
723
    assert destination_checksum is not None, destination_checksum
724
    assert origin_checksum_preprocess == origin_checksum, (origin_checksum_preprocess, origin_checksum)
725
726
727 View Code Duplication
def test_process_file_with_location_and_title():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
728
    filesystem = FileSystem()
729
    temporary_folder, folder = helper.create_working_folder()
730
731
    origin = os.path.join(folder,'photo.jpg')
732
    shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
733
734
    origin_checksum_preprocess = helper.checksum(origin)
735
    media = Photo(origin)
736
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
737
738
    origin_checksum = helper.checksum(origin)
739
    destination_checksum = helper.checksum(destination)
740
741
    shutil.rmtree(folder)
742
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
743
744
    assert origin_checksum_preprocess is not None
745
    assert origin_checksum is not None
746
    assert destination_checksum is not None
747
    assert origin_checksum_preprocess == origin_checksum
748
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
749
750 View Code Duplication
def test_process_file_with_album():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
751
    filesystem = FileSystem()
752
    temporary_folder, folder = helper.create_working_folder()
753
754
    origin = os.path.join(folder,'photo.jpg')
755
    shutil.copyfile(helper.get_file('with-album.jpg'), origin)
756
757
    origin_checksum_preprocess = helper.checksum(origin)
758
    media = Photo(origin)
759
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
760
761
    origin_checksum = helper.checksum(origin)
762
    destination_checksum = helper.checksum(destination)
763
764
    shutil.rmtree(folder)
765
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
766
767
    assert origin_checksum_preprocess is not None
768
    assert origin_checksum is not None
769
    assert destination_checksum is not None
770
    assert origin_checksum_preprocess == origin_checksum
771
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
772
773 View Code Duplication
def test_process_file_with_album_and_title():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
774
    filesystem = FileSystem()
775
    temporary_folder, folder = helper.create_working_folder()
776
777
    origin = os.path.join(folder,'photo.jpg')
778
    shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin)
779
780
    origin_checksum_preprocess = helper.checksum(origin)
781
    media = Photo(origin)
782
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
783
784
    origin_checksum = helper.checksum(origin)
785
    destination_checksum = helper.checksum(destination)
786
787
    shutil.rmtree(folder)
788
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
789
790
    assert origin_checksum_preprocess is not None
791
    assert origin_checksum is not None
792
    assert destination_checksum is not None
793
    assert origin_checksum_preprocess == origin_checksum
794
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
795
796 View Code Duplication
def test_process_file_with_album_and_title_and_location():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
797
    filesystem = FileSystem()
798
    temporary_folder, folder = helper.create_working_folder()
799
800
    origin = os.path.join(folder,'photo.jpg')
801
    shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)
802
803
    origin_checksum_preprocess = helper.checksum(origin)
804
    media = Photo(origin)
805
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
806
807
    origin_checksum = helper.checksum(origin)
808
    destination_checksum = helper.checksum(destination)
809
810
    shutil.rmtree(folder)
811
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
812
813
    assert origin_checksum_preprocess is not None
814
    assert origin_checksum is not None
815
    assert destination_checksum is not None
816
    assert origin_checksum_preprocess == origin_checksum
817
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
818
819
# gh-89 (setting album then title reverts album)
820 View Code Duplication
def test_process_video_with_album_then_title():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
821
    filesystem = FileSystem()
822
    temporary_folder, folder = helper.create_working_folder()
823
824
    origin = os.path.join(folder,'movie.mov')
825
    shutil.copyfile(helper.get_file('video.mov'), origin)
826
827
    origin_checksum = helper.checksum(origin)
828
829
    origin_checksum_preprocess = helper.checksum(origin)
830
    media = Video(origin)
831
    media.set_album('test_album')
832
    media.set_title('test_title')
833
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
834
835
    destination_checksum = helper.checksum(destination)
836
837
    shutil.rmtree(folder)
838
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
839
840
    assert origin_checksum_preprocess is not None
841
    assert origin_checksum is not None
842
    assert destination_checksum is not None
843
    assert origin_checksum_preprocess == origin_checksum
844
    assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination
845
846
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir())
847
def test_process_file_fallback_folder():
848
    with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f:
849
        f.write("""
850
[Directory]
851
date=%Y-%m
852
full_path=%date/%album|"fallback"
853
        """)
854
855
    if hasattr(load_config, 'config'):
856
        del load_config.config
857
    filesystem = FileSystem()
858
    temporary_folder, folder = helper.create_working_folder()
859
860
    origin = os.path.join(folder,'plain.jpg')
861
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
862
863
    media = Photo(origin)
864
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
865
    if hasattr(load_config, 'config'):
866
        del load_config.config
867
868
    assert helper.path_tz_fix(os.path.join('2015-12', 'fallback', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
869
870
    shutil.rmtree(folder)
871
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
872
873
@mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir())
874
def test_process_twice_more_than_two_levels_of_directories():
875
    with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f:
876
        f.write("""
877
[Directory]
878
year=%Y
879
month=%m
880
day=%d
881
full_path=%year/%month/%day
882
        """)
883
884
    if hasattr(load_config, 'config'):
885
        del load_config.config
886
    filesystem = FileSystem()
887
    temporary_folder, folder = helper.create_working_folder()
888
889
    origin = os.path.join(folder,'plain.jpg')
890
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
891
892
    media = Photo(origin)
893
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
894
    if hasattr(load_config, 'config'):
895
        del load_config.config
896
897
    assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
898
899
    if hasattr(load_config, 'config'):
900
        del load_config.config
901
    media_second = Photo(destination)
902
    media_second.set_title('foo')
903
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)
904
    if hasattr(load_config, 'config'):
905
        del load_config.config
906
907
    assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second
908
909
    shutil.rmtree(folder)
910
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
911
912
def test_process_existing_file_without_changes():
913
    # gh-210
914
    filesystem = FileSystem()
915
    temporary_folder, folder = helper.create_working_folder()
916
917
    origin = os.path.join(folder,'plain.jpg')
918
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
919
920
    media = Photo(origin)
921
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
922
923
    assert helper.path_tz_fix(os.path.join('2015-12-Dec', 'Unknown Location', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
924
925
    media_second = Photo(destination)
926
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)
927
928
    assert destination_second is None, destination_second
929
930
    shutil.rmtree(folder)
931
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
932
933 View Code Duplication
def test_set_utime_with_exif_date():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
934
    filesystem = FileSystem()
935
    temporary_folder, folder = helper.create_working_folder()
936
937
    origin = os.path.join(folder,'photo.jpg')
938
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
939
940
    media_initial = Photo(origin)
941
    metadata_initial = media_initial.get_metadata()
942
943
    initial_stat = os.stat(origin)
944
    initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
945
    initial_checksum = helper.checksum(origin)
946
947
    assert initial_time != time.mktime(metadata_initial['date_taken'])
948
949
    filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path())
950
    final_stat = os.stat(origin)
951
    final_checksum = helper.checksum(origin)
952
953
    media_final = Photo(origin)
954
    metadata_final = media_final.get_metadata()
955
956
    shutil.rmtree(folder)
957
958
    assert initial_stat.st_mtime != final_stat.st_mtime
959
    assert final_stat.st_mtime == time.mktime(metadata_final['date_taken'])
960
    assert initial_checksum == final_checksum
961
962 View Code Duplication
def test_set_utime_without_exif_date():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
963
    filesystem = FileSystem()
964
    temporary_folder, folder = helper.create_working_folder()
965
966
    origin = os.path.join(folder,'photo.jpg')
967
    shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
968
969
    media_initial = Photo(origin)
970
    metadata_initial = media_initial.get_metadata()
971
972
    initial_stat = os.stat(origin)
973
    initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
974
    initial_checksum = helper.checksum(origin)
975
976
    assert initial_time == time.mktime(metadata_initial['date_taken'])
977
978
    filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path())
979
    final_stat = os.stat(origin)
980
    final_checksum = helper.checksum(origin)
981
982
    media_final = Photo(origin)
983
    metadata_final = media_final.get_metadata()
984
985
    shutil.rmtree(folder)
986
987
    assert initial_time == final_stat.st_mtime
988
    assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken']))
989
    assert initial_checksum == final_checksum
990
991
@mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir())
992
def test_get_folder_path_definition_default():
993
    if hasattr(load_config, 'config'):
994
        del load_config.config
995
    filesystem = FileSystem()
996
    path_definition = filesystem.get_folder_path_definition()
997
    if hasattr(load_config, 'config'):
998
        del load_config.config
999
1000
    assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition
1001
1002 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir())
1003
def test_get_folder_path_definition_date_location():
1004
    with open('%s/config.ini-date-location' % gettempdir(), 'w') as f:
1005
        f.write("""
1006
[Directory]
1007
date=%Y-%m-%d
1008
location=%country
1009
full_path=%date/%location
1010
        """)
1011
1012
    if hasattr(load_config, 'config'):
1013
        del load_config.config
1014
    filesystem = FileSystem()
1015
    path_definition = filesystem.get_folder_path_definition()
1016
    expected = [
1017
        [('date', '%Y-%m-%d')], [('location', '%country')]
1018
    ]
1019
    if hasattr(load_config, 'config'):
1020
        del load_config.config
1021
1022
    assert path_definition == expected, path_definition
1023
1024 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
1025
def test_get_folder_path_definition_location_date():
1026
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1027
        f.write("""
1028
[Directory]
1029
date=%Y-%m-%d
1030
location=%country
1031
full_path=%location/%date
1032
        """)
1033
1034
    if hasattr(load_config, 'config'):
1035
        del load_config.config
1036
    filesystem = FileSystem()
1037
    path_definition = filesystem.get_folder_path_definition()
1038
    expected = [
1039
        [('location', '%country')], [('date', '%Y-%m-%d')]
1040
    ]
1041
    if hasattr(load_config, 'config'):
1042
        del load_config.config
1043
1044
    assert path_definition == expected, path_definition
1045
1046
@mock.patch('elodie.config.config_file', '%s/config.ini-cached' % gettempdir())
1047
def test_get_folder_path_definition_cached():
1048
    with open('%s/config.ini-cached' % gettempdir(), 'w') as f:
1049
        f.write("""
1050
[Directory]
1051
date=%Y-%m-%d
1052
location=%country
1053
full_path=%date/%location
1054
        """)
1055
1056
    if hasattr(load_config, 'config'):
1057
        del load_config.config
1058
    filesystem = FileSystem()
1059
    path_definition = filesystem.get_folder_path_definition()
1060
    expected = [
1061
        [('date', '%Y-%m-%d')], [('location', '%country')]
1062
    ]
1063
1064
    assert path_definition == expected, path_definition
1065
1066
    with open('%s/config.ini-cached' % gettempdir(), 'w') as f:
1067
        f.write("""
1068
[Directory]
1069
date=%uncached
1070
location=%uncached
1071
full_path=%date/%location
1072
        """)
1073
    if hasattr(load_config, 'config'):
1074
        del load_config.config
1075
    filesystem = FileSystem()
1076
    path_definition = filesystem.get_folder_path_definition()
1077
    expected = [
1078
        [('date', '%Y-%m-%d')], [('location', '%country')]
1079
    ]
1080
    if hasattr(load_config, 'config'):
1081
        del load_config.config
1082
1083 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
1084
def test_get_folder_path_definition_with_more_than_two_levels():
1085
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1086
        f.write("""
1087
[Directory]
1088
year=%Y
1089
month=%m
1090
day=%d
1091
full_path=%year/%month/%day
1092
        """)
1093
1094
    if hasattr(load_config, 'config'):
1095
        del load_config.config
1096
    filesystem = FileSystem()
1097
    path_definition = filesystem.get_folder_path_definition()
1098
    expected = [
1099
        [('year', '%Y')], [('month', '%m')], [('day', '%d')]
1100
    ]
1101
    if hasattr(load_config, 'config'):
1102
        del load_config.config
1103
1104
    assert path_definition == expected, path_definition
1105
1106 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
1107
def test_get_folder_path_definition_with_only_one_level():
1108
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1109
        f.write("""
1110
[Directory]
1111
year=%Y
1112
full_path=%year
1113
        """)
1114
1115
    if hasattr(load_config, 'config'):
1116
        del load_config.config
1117
    filesystem = FileSystem()
1118
    path_definition = filesystem.get_folder_path_definition()
1119
    expected = [
1120
        [('year', '%Y')]
1121
    ]
1122
    if hasattr(load_config, 'config'):
1123
        del load_config.config
1124
1125
    assert path_definition == expected, path_definition
1126
1127 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-multi-level-custom' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
1128
def test_get_folder_path_definition_multi_level_custom():
1129
    with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f:
1130
        f.write("""
1131
[Directory]
1132
year=%Y
1133
month=%M
1134
full_path=%year/%album|%month|%"foo"/%month
1135
        """)
1136
1137
    if hasattr(load_config, 'config'):
1138
        del load_config.config
1139
    filesystem = FileSystem()
1140
    path_definition = filesystem.get_folder_path_definition()
1141
    
1142
    expected = [[('year', '%Y')], [('album', ''), ('month', '%M'), ('"foo"', '')], [('month', '%M')]]
1143
    if hasattr(load_config, 'config'):
1144
        del load_config.config
1145
1146
    assert path_definition == expected, path_definition
1147