Passed
Push — master ( 6a4c21...3ad6c0 )
by Jaisen
02:19
created

elodie/tests/filesystem_test.py (1 issue)

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():
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():
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.get_metadata())
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.get_metadata())
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.get_metadata())
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.get_metadata())
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.get_metadata())
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())
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.get_metadata())
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())
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.get_metadata())
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())
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.get_metadata())
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 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-lowercase' % gettempdir())
303
def test_get_file_name_custom_with_lower_capitalization():
304
    with open('%s/config.ini-filename-custom-with-lowercase' % gettempdir(), 'w') as f:
305
        f.write("""
306
[File]
307
date=%Y-%m-%d
308
name=%date-%original_name-%title.%extension
309
capitalization=lower
310
        """)
311
    if hasattr(load_config, 'config'):
312
        del load_config.config
313
314
    filesystem = FileSystem()
315
    media = Photo(helper.get_file('plain.jpg'))
316
    file_name = filesystem.get_file_name(media.get_metadata())
317
318
    if hasattr(load_config, 'config'):
319
        del load_config.config
320
321
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
322
323 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-invalidcase' % gettempdir())
324
def test_get_file_name_custom_with_invalid_capitalization():
325
    with open('%s/config.ini-filename-custom-with-invalidcase' % gettempdir(), 'w') as f:
326
        f.write("""
327
[File]
328
date=%Y-%m-%d
329
name=%date-%original_name-%title.%extension
330
capitalization=garabage
331
        """)
332
    if hasattr(load_config, 'config'):
333
        del load_config.config
334
335
    filesystem = FileSystem()
336
    media = Photo(helper.get_file('plain.jpg'))
337
    file_name = filesystem.get_file_name(media.get_metadata())
338
339
    if hasattr(load_config, 'config'):
340
        del load_config.config
341
342
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
343
344 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-uppercase' % gettempdir())
345
def test_get_file_name_custom_with_upper_capitalization():
346
    with open('%s/config.ini-filename-custom-with-uppercase' % gettempdir(), 'w') as f:
347
        f.write("""
348
[File]
349
date=%Y-%m-%d
350
name=%date-%original_name-%title.%extension
351
capitalization=upper
352
        """)
353
    if hasattr(load_config, 'config'):
354
        del load_config.config
355
356
    filesystem = FileSystem()
357
    media = Photo(helper.get_file('plain.jpg'))
358
    file_name = filesystem.get_file_name(media.get_metadata())
359
360
    if hasattr(load_config, 'config'):
361
        del load_config.config
362
363
    assert file_name == helper.path_tz_fix('2015-12-05-PLAIN.JPG'), file_name
364
365
def test_get_folder_path_plain():
366
    filesystem = FileSystem()
367
    media = Photo(helper.get_file('plain.jpg'))
368
    path = filesystem.get_folder_path(media.get_metadata())
369
370
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
371
372
def test_get_folder_path_with_title():
373
    filesystem = FileSystem()
374
    media = Photo(helper.get_file('with-title.jpg'))
375
    path = filesystem.get_folder_path(media.get_metadata())
376
377
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
378
379
def test_get_folder_path_with_location():
380
    filesystem = FileSystem()
381
    media = Photo(helper.get_file('with-location.jpg'))
382
    path = filesystem.get_folder_path(media.get_metadata())
383
384
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
385
386 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model' % gettempdir())
387
def test_get_folder_path_with_camera_make_and_model():
388
    with open('%s/config.ini-original-with-camera-make-and-model' % gettempdir(), 'w') as f:
389
        f.write("""
390
[Directory]
391
full_path=%camera_make/%camera_model
392
        """)
393
    if hasattr(load_config, 'config'):
394
        del load_config.config
395
    filesystem = FileSystem()
396
    media = Photo(helper.get_file('plain.jpg'))
397
    path = filesystem.get_folder_path(media.get_metadata())
398
    if hasattr(load_config, 'config'):
399
        del load_config.config
400
401
    assert path == os.path.join('Canon', 'Canon EOS REBEL T2i'), path
