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
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
|
|||
44 | filesystem = FileSystem() |
||
45 | folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10)) |
||
46 | status = filesystem.create_directory(folder) |
||
47 | |||
48 | # Needs to be a subdirectory |
||
49 | assert helper.temp_dir() != folder |
||
50 | |||
51 | assert status == True |
||
52 | assert os.path.isdir(folder) == True |
||
53 | assert os.path.exists(folder) == True |
||
54 | |||
55 | shutil.rmtree(folder) |
||
56 | |||
57 | @mock.patch('elodie.filesystem.os.makedirs') |
||
58 | def test_create_directory_invalid_permissions(mock_makedirs): |
||
59 | if os.name == 'nt': |
||
60 | raise SkipTest("It isn't implemented on Windows") |
||
61 | |||
62 | # Mock the case where makedirs raises an OSError because the user does |
||
63 | # not have permission to create the given directory. |
||
64 | mock_makedirs.side_effect = OSError() |
||
65 | |||
66 | filesystem = FileSystem() |
||
67 | status = filesystem.create_directory('/apathwhichdoesnotexist/afolderwhichdoesnotexist') |
||
68 | |||
69 | assert status == False |
||
70 | |||
71 | def test_delete_directory_if_empty(): |
||
72 | filesystem = FileSystem() |
||
73 | folder = os.path.join(helper.temp_dir(), helper.random_string(10)) |
||
74 | os.makedirs(folder) |
||
75 | |||
76 | assert os.path.isdir(folder) == True |
||
77 | assert os.path.exists(folder) == True |
||
78 | |||
79 | filesystem.delete_directory_if_empty(folder) |
||
80 | |||
81 | assert os.path.isdir(folder) == False |
||
82 | assert os.path.exists(folder) == False |
||
83 | |||
84 | def test_delete_directory_if_empty_when_not_empty(): |
||
85 | filesystem = FileSystem() |
||
86 | folder = os.path.join(helper.temp_dir(), helper.random_string(10), helper.random_string(10)) |
||
87 | os.makedirs(folder) |
||
88 | parent_folder = os.path.dirname(folder) |
||
89 | |||
90 | assert os.path.isdir(folder) == True |
||
91 | assert os.path.exists(folder) == True |
||
92 | assert os.path.isdir(parent_folder) == True |
||
93 | assert os.path.exists(parent_folder) == True |
||
94 | |||
95 | filesystem.delete_directory_if_empty(parent_folder) |
||
96 | |||
97 | assert os.path.isdir(folder) == True |
||
98 | assert os.path.exists(folder) == True |
||
99 | assert os.path.isdir(parent_folder) == True |
||
100 | assert os.path.exists(parent_folder) == True |
||
101 | |||
102 | shutil.rmtree(parent_folder) |
||
103 | |||
104 | def test_get_all_files_success(): |
||
105 | filesystem = FileSystem() |
||
106 | folder = helper.populate_folder(5) |
||
107 | |||
108 | files = set() |
||
109 | files.update(filesystem.get_all_files(folder)) |
||
110 | shutil.rmtree(folder) |
||
111 | |||
112 | length = len(files) |
||
113 | assert length == 5, files |
||
114 | |||
115 | def test_get_all_files_by_extension(): |
||
116 | filesystem = FileSystem() |
||
117 | folder = helper.populate_folder(5) |
||
118 | |||
119 | files = set() |
||
120 | files.update(filesystem.get_all_files(folder)) |
||
121 | length = len(files) |
||
122 | assert length == 5, length |
||
123 | |||
124 | files = set() |
||
125 | files.update(filesystem.get_all_files(folder, 'jpg')) |
||
126 | length = len(files) |
||
127 | assert length == 3, length |
||
128 | |||
129 | files = set() |
||
130 | files.update(filesystem.get_all_files(folder, 'txt')) |
||
131 | length = len(files) |
||
132 | assert length == 2, length |
||
133 | |||
134 | files = set() |
||
135 | files.update(filesystem.get_all_files(folder, 'gif')) |
||
136 | length = len(files) |
||
137 | assert length == 0, length |
||
138 | |||
139 | shutil.rmtree(folder) |
||
140 | |||
141 | def test_get_all_files_with_only_invalid_file(): |
||
142 | filesystem = FileSystem() |
||
143 | folder = helper.populate_folder(0, include_invalid=True) |
||
144 | |||
145 | files = set() |
||
146 | files.update(filesystem.get_all_files(folder)) |
||
147 | shutil.rmtree(folder) |
||
148 | |||
149 | length = len(files) |
||
150 | assert length == 0, length |
||
151 | |||
152 | def test_get_all_files_with_invalid_file(): |
||
153 | filesystem = FileSystem() |
||
154 | folder = helper.populate_folder(5, include_invalid=True) |
||
155 | |||
156 | files = set() |
||
157 | files.update(filesystem.get_all_files(folder)) |
||
158 | shutil.rmtree(folder) |
||
159 | |||
160 | length = len(files) |
||
161 | assert length == 5, length |
||
162 | |||
163 | def test_get_all_files_for_loop(): |
||
164 | filesystem = FileSystem() |
||
165 | folder = helper.populate_folder(5) |
||
166 | |||
167 | files = set() |
||
168 | files.update() |
||
169 | counter = 0 |
||
170 | for file in filesystem.get_all_files(folder): |
||
171 | counter += 1 |
||
172 | shutil.rmtree(folder) |
||
173 | |||
174 | assert counter == 5, counter |
||
175 | |||
176 | def test_get_current_directory(): |
||
177 | filesystem = FileSystem() |
||
178 | assert os.getcwd() == filesystem.get_current_directory() |
||
179 | |||
180 | def test_get_file_name_definition_default(): |
||
181 | filesystem = FileSystem() |
||
182 | name_template, definition = filesystem.get_file_name_definition() |
||
183 | |||
184 | assert name_template == '%date-%original_name-%title.%extension', name_template |
||
185 | assert definition == [[('date', '%Y-%m-%d_%H-%M-%S')], [('original_name', '')], [('title', '')], [('extension', '')]], definition #noqa |
||
186 | |||
187 | @mock.patch('elodie.config.config_file', '%s/config.ini-custom-filename' % gettempdir()) |
||
188 | def test_get_file_name_definition_custom(): |
||
189 | with open('%s/config.ini-custom-filename' % gettempdir(), 'w') as f: |
||
190 | f.write(""" |
||
191 | [File] |
||
192 | date=%Y-%m-%b |
||
193 | name=%date-%original_name.%extension |
||
194 | """) |
||
195 | if hasattr(load_config, 'config'): |
||
196 | del load_config.config |
||
197 | |||
198 | filesystem = FileSystem() |
||
199 | name_template, definition = filesystem.get_file_name_definition() |
||
200 | |||
201 | if hasattr(load_config, 'config'): |
||
202 | del load_config.config |
||
203 | |||
204 | assert name_template == '%date-%original_name.%extension', name_template |
||
205 | assert definition == [[('date', '%Y-%m-%b')], [('original_name', '')], [('extension', '')]], definition #noqa |
||
206 | |||
207 | def test_get_file_name_plain(): |
||
208 | filesystem = FileSystem() |
||
209 | media = Photo(helper.get_file('plain.jpg')) |
||
210 | file_name = filesystem.get_file_name(media) |
||
211 | |||
212 | assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain.jpg'), file_name |
||
213 | |||
214 | def test_get_file_name_with_title(): |
||
215 | filesystem = FileSystem() |
||
216 | media = Photo(helper.get_file('with-title.jpg')) |
||
217 | file_name = filesystem.get_file_name(media) |
||
218 | |||
219 | assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-with-title-some-title.jpg'), file_name |
||
220 | |||
221 | def test_get_file_name_with_original_name_exif(): |
||
222 | filesystem = FileSystem() |
||
223 | media = Photo(helper.get_file('with-filename-in-exif.jpg')) |
||
224 | file_name = filesystem.get_file_name(media) |
||
225 | |||
226 | assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar.jpg'), file_name |
||
227 | |||
228 | def test_get_file_name_with_original_name_title_exif(): |
||
229 | filesystem = FileSystem() |
||
230 | media = Photo(helper.get_file('with-filename-and-title-in-exif.jpg')) |
||
231 | file_name = filesystem.get_file_name(media) |
||
232 | |||
233 | assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-foobar-foobar-title.jpg'), file_name |
||
234 | |||
235 | def test_get_file_name_with_uppercase_and_spaces(): |
||
236 | filesystem = FileSystem() |
||
237 | media = Photo(helper.get_file('Plain With Spaces And Uppercase 123.jpg')) |
||
238 | file_name = filesystem.get_file_name(media) |
||
239 | |||
240 | assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain-with-spaces-and-uppercase-123.jpg'), file_name |
||
241 | |||
242 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
243 | def test_get_file_name_custom(): |
||
244 | with open('%s/config.ini-filename-custom' % gettempdir(), 'w') as f: |
||
245 | f.write(""" |
||
246 | [File] |
||
247 | date=%Y-%m-%b |
||
248 | name=%date-%original_name.%extension |
||
249 | """) |
||
250 | if hasattr(load_config, 'config'): |
||
251 | del load_config.config |
||
252 | |||
253 | filesystem = FileSystem() |
||
254 | media = Photo(helper.get_file('plain.jpg')) |
||
255 | file_name = filesystem.get_file_name(media) |
||
256 | |||
257 | if hasattr(load_config, 'config'): |
||
258 | del load_config.config |
||
259 | |||
260 | assert file_name == helper.path_tz_fix('2015-12-dec-plain.jpg'), file_name |
||
261 | |||
262 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-title' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
263 | def test_get_file_name_custom_with_title(): |
||
264 | with open('%s/config.ini-filename-custom-with-title' % gettempdir(), 'w') as f: |
||
265 | f.write(""" |
||
266 | [File] |
||
267 | date=%Y-%m-%d |
||
268 | name=%date-%original_name-%title.%extension |
||
269 | """) |
||
270 | if hasattr(load_config, 'config'): |
||
271 | del load_config.config |
||
272 | |||
273 | filesystem = FileSystem() |
||
274 | media = Photo(helper.get_file('with-title.jpg')) |
||
275 | file_name = filesystem.get_file_name(media) |
||
276 | |||
277 | if hasattr(load_config, 'config'): |
||
278 | del load_config.config |
||
279 | |||
280 | assert file_name == helper.path_tz_fix('2015-12-05-with-title-some-title.jpg'), file_name |
||
281 | |||
282 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-empty-value' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
283 | def test_get_file_name_custom_with_empty_value(): |
||
284 | with open('%s/config.ini-filename-custom-with-empty-value' % gettempdir(), 'w') as f: |
||
285 | f.write(""" |
||
286 | [File] |
||
287 | date=%Y-%m-%d |
||
288 | name=%date-%original_name-%title.%extension |
||
289 | """) |
||
290 | if hasattr(load_config, 'config'): |
||
291 | del load_config.config |
||
292 | |||
293 | filesystem = FileSystem() |
||
294 | media = Photo(helper.get_file('plain.jpg')) |
||
295 | file_name = filesystem.get_file_name(media) |
||
296 | |||
297 | if hasattr(load_config, 'config'): |
||
298 | del load_config.config |
||
299 | |||
300 | assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name |
||
301 | |||
302 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-lowercase' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
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) |
||
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
|
|||
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) |
||
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
|
|||
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) |
||
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
|
|||
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
|
|||
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
|
|||
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 | @mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-and-album' % gettempdir()) |
||
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 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-album-location-fallback' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
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
|
|||
501 | def test_get_folder_path_with_original_default_unknown_location(): |
||
502 | with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f: |
||
503 | f.write('') |
||
504 | if hasattr(load_config, 'config'): |
||
505 | del load_config.config |
||
506 | filesystem = FileSystem() |
||
507 | media = Photo(helper.get_file('plain.jpg')) |
||
508 | path = filesystem.get_folder_path(media.get_metadata()) |
||
509 | if hasattr(load_config, 'config'): |
||
510 | del load_config.config |
||
511 | |||
512 | assert path == os.path.join('2015-12-Dec','Unknown Location'), path |
||
513 | |||
514 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-custom-path' % gettempdir()) |
|
515 | def test_get_folder_path_with_custom_path(): |
||
516 | with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f: |
||
517 | f.write(""" |
||
518 | [MapQuest] |
||
519 | key=czjNKTtFjLydLteUBwdgKAIC8OAbGLUx |
||
520 | |||
521 | [Directory] |
||
522 | date=%Y-%m-%d |
||
523 | location=%country-%state-%city |
||
524 | full_path=%date/%location |
||
525 | """) |
||
526 | if hasattr(load_config, 'config'): |
||
527 | del load_config.config |
||
528 | filesystem = FileSystem() |
||
529 | media = Photo(helper.get_file('with-location.jpg')) |
||
530 | path = filesystem.get_folder_path(media.get_metadata()) |
||
531 | if hasattr(load_config, 'config'): |
||
532 | del load_config.config |
||
533 | |||
534 | assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path |
||
535 | |||
536 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
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
|
|||
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
|
|||
582 | def test_get_folder_path_with_with_only_one_level(): |
||
583 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
584 | f.write(""" |
||
585 | [Directory] |
||
586 | year=%Y |
||
587 | full_path=%year |
||
588 | """) |
||
589 | |||
590 | if hasattr(load_config, 'config'): |
||
591 | del load_config.config |
||
592 | |||
593 | filesystem = FileSystem() |
||
594 | media = Photo(helper.get_file('plain.jpg')) |
||
595 | path = filesystem.get_folder_path(media.get_metadata()) |
||
596 | if hasattr(load_config, 'config'): |
||
597 | del load_config.config |
||
598 | |||
599 | assert path == os.path.join('2015'), path |
||
600 | |||
601 | def test_get_folder_path_with_location_and_title(): |
||
602 | filesystem = FileSystem() |
||
603 | media = Photo(helper.get_file('with-location-and-title.jpg')) |
||
604 | path = filesystem.get_folder_path(media.get_metadata()) |
||
605 | |||
606 | assert path == os.path.join('2015-12-Dec','Sunnyvale'), path |
||
607 | |||
608 | View Code Duplication | def test_parse_folder_name_default(): |
|
609 | if hasattr(load_config, 'config'): |
||
610 | del load_config.config |
||
611 | filesystem = FileSystem() |
||
612 | place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'} |
||
613 | mask = '%city' |
||
614 | location_parts = re.findall('(%[^%]+)', mask) |
||
615 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
616 | if hasattr(load_config, 'config'): |
||
617 | del load_config.config |
||
618 | |||
619 | assert path == 'Sunnyvale', path |
||
620 | |||
621 | View Code Duplication | def test_parse_folder_name_multiple(): |
|
622 | if hasattr(load_config, 'config'): |
||
623 | del load_config.config |
||
624 | filesystem = FileSystem() |
||
625 | place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'} |
||
626 | mask = '%city-%state-%country' |
||
627 | location_parts = re.findall('(%[^%]+)', mask) |
||
628 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
629 | if hasattr(load_config, 'config'): |
||
630 | del load_config.config |
||
631 | |||
632 | assert path == 'Sunnyvale-California-United States of America', path |
||
633 | |||
634 | View Code Duplication | def test_parse_folder_name_static_chars(): |
|
635 | if hasattr(load_config, 'config'): |
||
636 | del load_config.config |
||
637 | filesystem = FileSystem() |
||
638 | place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California', 'city': u'Sunnyvale'} |
||
639 | mask = '%city-is-the-city' |
||
640 | location_parts = re.findall('(%[^%]+)', mask) |
||
641 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
642 | if hasattr(load_config, 'config'): |
||
643 | del load_config.config |
||
644 | |||
645 | assert path == 'Sunnyvale-is-the-city', path |
||
646 | |||
647 | View Code Duplication | def test_parse_folder_name_key_not_found(): |
|
648 | if hasattr(load_config, 'config'): |
||
649 | del load_config.config |
||
650 | filesystem = FileSystem() |
||
651 | place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'} |
||
652 | mask = '%city' |
||
653 | location_parts = re.findall('(%[^%]+)', mask) |
||
654 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
655 | if hasattr(load_config, 'config'): |
||
656 | del load_config.config |
||
657 | |||
658 | assert path == 'California', path |
||
659 | |||
660 | View Code Duplication | def test_parse_folder_name_key_not_found_with_static_chars(): |
|
661 | if hasattr(load_config, 'config'): |
||
662 | del load_config.config |
||
663 | filesystem = FileSystem() |
||
664 | place_name = {'default': u'California', 'country': u'United States of America', 'state': u'California'} |
||
665 | mask = '%city-is-not-found' |
||
666 | location_parts = re.findall('(%[^%]+)', mask) |
||
667 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
668 | if hasattr(load_config, 'config'): |
||
669 | del load_config.config |
||
670 | |||
671 | assert path == 'California', path |
||
672 | |||
673 | View Code Duplication | def test_parse_folder_name_multiple_keys_not_found(): |
|
674 | if hasattr(load_config, 'config'): |
||
675 | del load_config.config |
||
676 | filesystem = FileSystem() |
||
677 | place_name = {'default': u'United States of America', 'country': u'United States of America'} |
||
678 | mask = '%city-%state' |
||
679 | location_parts = re.findall('(%[^%]+)', mask) |
||
680 | path = filesystem.parse_mask_for_location(mask, location_parts, place_name) |
||
681 | if hasattr(load_config, 'config'): |
||
682 | del load_config.config |
||
683 | |||
684 | assert path == 'United States of America', path |
||
685 | |||
686 | def test_process_file_invalid(): |
||
687 | filesystem = FileSystem() |
||
688 | temporary_folder, folder = helper.create_working_folder() |
||
689 | |||
690 | origin = os.path.join(folder,'photo.jpg') |
||
691 | shutil.copyfile(helper.get_file('invalid.jpg'), origin) |
||
692 | |||
693 | media = Photo(origin) |
||
694 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
695 | |||
696 | assert destination is None |
||
697 | |||
698 | View Code Duplication | def test_process_file_plain(): |
|
0 ignored issues
–
show
|
|||
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
|
|||
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
|
|||
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 | View Code Duplication | def test_process_file_with_location_and_title(): |
|
0 ignored issues
–
show
|
|||
791 | filesystem = FileSystem() |
||
792 | temporary_folder, folder = helper.create_working_folder() |
||
793 | |||
794 | origin = os.path.join(folder,'photo.jpg') |
||
795 | shutil.copyfile(helper.get_file('with-location-and-title.jpg'), origin) |
||
796 | |||
797 | origin_checksum_preprocess = helper.checksum(origin) |
||
798 | media = Photo(origin) |
||
799 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
800 | |||
801 | origin_checksum = helper.checksum(origin) |
||
802 | destination_checksum = helper.checksum(destination) |
||
803 | |||
804 | shutil.rmtree(folder) |
||
805 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
806 | |||
807 | assert origin_checksum_preprocess is not None |
||
808 | assert origin_checksum is not None |
||
809 | assert destination_checksum is not None |
||
810 | assert origin_checksum_preprocess == origin_checksum |
||
811 | 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 |
||
812 | |||
813 | View Code Duplication | def test_process_file_with_album(): |
|
0 ignored issues
–
show
|
|||
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-album.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','Test Album','2015-12-05_00-59-26-photo.jpg')) in destination, destination |
||
835 | |||
836 | View Code Duplication | def test_process_file_with_album_and_title(): |
|
0 ignored issues
–
show
|
|||
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-and-title.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-some-title.jpg')) in destination, destination |
||
858 | |||
859 | View Code Duplication | def test_process_file_with_album_and_title_and_location(): |
|
0 ignored issues
–
show
|
|||
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-and-location.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 | # gh-89 (setting album then title reverts album) |
||
883 | View Code Duplication | def test_process_video_with_album_then_title(): |
|
0 ignored issues
–
show
|
|||
884 | filesystem = FileSystem() |
||
885 | temporary_folder, folder = helper.create_working_folder() |
||
886 | |||
887 | origin = os.path.join(folder,'movie.mov') |
||
888 | shutil.copyfile(helper.get_file('video.mov'), origin) |
||
889 | |||
890 | origin_checksum = helper.checksum(origin) |
||
891 | |||
892 | origin_checksum_preprocess = helper.checksum(origin) |
||
893 | media = Video(origin) |
||
894 | media.set_album('test_album') |
||
895 | media.set_title('test_title') |
||
896 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
897 | |||
898 | destination_checksum = helper.checksum(destination) |
||
899 | |||
900 | shutil.rmtree(folder) |
||
901 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
902 | |||
903 | assert origin_checksum_preprocess is not None |
||
904 | assert origin_checksum is not None |
||
905 | assert destination_checksum is not None |
||
906 | assert origin_checksum_preprocess == origin_checksum |
||
907 | 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 |
||
908 | |||
909 | @mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir()) |
||
910 | def test_process_file_fallback_folder(): |
||
911 | with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f: |
||
912 | f.write(""" |
||
913 | [Directory] |
||
914 | date=%Y-%m |
||
915 | full_path=%date/%album|"fallback" |
||
916 | """) |
||
917 | |||
918 | if hasattr(load_config, 'config'): |
||
919 | del load_config.config |
||
920 | filesystem = FileSystem() |
||
921 | temporary_folder, folder = helper.create_working_folder() |
||
922 | |||
923 | origin = os.path.join(folder,'plain.jpg') |
||
924 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
925 | |||
926 | media = Photo(origin) |
||
927 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
928 | if hasattr(load_config, 'config'): |
||
929 | del load_config.config |
||
930 | |||
931 | assert helper.path_tz_fix(os.path.join('2015-12', 'fallback', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
932 | |||
933 | shutil.rmtree(folder) |
||
934 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
935 | |||
936 | @mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir()) |
||
937 | def test_process_twice_more_than_two_levels_of_directories(): |
||
938 | with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f: |
||
939 | f.write(""" |
||
940 | [Directory] |
||
941 | year=%Y |
||
942 | month=%m |
||
943 | day=%d |
||
944 | full_path=%year/%month/%day |
||
945 | """) |
||
946 | |||
947 | if hasattr(load_config, 'config'): |
||
948 | del load_config.config |
||
949 | |||
950 | filesystem = FileSystem() |
||
951 | temporary_folder, folder = helper.create_working_folder() |
||
952 | |||
953 | origin = os.path.join(folder,'plain.jpg') |
||
954 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
955 | |||
956 | media = Photo(origin) |
||
957 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
958 | if hasattr(load_config, 'config'): |
||
959 | del load_config.config |
||
960 | |||
961 | assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
962 | |||
963 | if hasattr(load_config, 'config'): |
||
964 | del load_config.config |
||
965 | |||
966 | media_second = Photo(destination) |
||
967 | media_second.set_title('foo') |
||
968 | destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True) |
||
969 | |||
970 | if hasattr(load_config, 'config'): |
||
971 | del load_config.config |
||
972 | |||
973 | assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second |
||
974 | |||
975 | shutil.rmtree(folder) |
||
976 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
977 | |||
978 | def test_process_existing_file_without_changes(): |
||
979 | # gh-210 |
||
980 | filesystem = FileSystem() |
||
981 | temporary_folder, folder = helper.create_working_folder() |
||
982 | |||
983 | origin = os.path.join(folder,'plain.jpg') |
||
984 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
985 | |||
986 | media = Photo(origin) |
||
987 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
988 | |||
989 | assert helper.path_tz_fix(os.path.join('2015-12-Dec', 'Unknown Location', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
990 | |||
991 | media_second = Photo(destination) |
||
992 | destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True) |
||
993 | |||
994 | assert destination_second is None, destination_second |
||
995 | |||
996 | shutil.rmtree(folder) |
||
997 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
998 | |||
999 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-plugin-throw-error' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
1000 | def test_process_file_with_plugin_throw_error(): |
||
1001 | with open('%s/config.ini-plugin-throw-error' % gettempdir(), 'w') as f: |
||
1002 | f.write(""" |
||
1003 | [Plugins] |
||
1004 | plugins=ThrowError |
||
1005 | """) |
||
1006 | |||
1007 | if hasattr(load_config, 'config'): |
||
1008 | del load_config.config |
||
1009 | |||
1010 | filesystem = FileSystem() |
||
1011 | temporary_folder, folder = helper.create_working_folder() |
||
1012 | |||
1013 | origin = os.path.join(folder,'plain.jpg') |
||
1014 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
1015 | |||
1016 | media = Photo(origin) |
||
1017 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
1018 | |||
1019 | if hasattr(load_config, 'config'): |
||
1020 | del load_config.config |
||
1021 | |||
1022 | assert destination is None, destination |
||
1023 | |||
1024 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-plugin-runtime-error' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
1025 | def test_process_file_with_plugin_runtime_error(): |
||
1026 | with open('%s/config.ini-plugin-runtime-error' % gettempdir(), 'w') as f: |
||
1027 | f.write(""" |
||
1028 | [Plugins] |
||
1029 | plugins=RuntimeError |
||
1030 | """) |
||
1031 | if hasattr(load_config, 'config'): |
||
1032 | del load_config.config |
||
1033 | |||
1034 | filesystem = FileSystem() |
||
1035 | temporary_folder, folder = helper.create_working_folder() |
||
1036 | |||
1037 | origin = os.path.join(folder,'plain.jpg') |
||
1038 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
1039 | |||
1040 | media = Photo(origin) |
||
1041 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
1042 | |||
1043 | if hasattr(load_config, 'config'): |
||
1044 | del load_config.config |
||
1045 | |||
1046 | assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination, destination |
||
1047 | |||
1048 | View Code Duplication | def test_set_utime_with_exif_date(): |
|
0 ignored issues
–
show
|
|||
1049 | filesystem = FileSystem() |
||
1050 | temporary_folder, folder = helper.create_working_folder() |
||
1051 | |||
1052 | origin = os.path.join(folder,'photo.jpg') |
||
1053 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
1054 | |||
1055 | media_initial = Photo(origin) |
||
1056 | metadata_initial = media_initial.get_metadata() |
||
1057 | |||
1058 | initial_stat = os.stat(origin) |
||
1059 | initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime)) |
||
1060 | initial_checksum = helper.checksum(origin) |
||
1061 | |||
1062 | assert initial_time != time.mktime(metadata_initial['date_taken']) |
||
1063 | |||
1064 | filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path()) |
||
1065 | final_stat = os.stat(origin) |
||
1066 | final_checksum = helper.checksum(origin) |
||
1067 | |||
1068 | media_final = Photo(origin) |
||
1069 | metadata_final = media_final.get_metadata() |
||
1070 | |||
1071 | shutil.rmtree(folder) |
||
1072 | |||
1073 | assert initial_stat.st_mtime != final_stat.st_mtime |
||
1074 | assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']) |
||
1075 | assert initial_checksum == final_checksum |
||
1076 | |||
1077 | View Code Duplication | def test_set_utime_without_exif_date(): |
|
0 ignored issues
–
show
|
|||
1078 | filesystem = FileSystem() |
||
1079 | temporary_folder, folder = helper.create_working_folder() |
||
1080 | |||
1081 | origin = os.path.join(folder,'photo.jpg') |
||
1082 | shutil.copyfile(helper.get_file('no-exif.jpg'), origin) |
||
1083 | |||
1084 | media_initial = Photo(origin) |
||
1085 | metadata_initial = media_initial.get_metadata() |
||
1086 | |||
1087 | initial_stat = os.stat(origin) |
||
1088 | initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime)) |
||
1089 | initial_checksum = helper.checksum(origin) |
||
1090 | |||
1091 | assert initial_time == time.mktime(metadata_initial['date_taken']) |
||
1092 | |||
1093 | filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path()) |
||
1094 | final_stat = os.stat(origin) |
||
1095 | final_checksum = helper.checksum(origin) |
||
1096 | |||
1097 | media_final = Photo(origin) |
||
1098 | metadata_final = media_final.get_metadata() |
||
1099 | |||
1100 | shutil.rmtree(folder) |
||
1101 | |||
1102 | assert initial_time == final_stat.st_mtime |
||
1103 | assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken'])) |
||
1104 | assert initial_checksum == final_checksum |
||
1105 | |||
1106 | @mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir()) |
||
1107 | def test_get_folder_path_definition_default(): |
||
1108 | if hasattr(load_config, 'config'): |
||
1109 | del load_config.config |
||
1110 | filesystem = FileSystem() |
||
1111 | path_definition = filesystem.get_folder_path_definition() |
||
1112 | if hasattr(load_config, 'config'): |
||
1113 | del load_config.config |
||
1114 | |||
1115 | assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition |
||
1116 | |||
1117 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir()) |
|
1118 | def test_get_folder_path_definition_date_location(): |
||
1119 | with open('%s/config.ini-date-location' % gettempdir(), 'w') as f: |
||
1120 | f.write(""" |
||
1121 | [Directory] |
||
1122 | date=%Y-%m-%d |
||
1123 | location=%country |
||
1124 | full_path=%date/%location |
||
1125 | """) |
||
1126 | |||
1127 | if hasattr(load_config, 'config'): |
||
1128 | del load_config.config |
||
1129 | filesystem = FileSystem() |
||
1130 | path_definition = filesystem.get_folder_path_definition() |
||
1131 | expected = [ |
||
1132 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
1133 | ] |
||
1134 | if hasattr(load_config, 'config'): |
||
1135 | del load_config.config |
||
1136 | |||
1137 | assert path_definition == expected, path_definition |
||
1138 | |||
1139 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
1140 | def test_get_folder_path_definition_location_date(): |
||
1141 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
1142 | f.write(""" |
||
1143 | [Directory] |
||
1144 | date=%Y-%m-%d |
||
1145 | location=%country |
||
1146 | full_path=%location/%date |
||
1147 | """) |
||
1148 | |||
1149 | if hasattr(load_config, 'config'): |
||
1150 | del load_config.config |
||
1151 | filesystem = FileSystem() |
||
1152 | path_definition = filesystem.get_folder_path_definition() |
||
1153 | expected = [ |
||
1154 | [('location', '%country')], [('date', '%Y-%m-%d')] |
||
1155 | ] |
||
1156 | if hasattr(load_config, 'config'): |
||
1157 | del load_config.config |
||
1158 | |||
1159 | assert path_definition == expected, path_definition |
||
1160 | |||
1161 | @mock.patch('elodie.config.config_file', '%s/config.ini-cached' % gettempdir()) |
||
1162 | def test_get_folder_path_definition_cached(): |
||
1163 | with open('%s/config.ini-cached' % gettempdir(), 'w') as f: |
||
1164 | f.write(""" |
||
1165 | [Directory] |
||
1166 | date=%Y-%m-%d |
||
1167 | location=%country |
||
1168 | full_path=%date/%location |
||
1169 | """) |
||
1170 | |||
1171 | if hasattr(load_config, 'config'): |
||
1172 | del load_config.config |
||
1173 | filesystem = FileSystem() |
||
1174 | path_definition = filesystem.get_folder_path_definition() |
||
1175 | expected = [ |
||
1176 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
1177 | ] |
||
1178 | |||
1179 | assert path_definition == expected, path_definition |
||
1180 | |||
1181 | with open('%s/config.ini-cached' % gettempdir(), 'w') as f: |
||
1182 | f.write(""" |
||
1183 | [Directory] |
||
1184 | date=%uncached |
||
1185 | location=%uncached |
||
1186 | full_path=%date/%location |
||
1187 | """) |
||
1188 | if hasattr(load_config, 'config'): |
||
1189 | del load_config.config |
||
1190 | filesystem = FileSystem() |
||
1191 | path_definition = filesystem.get_folder_path_definition() |
||
1192 | expected = [ |
||
1193 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
1194 | ] |
||
1195 | if hasattr(load_config, 'config'): |
||
1196 | del load_config.config |
||
1197 | |||
1198 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
1199 | def test_get_folder_path_definition_with_more_than_two_levels(): |
||
1200 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
1201 | f.write(""" |
||
1202 | [Directory] |
||
1203 | year=%Y |
||
1204 | month=%m |
||
1205 | day=%d |
||
1206 | full_path=%year/%month/%day |
||
1207 | """) |
||
1208 | |||
1209 | if hasattr(load_config, 'config'): |
||
1210 | del load_config.config |
||
1211 | filesystem = FileSystem() |
||
1212 | path_definition = filesystem.get_folder_path_definition() |
||
1213 | expected = [ |
||
1214 | [('year', '%Y')], [('month', '%m')], [('day', '%d')] |
||
1215 | ] |
||
1216 | if hasattr(load_config, 'config'): |
||
1217 | del load_config.config |
||
1218 | |||
1219 | assert path_definition == expected, path_definition |
||
1220 | |||
1221 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
1222 | def test_get_folder_path_definition_with_only_one_level(): |
||
1223 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
1224 | f.write(""" |
||
1225 | [Directory] |
||
1226 | year=%Y |
||
1227 | full_path=%year |
||
1228 | """) |
||
1229 | |||
1230 | if hasattr(load_config, 'config'): |
||
1231 | del load_config.config |
||
1232 | filesystem = FileSystem() |
||
1233 | path_definition = filesystem.get_folder_path_definition() |
||
1234 | expected = [ |
||
1235 | [('year', '%Y')] |
||
1236 | ] |
||
1237 | if hasattr(load_config, 'config'): |
||
1238 | del load_config.config |
||
1239 | |||
1240 | assert path_definition == expected, path_definition |
||
1241 | |||
1242 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-multi-level-custom' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
1243 | def test_get_folder_path_definition_multi_level_custom(): |
||
1244 | with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f: |
||
1245 | f.write(""" |
||
1246 | [Directory] |
||
1247 | year=%Y |
||
1248 | month=%M |
||
1249 | full_path=%year/%album|%month|%"foo"/%month |
||
1250 | """) |
||
1251 | |||
1252 | if hasattr(load_config, 'config'): |
||
1253 | del load_config.config |
||
1254 | filesystem = FileSystem() |
||
1255 | path_definition = filesystem.get_folder_path_definition() |
||
1256 | |||
1257 | expected = [[('year', '%Y')], [('album', ''), ('month', '%M'), ('"foo"', '')], [('month', '%M')]] |
||
1258 | if hasattr(load_config, 'config'): |
||
1259 | del load_config.config |
||
1260 | |||
1261 | assert path_definition == expected, path_definition |
||
1262 |