Passed
Push — master ( 75e659...d8cee1 )
by Jaisen
01:58
created

test_process_file_fallback_folder()   A

Complexity

Conditions 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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