elodie.tests.filesystem_test   F
last analyzed

Complexity

Total Complexity 179

Size/Duplication

Total Lines 1327
Duplicated Lines 56.37 %

Importance

Changes 0
Metric Value
eloc 889
dl 748
loc 1327
rs 1.711
c 0
b 0
f 0
wmc 179

78 Functions

Rating   Name   Duplication   Size   Complexity  
A test_get_file_name_custom_with_invalid_capitalization() 20 20 4
A test_should_exclude_with_multiple_with_one_matching_regex() 0 4 1
A test_parse_folder_name_key_not_found() 12 12 3
A test_get_file_name_definition_custom() 0 19 4
A test_get_file_name_definition_default() 0 6 1
A test_get_folder_path_with_location_and_title() 0 6 1
A test_process_file_with_plugin_runtime_error() 23 23 4
A test_process_file_fallback_folder() 0 26 4
A test_get_file_name_with_original_name_exif() 0 6 1
A test_get_file_name_custom() 19 19 4
A test_process_file_plain() 22 22 1
A test_get_folder_path_definition_multi_level_custom() 20 20 4
A test_get_all_files_success() 0 10 1
A test_create_directory_invalid_permissions() 0 13 2
A test_get_folder_path_definition_default() 0 10 3
A test_get_file_name_custom_with_title() 19 19 4
A test_process_file_with_location() 22 22 1
A test_delete_directory_if_empty() 0 12 1
A test_get_current_directory() 0 3 1
A test_parse_folder_name_multiple() 12 12 3
A test_get_folder_path_with_with_more_than_two_levels() 24 24 4
A test_get_folder_path_definition_date_location() 21 21 4
A test_get_file_name_with_original_name_title_exif() 0 6 1
A test_should_exclude_with_matching_regex() 0 4 1
A test_get_folder_path_with_int_in_config_component() 18 18 4
A test_get_folder_path_with_original_default_unknown_location() 13 13 4
A test_create_directory_recursive_success() 13 13 1
A test_get_folder_path_with_combined_date_and_album() 19 19 4
A test_get_folder_path_with_with_only_one_level() 19 19 4
A test_get_folder_path_with_location() 0 6 1
A teardown_module() 0 2 1
A test_get_folder_path_definition_with_more_than_two_levels() 22 22 4
B test_get_folder_path_definition_cached() 0 36 6
A test_get_file_name_custom_with_upper_capitalization() 20 20 4
A test_get_folder_path_with_album_and_location_fallback() 0 26 4
A setup_module() 0 6 1
A test_process_file_with_plugin_throw_error() 24 24 4
A test_parse_folder_name_multiple_keys_not_found() 12 12 3
A test_get_file_name_custom_with_lower_capitalization() 20 20 4
A test_should_exclude_with_no_exclude_arg() 0 4 1
A test_process_file_no_exif_date_is_correct_gh_330() 0 21 1
A test_delete_directory_if_empty_when_not_empty() 0 19 1
A test_process_file_with_album() 22 22 1
A test_get_folder_path_with_camera_make_and_model_fallback() 16 16 4
A test_get_file_name_plain() 0 6 1
A test_get_folder_path_with_custom_path() 21 21 4
B test_process_twice_more_than_two_levels_of_directories() 0 41 6
A test_should_exclude_with_complex_matching_regex() 0 4 1
A test_process_file_invalid() 0 11 1
A test_parse_folder_name_key_not_found_with_static_chars() 12 12 3
A test_get_folder_path_definition_with_only_one_level() 20 20 4
A test_process_video_with_album_then_title() 25 25 1
A test_get_folder_path_plain() 0 6 1
A test_set_utime_without_exif_date() 28 28 1
A test_process_file_with_album_and_title_and_location() 22 22 1
A test_get_folder_path_with_camera_make_and_model() 16 16 4
A test_process_existing_file_without_changes() 0 20 1
A test_get_file_name_custom_with_empty_value() 19 19 4
A test_process_file_validate_original_checksum() 0 21 1
A test_process_file_with_album_and_title() 22 22 1
A test_process_file_with_location_and_title() 22 22 1
A test_set_utime_with_exif_date() 28 28 1
A test_process_file_with_title() 22 22 1
A test_get_folder_path_with_title() 0 6 1
A test_create_directory_success() 14 14 1
A test_get_folder_path_with_fallback_folder() 0 19 4
A test_should_exclude_with_non_matching_regex() 0 4 1
A test_parse_folder_name_default() 12 12 3
A test_get_file_name_with_title() 0 6 1
A test_get_all_files_for_loop() 0 12 2
A test_should_not_exclude_with_multiple_with_non_matching_regex() 0 4 1
A test_get_folder_path_with_int_in_source_path() 0 12 1
A test_parse_folder_name_static_chars() 12 12 3
A test_get_file_name_with_uppercase_and_spaces() 0 6 1
A test_get_folder_path_definition_location_date() 21 21 4
A test_get_all_files_with_only_invalid_file() 0 10 1
A test_get_all_files_with_invalid_file() 0 10 1
A test_get_all_files_by_extension() 0 25 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like elodie.tests.filesystem_test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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','US-CA-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, CA'), 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'CA', 'country': u'US', 'state': u'CA', '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'CA', 'country': u'US', 'state': u'CA', '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-CA-US', 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'CA', 'country': u'US', 'state': u'CA', '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'CA', 'country': u'US', 'state': u'CA'}
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 == 'CA', 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'CA', 'country': u'US', 'state': u'CA'}
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 == 'CA', 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'US', 'country': u'US'}
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 == 'US', 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