402
403 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir())
404
def test_get_folder_path_with_camera_make_and_model_fallback():
405
    with open('%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir(), 'w') as f:
406
        f.write("""
407
[Directory]
408
full_path=%camera_make|"nomake"/%camera_model|"nomodel"
409
        """)
410
    if hasattr(load_config, 'config'):
411
        del load_config.config
412
    filesystem = FileSystem()
413
    media = Photo(helper.get_file('no-exif.jpg'))
414
    path = filesystem.get_folder_path(media.get_metadata())
415
    if hasattr(load_config, 'config'):
416
        del load_config.config
417
418
    assert path == os.path.join('nomake', 'nomodel'), path
419
420 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-int-in-component-path' % gettempdir())
421
def test_get_folder_path_with_int_in_config_component():
422
    # gh-239
423
    with open('%s/config.ini-int-in-component-path' % gettempdir(), 'w') as f:
424
        f.write("""
425
[Directory]
426
date=%Y
427
full_path=%date
428
        """)
429
    if hasattr(load_config, 'config'):
430
        del load_config.config
431
    filesystem = FileSystem()
432
    media = Photo(helper.get_file('plain.jpg'))
433
    path = filesystem.get_folder_path(media.get_metadata())
434
    if hasattr(load_config, 'config'):
435
        del load_config.config
436
437
    assert path == os.path.join('2015'), path
438
439 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-and-album' % gettempdir())
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
440
def test_get_folder_path_with_combined_date_and_album():
441
    # gh-239
442
    with open('%s/config.ini-combined-date-and-album' % gettempdir(), 'w') as f:
443
        f.write("""
444
[Directory]
445
date=%Y-%m-%b
446
custom=%date %album
447
full_path=%custom
448
        """)
449
    if hasattr(load_config, 'config'):
450
        del load_config.config
451
    filesystem = FileSystem()
452
    media = Photo(helper.get_file('with-album.jpg'))
453
    path = filesystem.get_folder_path(media.get_metadata())
454
    if hasattr(load_config, 'config'):
455
        del load_config.config
456
457
    assert path == '2015-12-Dec Test Album', path
458
459
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-album-location-fallback' % gettempdir())
460
def test_get_folder_path_with_album_and_location_fallback():
461
    # gh-279
462
    with open('%s/config.ini-combined-date-album-location-fallback' % gettempdir(), 'w') as f:
463
        f.write("""
464
[Directory]
465
date=%Y-%m-%b
466
custom=%album
467
full_path=%custom|%city
468
        """)
469
    if hasattr(load_config, 'config'):
470
        del load_config.config
471
    filesystem = FileSystem()
472
473
    # Test with no location
474
    media = Photo(helper.get_file('plain.jpg'))
475
    path_plain = filesystem.get_folder_path(media.get_metadata())
476
477
    # Test with City
478
    media = Photo(helper.get_file('with-location.jpg'))
479
    path_city = filesystem.get_folder_path(media.get_metadata())
480
    if hasattr(load_config, 'config'):
481
        del load_config.config
482
483
    assert path_plain == 'Unknown Location', path_plain
484
    assert path_city == 'Sunnyvale', path_city
485
486
487
def test_get_folder_path_with_int_in_source_path():
488
    # gh-239
489
    filesystem = FileSystem()
490
    temporary_folder, folder = helper.create_working_folder('int')
491
492
    origin = os.path.join(folder,'plain.jpg')
493
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
494
495
    media = Photo(origin)
496
    path = filesystem.get_folder_path(media.get_metadata())
497
498
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
499
500 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-default-unknown-location' % gettempdir())
501
def test_get_folder_path_with_original_default_unknown_location():
502
    with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f:
503
        f.write('')
504
    if hasattr(load_config, 'config'):
505
        del load_config.config
506
    filesystem = FileSystem()
507
    media = Photo(helper.get_file('plain.jpg'))
508
    path = filesystem.get_folder_path(media.get_metadata())
509
    if hasattr(load_config, 'config'):
510
        del load_config.config
511
512
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
513
514 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-path' % gettempdir())
515
def test_get_folder_path_with_custom_path():
516
    with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f:
517
        f.write("""
518
[MapQuest]
519
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
520
521
[Directory]
522
date=%Y-%m-%d
523
location=%country-%state-%city
524
full_path=%date/%location
525
        """)
526
    if hasattr(load_config, 'config'):
527
        del load_config.config
528
    filesystem = FileSystem()
529
    media = Photo(helper.get_file('with-location.jpg'))
530
    path = filesystem.get_folder_path(media.get_metadata())
531
    if hasattr(load_config, 'config'):
532
        del load_config.config
533
534
    assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
535
536
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir())
537
def test_get_folder_path_with_fallback_folder():
538
    with open('%s/config.ini-fallback' % gettempdir(), 'w') as f:
