elodie.tests.filesystem_test   F
last analyzed

Complexity

Total Complexity 171

Size/Duplication

Total Lines 1285
Duplicated Lines 58.21 %

Importance

Changes 0
Metric Value
eloc 855
dl 748
loc 1285
rs 1.745
c 0
b 0
f 0
wmc 171

70 Functions

Rating   Name   Duplication   Size   Complexity  
A test_get_file_name_definition_custom() 0 19 4
A test_get_file_name_definition_default() 0 6 1
A test_get_file_name_with_original_name_exif() 0 6 1
A test_get_file_name_custom() 19 19 4
A test_create_directory_invalid_permissions() 0 13 2
A test_get_all_files_success() 0 10 1
A test_get_file_name_custom_with_title() 19 19 4
A test_delete_directory_if_empty() 0 12 1
A test_get_current_directory() 0 3 1
A test_get_file_name_with_original_name_title_exif() 0 6 1
A test_create_directory_recursive_success() 13 13 1
A test_delete_directory_if_empty_when_not_empty() 0 19 1
A test_get_file_name_plain() 0 6 1
A test_get_file_name_custom_with_empty_value() 19 19 4
A test_create_directory_success() 14 14 1
A test_get_file_name_with_title() 0 6 1
A test_get_all_files_for_loop() 0 12 2
A test_get_file_name_with_uppercase_and_spaces() 0 6 1
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
A test_get_file_name_custom_with_invalid_capitalization() 20 20 4
A test_parse_folder_name_key_not_found() 12 12 3
A test_get_folder_path_with_location_and_title() 0 6 1
A test_process_file_plain() 22 22 1
A test_process_file_with_location() 22 22 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_with_int_in_config_component() 18 18 4
A test_get_folder_path_with_original_default_unknown_location() 13 13 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 test_get_file_name_custom_with_upper_capitalization() 20 20 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_get_folder_path_with_camera_make_and_model_fallback() 16 16 4
A test_get_folder_path_with_custom_path() 21 21 4
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_plain() 0 6 1
A test_get_folder_path_with_camera_make_and_model() 16 16 4
A test_process_file_validate_original_checksum() 0 21 1
A test_process_file_with_title() 22 22 1
A test_get_folder_path_with_title() 0 6 1
A test_parse_folder_name_default() 12 12 3
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_process_file_with_plugin_runtime_error() 23 23 4
A test_process_file_fallback_folder() 0 26 4
A test_get_folder_path_definition_multi_level_custom() 20 20 4
A test_get_folder_path_definition_default() 0 10 3
A test_get_folder_path_definition_date_location() 21 21 4
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_process_file_with_plugin_throw_error() 24 24 4
A test_process_file_no_exif_date_is_correct_gh_330() 0 21 1
A test_process_file_with_album() 22 22 1
B test_process_twice_more_than_two_levels_of_directories() 0 41 6
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_set_utime_without_exif_date() 28 28 1
A test_process_file_with_album_and_title_and_location() 22 22 1
A test_process_existing_file_without_changes() 0 20 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_get_folder_path_definition_location_date() 21 21 4
A test_get_folder_path_with_combined_date_and_album() 19 19 4
A test_get_folder_path_with_album_and_location_fallback() 0 26 4
A test_get_folder_path_with_fallback_folder() 0 19 4

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
24
os.environ['TZ'] = 'GMT'
25
26
27 View Code Duplication
def test_create_directory_success():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
28
    filesystem = FileSystem()
29
    folder = os.path.join(helper.temp_dir(), helper.random_string(10))
30
    status = filesystem.create_directory(folder)
31
32
    # Needs to be a subdirectory
33
    assert helper.temp_dir() != folder
34
35
    assert status == True
36
    assert os.path.isdir(folder) == True
37
    assert os.path.exists(folder) == True
38
39
    # Clean up
40
    shutil.rmtree(folder)
41
42
43 View Code Duplication
def test_create_directory_recursive_success():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
44
    filesystem = FileSystem()
45
    folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
46
    status = filesystem.create_directory(folder)
47
48
    # Needs to be a subdirectory
49
    assert helper.temp_dir() != folder
50
51
    assert status == True
52
    assert os.path.isdir(folder) == True
53
    assert os.path.exists(folder) == True
54
55
    shutil.rmtree(folder)
56
57
@mock.patch('elodie.filesystem.os.makedirs')
58
def test_create_directory_invalid_permissions(mock_makedirs):
59
    if os.name == 'nt':
60
       raise SkipTest("It isn't implemented on Windows")
61
62
    # Mock the case where makedirs raises an OSError because the user does
63
    # not have permission to create the given directory.
64
    mock_makedirs.side_effect = OSError()
65
66
    filesystem = FileSystem()
67
    status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist')
68
69
    assert status == False
70
71
def test_delete_directory_if_empty():
72
    filesystem = FileSystem()
73
    folder = os.path.join(helper.temp_dir(), helper.random_string(10))
74
    os.makedirs(folder)
75
76
    assert os.path.isdir(folder) == True
77
    assert os.path.exists(folder) == True
78
79
    filesystem.delete_directory_if_empty(folder)
80
81
    assert os.path.isdir(folder) == False
82
    assert os.path.exists(folder) == False
83
84
def test_delete_directory_if_empty_when_not_empty():
85
    filesystem = FileSystem()
86
    folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10))
87
    os.makedirs(folder)
88
    parent_folder = os.path.dirname(folder)
89
90
    assert os.path.isdir(folder) == True
91
    assert os.path.exists(folder) == True
92
    assert os.path.isdir(parent_folder) == True
93
    assert os.path.exists(parent_folder) == True
94
95
    filesystem.delete_directory_if_empty(parent_folder)
96
97
    assert os.path.isdir(folder) == True
98
    assert os.path.exists(folder) == True
99
    assert os.path.isdir(parent_folder) == True
100
    assert os.path.exists(parent_folder) == True
101
102
    shutil.rmtree(parent_folder)
103
104
def test_get_all_files_success():
105
    filesystem = FileSystem()
106
    folder = helper.populate_folder(5)
107
108
    files = set()
109
    files.update(filesystem.get_all_files(folder))
110
    shutil.rmtree(folder)
111
112
    length = len(files)
113
    assert length == 5, files
114
115
def test_get_all_files_by_extension():
116
    filesystem = FileSystem()
117
    folder = helper.populate_folder(5)
118
119
    files = set()
120
    files.update(filesystem.get_all_files(folder))
121
    length = len(files)
122
    assert length == 5, length
123
124
    files = set()
125
    files.update(filesystem.get_all_files(folder, 'jpg'))
126
    length = len(files)
127
    assert length == 3, length
128
129
    files = set()
130
    files.update(filesystem.get_all_files(folder, 'txt'))
131
    length = len(files)
132
    assert length == 2, length
133
134
    files = set()
135
    files.update(filesystem.get_all_files(folder, 'gif'))
136
    length = len(files)
137
    assert length == 0, length
138
139
    shutil.rmtree(folder)
140
141
def test_get_all_files_with_only_invalid_file():
142
    filesystem = FileSystem()
143
    folder = helper.populate_folder(0, include_invalid=True)
144
145
    files = set()
146
    files.update(filesystem.get_all_files(folder))
147
    shutil.rmtree(folder)
148
149
    length = len(files)
150
    assert length == 0, length
151
152
def test_get_all_files_with_invalid_file():
153
    filesystem = FileSystem()
154
    folder = helper.populate_folder(5, include_invalid=True)
155
156
    files = set()
157
    files.update(filesystem.get_all_files(folder))
158
    shutil.rmtree(folder)
159
160
    length = len(files)
161
    assert length == 5, length
162
163
def test_get_all_files_for_loop():
164
    filesystem = FileSystem()
165
    folder = helper.populate_folder(5)
166
167
    files = set()
168
    files.update()
169
    counter = 0
170
    for file in filesystem.get_all_files(folder):
171
        counter += 1
172
    shutil.rmtree(folder)
173
174
    assert counter == 5, counter
175
176
def test_get_current_directory():
177
    filesystem = FileSystem()
178
    assert os.getcwd() == filesystem.get_current_directory()
179
180
def test_get_file_name_definition_default():
181
    filesystem = FileSystem()
182
    name_template, definition = filesystem.get_file_name_definition()
183
184
    assert name_template == '%date-%original_name-%title.%extension', name_template
185
    assert definition == [[('date', '%Y-%m-%d_%H-%M-%S')], [('original_name', '')], [('title', '')], [('extension', '')]], definition #noqa