539
        f.write("""
540
[Directory]
541
year=%Y
542
month=%m
543
full_path=%year/%month/%album|%"No Album Fool"/%month
544
        """)
545
#full_path=%year/%album|"No Album"
546
    if hasattr(load_config, 'config'):
547
        del load_config.config
548
    filesystem = FileSystem()
549
    media = Photo(helper.get_file('plain.jpg'))
550
    path = filesystem.get_folder_path(media.get_metadata())
551
    if hasattr(load_config, 'config'):
552
        del load_config.config
553
554
    assert path == os.path.join('2015','12','No Album Fool','12'), path
555
556 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
557
def test_get_folder_path_with_with_more_than_two_levels():
558
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
559
        f.write("""
560
[MapQuest]
561
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
562
563
[Directory]
564
year=%Y
565
month=%m
566
location=%city, %state
567
full_path=%year/%month/%location
568
        """)
569
570
    if hasattr(load_config, 'config'):
571
        del load_config.config
572
573
    filesystem = FileSystem()
574
    media = Photo(helper.get_file('with-location.jpg'))
575
    path = filesystem.get_folder_path(media.get_metadata())
576
    if hasattr(load_config, 'config'):
577
        del load_config.config
578
579
    assert path == os.path.join('2015','12','Sunnyvale, California'), path
580
581 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
582
def test_get_folder_path_with_with_only_one_level():
583
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
584
        f.write("""
585
[Directory]
586
year=%Y
587
full_path=%year
588
        """)
589
590
    if hasattr(load_config, 'config'):
591
        del load_config.config
592
593
    filesystem = FileSystem()
594
    media = Photo(helper.get_file('plain.jpg'))
595
    path = filesystem.get_folder_path(media.get_metadata())
596
    if hasattr(load_config, 'config'):
597
        del load_config.config
598
599
    assert path == os.path.join('2015'), path
600
601
def test_get_folder_path_with_location_and_title():
602
    filesystem = FileSystem()
603
    media = Photo(helper.get_file('with-location-and-title.jpg'))
604
    path = filesystem.get_folder_path(media.get_metadata())
605
606
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
607
608 View Code Duplication
def test_parse_folder_name_default():
609
    if hasattr(load_config, 'config'):
610
        del load_config.config
611
    filesystem = FileSystem()
612
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
613
    mask = '%city'
614
    location_parts = re.findall('(%[^%]+)', mask)
615
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
616
    if hasattr(load_config, 'config'):
617
        del load_config.config
618
619
    assert path == 'Sunnyvale', path
620
621 View Code Duplication
def test_parse_folder_name_multiple():
622
    if hasattr(load_config, 'config'):
623
        del load_config.config
624
    filesystem = FileSystem()
625
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
626
    mask = '%city-%state-%country'
627
    location_parts = re.findall('(%[^%]+)', mask)
628
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
629
    if hasattr(load_config, 'config'):
630
        del load_config.config
631
632
    assert path == 'Sunnyvale-California-United States of America', path
633
634 View Code Duplication
def test_parse_folder_name_static_chars():
635
    if hasattr(load_config, 'config'):
636
        del load_config.config
637
    filesystem = FileSystem()
638
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
639
    mask = '%city-is-the-city'
640
    location_parts = re.findall('(%[^%]+)', mask)
641
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
642
    if hasattr(load_config, 'config'):
643
        del load_config.config
644
645
    assert path == 'Sunnyvale-is-the-city', path
646
647 View Code Duplication
def test_parse_folder_name_key_not_found():
648
    if hasattr(load_config, 'config'):