186
187
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-filename' % gettempdir())
188
def test_get_file_name_definition_custom():
189
    with open('%s/config.ini-custom-filename' % gettempdir(), 'w') as f:
190
        f.write("""
191
[File]
192
date=%Y-%m-%b
193
name=%date-%original_name.%extension
194
        """)
195
    if hasattr(load_config, 'config'):
196
        del load_config.config
197
198
    filesystem = FileSystem()
199
    name_template, definition = filesystem.get_file_name_definition()
200
201
    if hasattr(load_config, 'config'):
202
        del load_config.config
203
204
    assert name_template == '%date-%original_name.%extension', name_template
205
    assert definition == [[('date', '%Y-%m-%b')], [('original_name', '')], [('extension', '')]], definition #noqa
206
207
def test_get_file_name_plain():
208
    filesystem = FileSystem()
209
    media = Photo(helper.get_file('plain.jpg'))
210
    file_name = filesystem.get_file_name(media.get_metadata())
211
212
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name
213
214
def test_get_file_name_with_title():
215
    filesystem = FileSystem()
216
    media = Photo(helper.get_file('with-title.jpg'))
217
    file_name = filesystem.get_file_name(media.get_metadata())
218
219
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name
220
221
def test_get_file_name_with_original_name_exif():
222
    filesystem = FileSystem()
223
    media = Photo(helper.get_file('with-filename-in-exif.jpg'))
224
    file_name = filesystem.get_file_name(media.get_metadata())
225
226
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar.jpg'), file_name
227
228
def test_get_file_name_with_original_name_title_exif():
229
    filesystem = FileSystem()
230
    media = Photo(helper.get_file('with-filename-and-title-in-exif.jpg'))
231
    file_name = filesystem.get_file_name(media.get_metadata())
232
233
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar-foobar-title.jpg'), file_name
234
235
def test_get_file_name_with_uppercase_and_spaces():
236
    filesystem = FileSystem()
237
    media = Photo(helper.get_file('Plain With Spaces And Uppercase 123.jpg'))
238
    file_name = filesystem.get_file_name(media.get_metadata())
239
240
    assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain-with-spaces-and-uppercase-123.jpg'), file_name
241
242 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
243
def test_get_file_name_custom():
244
    with open('%s/config.ini-filename-custom' % gettempdir(), 'w') as f:
245
        f.write("""
246
[File]
247
date=%Y-%m-%b
248
name=%date-%original_name.%extension
249
        """)
250
    if hasattr(load_config, 'config'):
251
        del load_config.config
252
253
    filesystem = FileSystem()
254
    media = Photo(helper.get_file('plain.jpg'))
255
    file_name = filesystem.get_file_name(media.get_metadata())
256
257
    if hasattr(load_config, 'config'):
258
        del load_config.config
259
260
    assert file_name == helper.path_tz_fix('2015-12-dec-plain.jpg'), file_name
261
262 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-title' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
263
def test_get_file_name_custom_with_title():
264
    with open('%s/config.ini-filename-custom-with-title' % gettempdir(), 'w') as f:
265
        f.write("""
266
[File]
267
date=%Y-%m-%d
268
name=%date-%original_name-%title.%extension
269
        """)
270
    if hasattr(load_config, 'config'):
271
        del load_config.config
272
273
    filesystem = FileSystem()
274
    media = Photo(helper.get_file('with-title.jpg'))
275
    file_name = filesystem.get_file_name(media.get_metadata())
276
277
    if hasattr(load_config, 'config'):
278
        del load_config.config
279
280
    assert file_name == helper.path_tz_fix('2015-12-05-with-title-some-title.jpg'), file_name
281
282 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-empty-value' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
283
def test_get_file_name_custom_with_empty_value():
284
    with open('%s/config.ini-filename-custom-with-empty-value' % gettempdir(), 'w') as f:
285
        f.write("""
286
[File]
287
date=%Y-%m-%d
288
name=%date-%original_name-%title.%extension
289
        """)
290
    if hasattr(load_config, 'config'):
291
        del load_config.config
292
293
    filesystem = FileSystem()
294
    media = Photo(helper.get_file('plain.jpg'))
295
    file_name = filesystem.get_file_name(media.get_metadata())
296
297
    if hasattr(load_config, 'config'):
298
        del load_config.config
299
300
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
301
302 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-lowercase' % gettempdir())
0 ignored issues
show
Duplication introduced by Andrew DeMaria
This code seems to be duplicated in your project.
Loading history...
303
def test_get_file_name_custom_with_lower_capitalization():
304
    with open('%s/config.ini-filename-custom-with-lowercase' % gettempdir(), 'w') as f:
305
        f.write("""
306
[File]
307
date=%Y-%m-%d
308
name=%date-%original_name-%title.%extension
309
capitalization=lower
310
        """)
311
    if hasattr(load_config, 'config'):
312
        del load_config.config
313
314
    filesystem = FileSystem()
315
    media = Photo(helper.get_file('plain.jpg'))
316
    file_name = filesystem.get_file_name(media.get_metadata())
317
318
    if hasattr(load_config, 'config'):
319
        del load_config.config
320
321
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
322
323 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-invalidcase' % gettempdir())
0 ignored issues
show
Duplication introduced by Andrew DeMaria
This code seems to be duplicated in your project.
Loading history...
324
def test_get_file_name_custom_with_invalid_capitalization():
325
    with open('%s/config.ini-filename-custom-with-invalidcase' % gettempdir(), 'w') as f:
326
        f.write("""
327
[File]
328
date=%Y-%m-%d
329
name=%date-%original_name-%title.%extension
330
capitalization=garabage
331
        """)
332
    if hasattr(load_config, 'config'):
333
        del load_config.config
334
335
    filesystem = FileSystem()
336
    media = Photo(helper.get_file('plain.jpg'))
337
    file_name = filesystem.get_file_name(media.get_metadata())
338
339
    if hasattr(load_config, 'config'):
340
        del load_config.config
341
342
    assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
343
344 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-uppercase' % gettempdir())
0 ignored issues
show
Duplication introduced by Andrew DeMaria
This code seems to be duplicated in your project.
Loading history...
345
def test_get_file_name_custom_with_upper_capitalization():
346
    with open('%s/config.ini-filename-custom-with-uppercase' % gettempdir(), 'w') as f:
347
        f.write("""
348
[File]
349
date=%Y-%m-%d
350
name=%date-%original_name-%title.%extension
351
capitalization=upper
352
        """)
353
    if hasattr(load_config, 'config'):
354
        del load_config.config
355
356
    filesystem = FileSystem()
357
    media = Photo(helper.get_file('plain.jpg'))
358
    file_name = filesystem.get_file_name(media.get_metadata())
359
360
    if hasattr(load_config, 'config'):
361
        del load_config.config
362
363
    assert file_name == helper.path_tz_fix('2015-12-05-PLAIN.JPG'), file_name
364
365
def test_get_folder_path_plain():
366
    filesystem = FileSystem()
367
    media = Photo(helper.get_file('plain.jpg'))
368
    path = filesystem.get_folder_path(media.get_metadata())
369
370
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
371
372
def test_get_folder_path_with_title():
373
    filesystem = FileSystem()
374
    media = Photo(helper.get_file('with-title.jpg'))
375
    path = filesystem.get_folder_path(media.get_metadata())
376
377
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
378
379
def test_get_folder_path_with_location():
380
    filesystem = FileSystem()
381
    media = Photo(helper.get_file('with-location.jpg'))
382
    path = filesystem.get_folder_path(media.get_metadata())
383
384
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
385
386 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
387
def test_get_folder_path_with_camera_make_and_model():
388
    with open('%s/config.ini-original-with-camera-make-and-model' % gettempdir(), 'w') as f:
389
        f.write("""
390
[Directory]
391
full_path=%camera_make/%camera_model
392
        """)
393
    if hasattr(load_config, 'config'):
394
        del load_config.config
395
    filesystem = FileSystem()
396
    media = Photo(helper.get_file('plain.jpg'))
397
    path = filesystem.get_folder_path(media.get_metadata())
398
    if hasattr(load_config, 'config'):
399
        del load_config.config
400
401
    assert path == os.path.join('Canon', 'Canon EOS REBEL T2i'), path