649
        del load_config.config
650
    filesystem = FileSystem()
651
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
652
    mask = '%city'
653
    location_parts = re.findall('(%[^%]+)', mask)
654
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
655
    if hasattr(load_config, 'config'):
656
        del load_config.config
657
658
    assert path == 'California', path
659
660 View Code Duplication
def test_parse_folder_name_key_not_found_with_static_chars():
661
    if hasattr(load_config, 'config'):
662
        del load_config.config
663
    filesystem = FileSystem()
664
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
665
    mask = '%city-is-not-found'
666
    location_parts = re.findall('(%[^%]+)', mask)
667
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
668
    if hasattr(load_config, 'config'):
669
        del load_config.config
670
671
    assert path == 'California', path
672
673 View Code Duplication
def test_parse_folder_name_multiple_keys_not_found():
674
    if hasattr(load_config, 'config'):
675
        del load_config.config
676
    filesystem = FileSystem()
677
    place_name = {'default': u'United States of America', 'country': u'United States of America'}
678
    mask = '%city-%state'
679
    location_parts = re.findall('(%[^%]+)', mask)
680
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
681
    if hasattr(load_config, 'config'):
682
        del load_config.config
683
684
    assert path == 'United States of America', path
685
686
def test_process_file_invalid():
687
    filesystem = FileSystem()
688
    temporary_folder, folder = helper.create_working_folder()
689
690
    origin = os.path.join(folder,'photo.jpg')
691
    shutil.copyfile(helper.get_file('invalid.jpg'), origin)
692
693
    media = Photo(origin)
694
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
695
696
    assert destination is None
697
698 View Code Duplication
def test_process_file_plain():
699
    filesystem = FileSystem()
700
    temporary_folder, folder = helper.create_working_folder()
701
702
    origin = os.path.join(folder,'photo.jpg')
703
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
704
705
    origin_checksum_preprocess = helper.checksum(origin)
706
    media = Photo(origin)
707
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
708
709
    origin_checksum = helper.checksum(origin)
710
    destination_checksum = helper.checksum(destination)
711
712
    shutil.rmtree(folder)
713
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
714
715
    assert origin_checksum_preprocess is not None
716
    assert origin_checksum is not None
717
    assert destination_checksum is not None
718
    assert origin_checksum_preprocess == origin_checksum
719
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
720
721 View Code Duplication
def test_process_file_with_title():
722
    filesystem = FileSystem()
723
    temporary_folder, folder = helper.create_working_folder()
724
725
    origin = '%s/photo.jpg' % folder
726
    shutil.copyfile(helper.get_file('with-title.jpg'), origin)
727
728
    origin_checksum_preprocess = helper.checksum(origin)
729
    media = Photo(origin)
730
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
731
732
    origin_checksum = helper.checksum(origin)
733
    destination_checksum = helper.checksum(destination)
734
735
    shutil.rmtree(folder)
736
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
737
738
    assert origin_checksum_preprocess is not None
739
    assert origin_checksum is not None
740
    assert destination_checksum is not None
741
    assert origin_checksum_preprocess == origin_checksum
742
    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
743
744 View Code Duplication
def test_process_file_with_location():
745
    filesystem = FileSystem()
746
    temporary_folder, folder = helper.create_working_folder()
747
748
    origin = os.path.join(folder,'photo.jpg')
749
    shutil.copyfile(helper.get_file('with-location.jpg'), origin)
750
751
    origin_checksum_preprocess = helper.checksum(origin)
752
    media = Photo(origin)
753
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
754
755
    origin_checksum = helper.checksum(origin)
756
    destination_checksum = helper.checksum(destination)
757
758
    shutil.rmtree(folder)
759
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
760
761
    assert origin_checksum_preprocess is not None
762
    assert origin_checksum is not None
763
    assert destination_checksum is not None
764
    assert origin_checksum_preprocess == origin_checksum
765
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
766
767
def test_process_file_validate_original_checksum():
768
    filesystem = FileSystem()
769
    temporary_folder, folder = helper.create_working_folder()
770
771
    origin = os.path.join(folder,'photo.jpg')
772
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
773
774
    origin_checksum_preprocess = helper.checksum(origin)