402
403 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
404
def test_get_folder_path_with_camera_make_and_model_fallback():
405
    with open('%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir(), 'w') as f:
406
        f.write("""
407
[Directory]
408
full_path=%camera_make|"nomake"/%camera_model|"nomodel"
409
        """)
410
    if hasattr(load_config, 'config'):
411
        del load_config.config
412
    filesystem = FileSystem()
413
    media = Photo(helper.get_file('no-exif.jpg'))
414
    path = filesystem.get_folder_path(media.get_metadata())
415
    if hasattr(load_config, 'config'):
416
        del load_config.config
417
418
    assert path == os.path.join('nomake', 'nomodel'), path
419
420 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-int-in-component-path' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
421
def test_get_folder_path_with_int_in_config_component():
422
    # gh-239
423
    with open('%s/config.ini-int-in-component-path' % gettempdir(), 'w') as f:
424
        f.write("""
425
[Directory]
426
date=%Y
427
full_path=%date
428
        """)
429
    if hasattr(load_config, 'config'):
430
        del load_config.config
431
    filesystem = FileSystem()
432
    media = Photo(helper.get_file('plain.jpg'))
433
    path = filesystem.get_folder_path(media.get_metadata())
434
    if hasattr(load_config, 'config'):
435
        del load_config.config
436
437
    assert path == os.path.join('2015'), path
438
439 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-and-album' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
440
def test_get_folder_path_with_combined_date_and_album():
441
    # gh-239
442
    with open('%s/config.ini-combined-date-and-album' % gettempdir(), 'w') as f:
443
        f.write("""
444
[Directory]
445
date=%Y-%m-%b
446
custom=%date %album
447
full_path=%custom
448
        """)
449
    if hasattr(load_config, 'config'):
450
        del load_config.config
451
    filesystem = FileSystem()
452
    media = Photo(helper.get_file('with-album.jpg'))
453
    path = filesystem.get_folder_path(media.get_metadata())
454
    if hasattr(load_config, 'config'):
455
        del load_config.config
456
457
    assert path == '2015-12-Dec Test Album', path
458
459
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-album-location-fallback' % gettempdir())
460
def test_get_folder_path_with_album_and_location_fallback():
461
    # gh-279
462
    with open('%s/config.ini-combined-date-album-location-fallback' % gettempdir(), 'w') as f:
463
        f.write("""
464
[Directory]
465
date=%Y-%m-%b
466
custom=%album
467
full_path=%custom|%city
468
        """)
469
    if hasattr(load_config, 'config'):
470
        del load_config.config
471
    filesystem = FileSystem()
472
473
    # Test with no location
474
    media = Photo(helper.get_file('plain.jpg'))
475
    path_plain = filesystem.get_folder_path(media.get_metadata())
476
477
    # Test with City
478
    media = Photo(helper.get_file('with-location.jpg'))
479
    path_city = filesystem.get_folder_path(media.get_metadata())
480
    if hasattr(load_config, 'config'):
481
        del load_config.config
482
483
    assert path_plain == 'Unknown Location', path_plain
484
    assert path_city == 'Sunnyvale', path_city
485
486
487
def test_get_folder_path_with_int_in_source_path():
488
    # gh-239
489
    filesystem = FileSystem()
490
    temporary_folder, folder = helper.create_working_folder('int')
491
492
    origin = os.path.join(folder,'plain.jpg')
493
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
494
495
    media = Photo(origin)
496
    path = filesystem.get_folder_path(media.get_metadata())
497
498
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
499
500 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-original-default-unknown-location' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
501
def test_get_folder_path_with_original_default_unknown_location():
502
    with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f:
503
        f.write('')
504
    if hasattr(load_config, 'config'):
505
        del load_config.config
506
    filesystem = FileSystem()
507
    media = Photo(helper.get_file('plain.jpg'))
508
    path = filesystem.get_folder_path(media.get_metadata())
509
    if hasattr(load_config, 'config'):
510
        del load_config.config
511
512
    assert path == os.path.join('2015-12-Dec','Unknown Location'), path
513
514 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-path' % gettempdir())
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
515
def test_get_folder_path_with_custom_path():
516
    with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f:
517
        f.write("""
518
[MapQuest]
519
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
520
521
[Directory]
522
date=%Y-%m-%d
523
location=%country-%state-%city
524
full_path=%date/%location
525
        """)
526
    if hasattr(load_config, 'config'):
527
        del load_config.config
528
    filesystem = FileSystem()
529
    media = Photo(helper.get_file('with-location.jpg'))
530
    path = filesystem.get_folder_path(media.get_metadata())
531
    if hasattr(load_config, 'config'):
532
        del load_config.config
533
534
    assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
535
536
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir())
537
def test_get_folder_path_with_fallback_folder():
538
    with open('%s/config.ini-fallback' % gettempdir(), 'w') as f:
539
        f.write("""
540
[Directory]
541
year=%Y
542
month=%m
543
full_path=%year/%month/%album|%"No Album Fool"/%month
544
        """)
545
#full_path=%year/%album|"No Album"
546
    if hasattr(load_config, 'config'):
547
        del load_config.config
548
    filesystem = FileSystem()
549
    media = Photo(helper.get_file('plain.jpg'))
550
    path = filesystem.get_folder_path(media.get_metadata())
551
    if hasattr(load_config, 'config'):
552
        del load_config.config
553
554
    assert path == os.path.join('2015','12','No Album Fool','12'), path
555
556 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
557
def test_get_folder_path_with_with_more_than_two_levels():
558
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
559
        f.write("""
560
[MapQuest]
561
key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx
562
563
[Directory]
564
year=%Y
565
month=%m
566
location=%city, %state
567
full_path=%year/%month/%location
568
        """)
569
570
    if hasattr(load_config, 'config'):
571
        del load_config.config
572
573
    filesystem = FileSystem()
574
    media = Photo(helper.get_file('with-location.jpg'))
575
    path = filesystem.get_folder_path(media.get_metadata())
576
    if hasattr(load_config, 'config'):
577
        del load_config.config
578
579
    assert path == os.path.join('2015','12','Sunnyvale, California'), path
580
581 View Code Duplication
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
582
def test_get_folder_path_with_with_only_one_level():
583
    with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
584
        f.write("""
585
[Directory]
586
year=%Y
587
full_path=%year
588
        """)
589
590
    if hasattr(load_config, 'config'):
591
        del load_config.config
592
593
    filesystem = FileSystem()
594
    media = Photo(helper.get_file('plain.jpg'))
595
    path = filesystem.get_folder_path(media.get_metadata())
596
    if hasattr(load_config, 'config'):
597
        del load_config.config
598
599
    assert path == os.path.join('2015'), path
600
601
def test_get_folder_path_with_location_and_title():
602
    filesystem = FileSystem()
603
    media = Photo(helper.get_file('with-location-and-title.jpg'))
604
    path = filesystem.get_folder_path(media.get_metadata())
605
606
    assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
607
608 View Code Duplication
def test_parse_folder_name_default():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
609
    if hasattr(load_config, 'config'):
610
        del load_config.config
611
    filesystem = FileSystem()
612
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
613
    mask = '%city'
614
    location_parts = re.findall('(%[^%]+)', mask)
615
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
616
    if hasattr(load_config, 'config'):
617
        del load_config.config
618
619
    assert path == 'Sunnyvale', path
620
621 View Code Duplication
def test_parse_folder_name_multiple():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
622
    if hasattr(load_config, 'config'):
623
        del load_config.config
624
    filesystem = FileSystem()
625
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
626
    mask = '%city-%state-%country'
627
    location_parts = re.findall('(%[^%]+)', mask)
628
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
629
    if hasattr(load_config, 'config'):
630
        del load_config.config
631
632
    assert path == 'Sunnyvale-California-United States of America', path
633
634 View Code Duplication
def test_parse_folder_name_static_chars():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
635
    if hasattr(load_config, 'config'):
636
        del load_config.config
637
    filesystem = FileSystem()
638
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'}
639
    mask = '%city-is-the-city'
640
    location_parts = re.findall('(%[^%]+)', mask)
641
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
642
    if hasattr(load_config, 'config'):
643
        del load_config.config
644
645
    assert path == 'Sunnyvale-is-the-city', path
646
647 View Code Duplication
def test_parse_folder_name_key_not_found():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
648
    if hasattr(load_config, 'config'):
649
        del load_config.config
650
    filesystem = FileSystem()
651
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
652
    mask = '%city'
653
    location_parts = re.findall('(%[^%]+)', mask)
654
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
655
    if hasattr(load_config, 'config'):
656
        del load_config.config
657
658
    assert path == 'California', path
659
660 View Code Duplication
def test_parse_folder_name_key_not_found_with_static_chars():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
661
    if hasattr(load_config, 'config'):
662
        del load_config.config
663
    filesystem = FileSystem()
664
    place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'}
665
    mask = '%city-is-not-found'
666
    location_parts = re.findall('(%[^%]+)', mask)
667
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
668
    if hasattr(load_config, 'config'):
669
        del load_config.config
670
671
    assert path == 'California', path