775
    media = Photo(origin)
776
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
777
778
    origin_checksum = helper.checksum(origin)
779
    destination_checksum = helper.checksum(destination)
780
781
    shutil.rmtree(folder)
782
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
783
784
    assert origin_checksum_preprocess is not None, origin_checksum_preprocess
785
    assert origin_checksum is not None, origin_checksum
786
    assert destination_checksum is not None, destination_checksum
787
    assert origin_checksum_preprocess == origin_checksum, (origin_checksum_preprocess, origin_checksum)
788
789
790
# See https://github.com/jmathai/elodie/issues/330
791
def test_process_file_no_exif_date_is_correct_gh_330():
792
    filesystem = FileSystem()
793
    temporary_folder, folder = helper.create_working_folder()
794
795
    origin = os.path.join(folder,'photo.jpg')
796
    shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
797
798
    atime = 1330712100
799
    utime = 1330712900
800
    os.utime(origin, (atime, utime))
801
802
    media = Photo(origin)
803
    metadata = media.get_metadata()
804
805
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
806
807
    shutil.rmtree(folder)
808
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
809
810
    assert '/2012-03-Mar/' in destination, destination
811
    assert '/2012-03-02_18-28-20' in destination, destination
812
813 View Code Duplication
def test_process_file_with_location_and_title():
814
    filesystem = FileSystem()
815
    temporary_folder, folder = helper.create_working_folder()
816
817
    origin = os.path.join(folder,'photo.jpg')
818
    shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
819
820
    origin_checksum_preprocess = helper.checksum(origin)
821
    media = Photo(origin)
822
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
823
824
    origin_checksum = helper.checksum(origin)
825
    destination_checksum = helper.checksum(destination)
826
827
    shutil.rmtree(folder)
828
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
829
830
    assert origin_checksum_preprocess is not None
831
    assert origin_checksum is not None
832
    assert destination_checksum is not None
833
    assert origin_checksum_preprocess == origin_checksum
834
    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
835
836 View Code Duplication
def test_process_file_with_album():
837
    filesystem = FileSystem()
838
    temporary_folder, folder = helper.create_working_folder()
839
840
    origin = os.path.join(folder,'photo.jpg')
841
    shutil.copyfile(helper.get_file('with-album.jpg'), origin)
842
843
    origin_checksum_preprocess = helper.checksum(origin)
844
    media = Photo(origin)
845
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
846
847
    origin_checksum = helper.checksum(origin)
848
    destination_checksum = helper.checksum(destination)
849
850
    shutil.rmtree(folder)
851
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
852
853
    assert origin_checksum_preprocess is not None
854
    assert origin_checksum is not None
855
    assert destination_checksum is not None
856
    assert origin_checksum_preprocess == origin_checksum
857
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
858
859 View Code Duplication
def test_process_file_with_album_and_title():
860
    filesystem = FileSystem()
861
    temporary_folder, folder = helper.create_working_folder()
862
863
    origin = os.path.join(folder,'photo.jpg')
864
    shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin)
865
866
    origin_checksum_preprocess = helper.checksum(origin)
867
    media = Photo(origin)
868
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
869
870
    origin_checksum = helper.checksum(origin)
871
    destination_checksum = helper.checksum(destination)
872
873
    shutil.rmtree(folder)
874
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
875
876
    assert origin_checksum_preprocess is not None
877
    assert origin_checksum is not None
878
    assert destination_checksum is not None
879
    assert origin_checksum_preprocess == origin_checksum
880
    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
881
882 View Code Duplication
def test_process_file_with_album_and_title_and_location():
883
    filesystem = FileSystem()
884
    temporary_folder, folder = helper.create_working_folder()
885
886
    origin = os.path.join(folder,'photo.jpg')
887
    shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)
888
889
    origin_checksum_preprocess = helper.checksum(origin)
890
    media = Photo(origin)
891
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
892
893
    origin_checksum = helper.checksum(origin)
894
    destination_checksum = helper.checksum(destination)
895
896
    shutil.rmtree(folder)
897
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
898
899
    assert origin_checksum_preprocess is not None
900
    assert origin_checksum is not None