672
673 View Code Duplication
def test_parse_folder_name_multiple_keys_not_found():
0 ignored issues
show
Duplication introduced by Terence Eden
This code seems to be duplicated in your project.
Loading history...
674
    if hasattr(load_config, 'config'):
675
        del load_config.config
676
    filesystem = FileSystem()
677
    place_name = {'default': u'United States of America', 'country': u'United States of America'}
678
    mask = '%city-%state'
679
    location_parts = re.findall('(%[^%]+)', mask)
680
    path = filesystem.parse_mask_for_location(mask, location_parts, place_name)
681
    if hasattr(load_config, 'config'):
682
        del load_config.config
683
684
    assert path == 'United States of America', path
685
686
def test_process_file_invalid():
687
    filesystem = FileSystem()
688
    temporary_folder, folder = helper.create_working_folder()
689
690
    origin = os.path.join(folder,'photo.jpg')
691
    shutil.copyfile(helper.get_file('invalid.jpg'), origin)
692
693
    media = Photo(origin)
694
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
695
696
    assert destination is None
697
698 View Code Duplication
def test_process_file_plain():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
699
    filesystem = FileSystem()
700
    temporary_folder, folder = helper.create_working_folder()
701
702
    origin = os.path.join(folder,'photo.jpg')
703
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
704
705
    origin_checksum_preprocess = helper.checksum(origin)
706
    media = Photo(origin)
707
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
708
709
    origin_checksum = helper.checksum(origin)
710
    destination_checksum = helper.checksum(destination)
711
712
    shutil.rmtree(folder)
713
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
714
715
    assert origin_checksum_preprocess is not None
716
    assert origin_checksum is not None
717
    assert destination_checksum is not None
718
    assert origin_checksum_preprocess == origin_checksum
719
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo.jpg')) in destination, destination
720
721 View Code Duplication
def test_process_file_with_title():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
722
    filesystem = FileSystem()
723
    temporary_folder, folder = helper.create_working_folder()
724
725
    origin = '%s/photo.jpg' % folder
726
    shutil.copyfile(helper.get_file('with-title.jpg'), origin)
727
728
    origin_checksum_preprocess = helper.checksum(origin)
729
    media = Photo(origin)
730
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
731
732
    origin_checksum = helper.checksum(origin)
733
    destination_checksum = helper.checksum(destination)
734
735
    shutil.rmtree(folder)
736
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
737
738
    assert origin_checksum_preprocess is not None
739
    assert origin_checksum is not None
740
    assert destination_checksum is not None
741
    assert origin_checksum_preprocess == origin_checksum
742
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
743
744 View Code Duplication
def test_process_file_with_location():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
745
    filesystem = FileSystem()
746
    temporary_folder, folder = helper.create_working_folder()
747
748
    origin = os.path.join(folder,'photo.jpg')
749
    shutil.copyfile(helper.get_file('with-location.jpg'), origin)
750
751
    origin_checksum_preprocess = helper.checksum(origin)
752
    media = Photo(origin)
753
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
754
755
    origin_checksum = helper.checksum(origin)
756
    destination_checksum = helper.checksum(destination)
757
758
    shutil.rmtree(folder)
759
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
760
761
    assert origin_checksum_preprocess is not None
762
    assert origin_checksum is not None
763
    assert destination_checksum is not None
764
    assert origin_checksum_preprocess == origin_checksum
765
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo.jpg')) in destination, destination
766
767
def test_process_file_validate_original_checksum():
768
    filesystem = FileSystem()
769
    temporary_folder, folder = helper.create_working_folder()
770
771
    origin = os.path.join(folder,'photo.jpg')
772
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
773
774
    origin_checksum_preprocess = helper.checksum(origin)
775
    media = Photo(origin)
776
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
777
778
    origin_checksum = helper.checksum(origin)
779
    destination_checksum = helper.checksum(destination)
780
781
    shutil.rmtree(folder)
782
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
783
784
    assert origin_checksum_preprocess is not None, origin_checksum_preprocess
785
    assert origin_checksum is not None, origin_checksum
786
    assert destination_checksum is not None, destination_checksum
787
    assert origin_checksum_preprocess == origin_checksum, (origin_checksum_preprocess, origin_checksum)
788
789
790
# See https://github.com/jmathai/elodie/issues/330
791
def test_process_file_no_exif_date_is_correct_gh_330():
792
    filesystem = FileSystem()