901
    assert destination_checksum is not None
902
    assert origin_checksum_preprocess == origin_checksum
903
    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
904
905
# gh-89 (setting album then title reverts album)
906 View Code Duplication
def test_process_video_with_album_then_title():
907
    filesystem = FileSystem()
908
    temporary_folder, folder = helper.create_working_folder()
909
910
    origin = os.path.join(folder,'movie.mov')
911
    shutil.copyfile(helper.get_file('video.mov'), origin)
912
913
    origin_checksum = helper.checksum(origin)
914
915
    origin_checksum_preprocess = helper.checksum(origin)
916
    media = Video(origin)
917
    media.set_album('test_album')
918
    media.set_title('test_title')
919
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
920
921
    destination_checksum = helper.checksum(destination)
922
923
    shutil.rmtree(folder)
924
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
925
926
    assert origin_checksum_preprocess is not None
927
    assert origin_checksum is not None
928
    assert destination_checksum is not None
929
    assert origin_checksum_preprocess == origin_checksum
930
    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
931
932
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir())
933
def test_process_file_fallback_folder():
934
    with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f:
935
        f.write("""
936
[Directory]
937
date=%Y-%m
938
full_path=%date/%album|"fallback"
939
        """)
940
941
    if hasattr(load_config, 'config'):
942
        del load_config.config
943
    filesystem = FileSystem()
944
    temporary_folder, folder = helper.create_working_folder()
945
946
    origin = os.path.join(folder,'plain.jpg')
947
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
948
949
    media = Photo(origin)
950
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
951
    if hasattr(load_config, 'config'):
952
        del load_config.config
953
954
    assert helper.path_tz_fix(os.path.join('2015-12', 'fallback', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
955
956
    shutil.rmtree(folder)
957
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
958
959
@mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir())
960
def test_process_twice_more_than_two_levels_of_directories():
961
    with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f:
962
        f.write("""
963
[Directory]
964
year=%Y
965
month=%m
966
day=%d
967
full_path=%year/%month/%day
968
        """)
969
970
    if hasattr(load_config, 'config'):
971
        del load_config.config
972
973
    filesystem = FileSystem()
974
    temporary_folder, folder = helper.create_working_folder()
975
976
    origin = os.path.join(folder,'plain.jpg')
977
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
978
979
    media = Photo(origin)
980
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
981
    if hasattr(load_config, 'config'):
982
        del load_config.config
983
984
    assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
985
986
    if hasattr(load_config, 'config'):
987
        del load_config.config
988
989
    media_second = Photo(destination)
990
    media_second.set_title('foo')
991
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)
992
993
    if hasattr(load_config, 'config'):
994
        del load_config.config
995
996
    assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second
997
998
    shutil.rmtree(folder)
999
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
1000
1001
def test_process_existing_file_without_changes():
1002
    # gh-210
1003
    filesystem = FileSystem()
1004
    temporary_folder, folder = helper.create_working_folder()
1005
1006
    origin = os.path.join(folder,'plain.jpg')
1007
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
1008
1009
    media = Photo(origin)
1010
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
1011
1012
    assert helper.path_tz_fix(os.path.join('2015-12-Dec', 'Unknown Location', '2015-12-05_00-59-26-plain.jpg')) in destination, destination
1013
1014
    media_second = Photo(destination)
1015
    destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True)
1016
1017
    assert destination_second is None, destination_second
1018
1019
    shutil.rmtree(folder)
1020
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
1021
1022 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-plugin-throw-error' % gettempdir())
1023
def test_process_file_with_plugin_throw_error():
1024
    with open('%s/config.ini-plugin-throw-error' % gettempdir(), 'w') as f:
1025
        f.write("""
1026
[Plugins]
1027
plugins=ThrowError
1028
        """)
1029
1030
    if hasattr(load_config, 'config'):
1031
        del load_config.config
1032
1033
    filesystem = FileSystem()
1034
    temporary_folder, folder = helper.create_working_folder()
1035
1036
    origin = os.path.join(folder,'plain.jpg')
1037
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
1038
1039
    media = Photo(origin)
1040
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
1041
1042
    if hasattr(load_config, 'config'):
1043
        del load_config.config
1044
1045
    assert destination is None, destination
1046
1047 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-plugin-runtime-error' % gettempdir())
1048
def test_process_file_with_plugin_runtime_error():
1049
    with open('%s/config.ini-plugin-runtime-error' % gettempdir(), 'w') as f:
1050
        f.write("""
1051
[Plugins]
1052
plugins=RuntimeError
1053
        """)
1054
    if hasattr(load_config, 'config'):
1055
        del load_config.config
1056
1057
    filesystem = FileSystem()
1058
    temporary_folder, folder = helper.create_working_folder()
1059
1060
    origin = os.path.join(folder,'plain.jpg')
1061
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
1062
1063
    media = Photo(origin)
1064
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
1065
1066
    if hasattr(load_config, 'config'):
1067
        del load_config.config
1068
1069
    assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination, destination
1070
1071 View Code Duplication
def test_set_utime_with_exif_date():
1072
    filesystem = FileSystem()
1073
    temporary_folder, folder = helper.create_working_folder()
1074
1075
    origin = os.path.join(folder,'photo.jpg')
1076
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
1077
1078
    media_initial = Photo(origin)
1079
    metadata_initial = media_initial.get_metadata()
1080
1081
    initial_stat = os.stat(origin)
1082
    initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
1083
    initial_checksum = helper.checksum(origin)
1084
1085
    assert initial_time != time.mktime(metadata_initial['date_taken'])
1086
1087
    filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path())
1088
    final_stat = os.stat(origin)
1089
    final_checksum = helper.checksum(origin)
1090
1091
    media_final = Photo(origin)
1092
    metadata_final = media_final.get_metadata()
1093
1094
    shutil.rmtree(folder)
1095
1096
    assert initial_stat.st_mtime != final_stat.st_mtime
1097
    assert final_stat.st_mtime == time.mktime(metadata_final['date_taken'])
1098
    assert initial_checksum == final_checksum
1099
1100 View Code Duplication
def test_set_utime_without_exif_date():
1101
    filesystem = FileSystem()
1102
    temporary_folder, folder = helper.create_working_folder()
1103
1104
    origin = os.path.join(folder,'photo.jpg')
1105
    shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
1106
1107
    media_initial = Photo(origin)
1108
    metadata_initial = media_initial.get_metadata()
1109
1110
    initial_stat = os.stat(origin)
1111
    initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime))
1112
    initial_checksum = helper.checksum(origin)
1113
1114
    assert initial_time == time.mktime(metadata_initial['date_taken'])
1115
1116
    filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path())
1117
    final_stat = os.stat(origin)
1118
    final_checksum = helper.checksum(origin)
1119
1120
    media_final = Photo(origin)
1121
    metadata_final = media_final.get_metadata()
1122
1123
    shutil.rmtree(folder)
1124
1125
    assert initial_time == final_stat.st_mtime
1126
    assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken']))
1127
    assert initial_checksum == final_checksum
1128
1129
@mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir())
1130
def test_get_folder_path_definition_default():
1131
    if hasattr(load_config, 'config'):
1132
        del load_config.config
1133
    filesystem = FileSystem()
1134
    path_definition = filesystem.get_folder_path_definition()
1135
    if hasattr(load_config, 'config'):
1136
        del load_config.config
1137
1138
    assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition
1139
1140 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir())
1141
def test_get_folder_path_definition_date_location():
1142
    with open('%s/config.ini-date-location' % gettempdir(), 'w') as f:
1143
        f.write("""
1144
[Directory]
1145
date=%Y-%m-%d
1146
location=%country
1147
full_path=%date/%location
1148
        """)
1149
1150
    if hasattr(load_config, 'config'):
1151
        del load_config.config
1152
    filesystem = FileSystem()
1153
    path_definition = filesystem.get_folder_path_definition()
1154
    expected = [
1155
        [('date', '%Y-%m-%d')], [('location', '%country')]
1156
    ]
1157
    if hasattr(load_config, 'config'):
1158
        del load_config.config
1159
1160
    assert path_definition == expected, path_definition