793
    temporary_folder, folder = helper.create_working_folder()
794
795
    origin = os.path.join(folder,'photo.jpg')
796
    shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
797
798
    atime = 1330712100
799
    utime = 1330712900
800
    os.utime(origin, (atime, utime))
801
802
    media = Photo(origin)
803
    metadata = media.get_metadata()
804
805
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
806
807
    shutil.rmtree(folder)
808
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
809
810
    assert '/2012-03-Mar/' in destination, destination
811
    assert '/2012-03-02_18-28-20' in destination, destination
812
813 View Code Duplication
def test_process_file_with_location_and_title():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
814
    filesystem = FileSystem()
815
    temporary_folder, folder = helper.create_working_folder()
816
817
    origin = os.path.join(folder,'photo.jpg')
818
    shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin)
819
820
    origin_checksum_preprocess = helper.checksum(origin)
821
    media = Photo(origin)
822
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
823
824
    origin_checksum = helper.checksum(origin)
825
    destination_checksum = helper.checksum(destination)
826
827
    shutil.rmtree(folder)
828
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
829
830
    assert origin_checksum_preprocess is not None
831
    assert origin_checksum is not None
832
    assert destination_checksum is not None
833
    assert origin_checksum_preprocess == origin_checksum
834
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Sunnyvale','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
835
836 View Code Duplication
def test_process_file_with_album():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
837
    filesystem = FileSystem()
838
    temporary_folder, folder = helper.create_working_folder()
839
840
    origin = os.path.join(folder,'photo.jpg')
841
    shutil.copyfile(helper.get_file('with-album.jpg'), origin)
842
843
    origin_checksum_preprocess = helper.checksum(origin)
844
    media = Photo(origin)
845
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
846
847
    origin_checksum = helper.checksum(origin)
848
    destination_checksum = helper.checksum(destination)
849
850
    shutil.rmtree(folder)
851
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
852
853
    assert origin_checksum_preprocess is not None
854
    assert origin_checksum is not None
855
    assert destination_checksum is not None
856
    assert origin_checksum_preprocess == origin_checksum
857
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination
858
859 View Code Duplication
def test_process_file_with_album_and_title():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
860
    filesystem = FileSystem()
861
    temporary_folder, folder = helper.create_working_folder()
862
863
    origin = os.path.join(folder,'photo.jpg')
864
    shutil.copyfile(helper.get_file('with-album-and-title.jpg'), origin)
865
866
    origin_checksum_preprocess = helper.checksum(origin)
867
    media = Photo(origin)
868
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
869
870
    origin_checksum = helper.checksum(origin)
871
    destination_checksum = helper.checksum(destination)
872
873
    shutil.rmtree(folder)
874
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
875
876
    assert origin_checksum_preprocess is not None
877
    assert origin_checksum is not None
878
    assert destination_checksum is not None
879
    assert origin_checksum_preprocess == origin_checksum
880
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
881
882 View Code Duplication
def test_process_file_with_album_and_title_and_location():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
883
    filesystem = FileSystem()
884
    temporary_folder, folder = helper.create_working_folder()
885
886
    origin = os.path.join(folder,'photo.jpg')
887
    shutil.copyfile(helper.get_file('with-album-and-title-and-location.jpg'), origin)
888
889
    origin_checksum_preprocess = helper.checksum(origin)
890
    media = Photo(origin)
891
    destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True)
892
893
    origin_checksum = helper.checksum(origin)
894
    destination_checksum = helper.checksum(destination)
895
896
    shutil.rmtree(folder)
897
    shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
898
899
    assert origin_checksum_preprocess is not None
900
    assert origin_checksum is not None
901
    assert destination_checksum is not None
902
    assert origin_checksum_preprocess == origin_checksum
903
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Test Album','2015-12-05_00-59-26-photo-some-title.jpg')) in destination, destination
904
905
# gh-89 (setting album then title reverts album)
906 View Code Duplication
def test_process_video_with_album_then_title():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
907
    filesystem = FileSystem()
908
    temporary_folder, folder = helper.create_working_folder()
909
910
    origin = os.path.join(folder,'movie.mov')
911
    shutil.copyfile(helper.get_file('video.mov'), origin)
912
913
    origin_checksum = helper.checksum(origin)
914
915
    origin_checksum_preprocess = helper.checksum(origin)
916