1161
1162 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
1163
def test_get_folder_path_definition_location_date():
1164
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1165
        f.write("""
1166
[Directory]
1167
date=%Y-%m-%d
1168
location=%country
1169
full_path=%location/%date
1170
        """)
1171
1172
    if hasattr(load_config, 'config'):
1173
        del load_config.config
1174
    filesystem = FileSystem()
1175
    path_definition = filesystem.get_folder_path_definition()
1176
    expected = [
1177
        [('location', '%country')], [('date', '%Y-%m-%d')]
1178
    ]
1179
    if hasattr(load_config, 'config'):
1180
        del load_config.config
1181
1182
    assert path_definition == expected, path_definition
1183
1184
@mock.patch('elodie.config.config_file', '%s/config.ini-cached' % gettempdir())
1185
def test_get_folder_path_definition_cached():
1186
    with open('%s/config.ini-cached' % gettempdir(), 'w') as f:
1187
        f.write("""
1188
[Directory]
1189
date=%Y-%m-%d
1190
location=%country
1191
full_path=%date/%location
1192
        """)
1193
1194
    if hasattr(load_config, 'config'):
1195
        del load_config.config
1196
    filesystem = FileSystem()
1197
    path_definition = filesystem.get_folder_path_definition()
1198
    expected = [
1199
        [('date', '%Y-%m-%d')], [('location', '%country')]
1200
    ]
1201
1202
    assert path_definition == expected, path_definition
1203
1204
    with open('%s/config.ini-cached' % gettempdir(), 'w') as f:
1205
        f.write("""
1206
[Directory]
1207
date=%uncached
1208
location=%uncached
1209
full_path=%date/%location
1210
        """)
1211
    if hasattr(load_config, 'config'):
1212
        del load_config.config
1213
    filesystem = FileSystem()
1214
    path_definition = filesystem.get_folder_path_definition()
1215
    expected = [
1216
        [('date', '%Y-%m-%d')], [('location', '%country')]
1217
    ]
1218
    if hasattr(load_config, 'config'):
1219
        del load_config.config
1220
1221 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
1222
def test_get_folder_path_definition_with_more_than_two_levels():
1223
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1224
        f.write("""
1225
[Directory]
1226
year=%Y
1227
month=%m
1228
day=%d
1229
full_path=%year/%month/%day
1230
        """)
1231
1232
    if hasattr(load_config, 'config'):
1233
        del load_config.config
1234
    filesystem = FileSystem()
1235
    path_definition = filesystem.get_folder_path_definition()
1236
    expected = [
1237
        [('year', '%Y')], [('month', '%m')], [('day', '%d')]
1238
    ]
1239
    if hasattr(load_config, 'config'):
1240
        del load_config.config
1241
1242
    assert path_definition == expected, path_definition
1243
1244 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
1245
def test_get_folder_path_definition_with_only_one_level():
1246
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
1247
        f.write("""
1248
[Directory]
1249
year=%Y
1250
full_path=%year
1251
        """)
1252
1253
    if hasattr(load_config, 'config'):
1254
        del load_config.config
1255
    filesystem = FileSystem()
1256
    path_definition = filesystem.get_folder_path_definition()
1257
    expected = [
1258
        [('year', '%Y')]
1259
    ]
1260
    if hasattr(load_config, 'config'):
1261
        del load_config.config
1262
1263
    assert path_definition == expected, path_definition
1264
1265 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-multi-level-custom' % gettempdir())
1266
def test_get_folder_path_definition_multi_level_custom():
1267
    with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f:
1268
        f.write("""
1269
[Directory]
1270
year=%Y
1271
month=%M
1272
full_path=%year/%album|%month|%"foo"/%month
1273
        """)
1274
1275
    if hasattr(load_config, 'config'):
1276
        del load_config.config
1277
    filesystem = FileSystem()
1278
    path_definition = filesystem.get_folder_path_definition()
1279
    
1280
    expected = [[('year', '%Y')], [('album', ''), ('month', '%M'), ('"foo"', '')], [('month', '%M')]]
1281
    if hasattr(load_config, 'config'):
1282
        del load_config.config
1283
1284
    assert path_definition == expected, path_definition
1285