test_import_file_path_unicode_checkmark()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 0
dl 16
loc 16
rs 9.85
c 0
b 0
f 0
1
# Project imports
2
from imp import load_source
3
import mock
4
import os
5
import sys
6
import shutil
7
8
from click.testing import CliRunner
9
from nose.plugins.skip import SkipTest
10
from nose.tools import assert_raises
11
from six import text_type, unichr as six_unichr
12
from tempfile import gettempdir
13
14
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
15
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))))
16
17
import helper
18
elodie = load_source('elodie', os.path.abspath('{}/../../elodie.py'.format(os.path.dirname(os.path.realpath(__file__)))))
19
20
from elodie.config import load_config
21
from elodie.localstorage import Db
22
from elodie.media.audio import Audio
23
from elodie.media.photo import Photo
24
from elodie.media.text import Text
25
from elodie.media.video import Video
26
from elodie.plugins.plugins import Plugins
27
from elodie.plugins.googlephotos.googlephotos import GooglePhotos
28
29
os.environ['TZ'] = 'GMT'
30
31
def test_import_file_text():
32
    temporary_folder, folder = helper.create_working_folder()
33
    temporary_folder_destination, folder_destination = helper.create_working_folder()
34
35
    origin = '%s/valid.txt' % folder
36
    shutil.copyfile(helper.get_file('valid.txt'), origin)
37
38
    helper.reset_dbs()
39
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
40
    helper.restore_dbs()
41
42
    shutil.rmtree(folder)
43
    shutil.rmtree(folder_destination)
44
45
    assert helper.path_tz_fix(os.path.join('2016-04-Apr','London','2016-04-07_11-15-26-valid-sample-title.txt')) in dest_path, dest_path
46
47
def test_import_file_audio():
48
    temporary_folder, folder = helper.create_working_folder()
49
    temporary_folder_destination, folder_destination = helper.create_working_folder()
50
51
    origin = '%s/audio.m4a' % folder
52
    shutil.copyfile(helper.get_file('audio.m4a'), origin)
53
54
    helper.reset_dbs()
55
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
56
    helper.restore_dbs()
57
58
    shutil.rmtree(folder)
59
    shutil.rmtree(folder_destination)
60
61
    assert helper.path_tz_fix(os.path.join('2016-01-Jan','Houston','2016-01-04_05-28-15-audio.m4a')) in dest_path, dest_path
62
63
def test_import_file_photo():
64
    temporary_folder, folder = helper.create_working_folder()
65
    temporary_folder_destination, folder_destination = helper.create_working_folder()
66
67
    origin = '%s/plain.jpg' % folder
68
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
69
70
    helper.reset_dbs()
71
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
72
    helper.restore_dbs()
73
74
    shutil.rmtree(folder)
75
    shutil.rmtree(folder_destination)
76
77
    assert helper.path_tz_fix(os.path.join('2015-12-Dec','Unknown Location','2015-12-05_00-59-26-plain.jpg')) in dest_path, dest_path
78
79
def test_import_file_video():
80
    temporary_folder, folder = helper.create_working_folder()
81
    temporary_folder_destination, folder_destination = helper.create_working_folder()
82
83
    origin = '%s/video.mov' % folder
84
    shutil.copyfile(helper.get_file('video.mov'), origin)
85
86
    helper.reset_dbs()
87
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
88
    helper.restore_dbs()
89
90
    shutil.rmtree(folder)
91
    shutil.rmtree(folder_destination)
92
93
    assert helper.path_tz_fix(os.path.join('2015-01-Jan','California','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path
94
95 View Code Duplication
def test_import_file_path_utf8_encoded_ascii_checkmark():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
96
    temporary_folder, folder = helper.create_working_folder()
97
    temporary_folder_destination, folder_destination = helper.create_working_folder()
98
99
    origin = text_type(folder)+u'/unicode\u2713filename.txt'
100
    # encode the unicode string to ascii
101
    origin = origin.encode('utf-8')
102
103
    shutil.copyfile(helper.get_file('valid.txt'), origin)
104
105
    helper.reset_dbs()
106
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
107
    helper.restore_dbs()
108
109
    shutil.rmtree(folder)
110
    shutil.rmtree(folder_destination)
111
112
    assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path
113
114 View Code Duplication
def test_import_file_path_unicode_checkmark():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
115
    temporary_folder, folder = helper.create_working_folder()
116
    temporary_folder_destination, folder_destination = helper.create_working_folder()
117
118
    origin = text_type(folder)+u'/unicode\u2713filename.txt'
119
120
    shutil.copyfile(helper.get_file('valid.txt'), origin)
121
122
    helper.reset_dbs()
123
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
124
    helper.restore_dbs()
125
126
    shutil.rmtree(folder)
127
    shutil.rmtree(folder_destination)
128
129
    assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\u2713filename-sample-title.txt')) in dest_path, dest_path
130
131 View Code Duplication
def test_import_file_path_utf8_encoded_ascii_latin_nbsp():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
132
    temporary_folder, folder = helper.create_working_folder()
133
    temporary_folder_destination, folder_destination = helper.create_working_folder()
134
135
    origin = text_type(folder)+u'/unicode'+six_unichr(160)+u'filename.txt'
136
    # encode the unicode string to ascii
137
    origin = origin.encode('utf-8')
138
139
    shutil.copyfile(helper.get_file('valid.txt'), origin)
140
141
    helper.reset_dbs()
142
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
143
    helper.restore_dbs()
144
145
    shutil.rmtree(folder)
146
    shutil.rmtree(folder_destination)
147
148
    assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path
149
150 View Code Duplication
def test_import_file_path_unicode_latin_nbsp():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
151
    temporary_folder, folder = helper.create_working_folder()
152
    temporary_folder_destination, folder_destination = helper.create_working_folder()
153
154
    origin = text_type(folder)+u'/unicode'+six_unichr(160)+u'filename.txt'
155
156
    shutil.copyfile(helper.get_file('valid.txt'), origin)
157
158
    helper.reset_dbs()
159
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
160
    helper.restore_dbs()
161
162
    shutil.rmtree(folder)
163
    shutil.rmtree(folder_destination)
164
165
    assert helper.path_tz_fix(os.path.join('2016-04-Apr','London',u'2016-04-07_11-15-26-unicode\xa0filename-sample-title.txt')) in dest_path, dest_path
166
    
167 View Code Duplication
def test_import_file_allow_duplicate_false():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
168
    temporary_folder, folder = helper.create_working_folder()
169
    temporary_folder_destination, folder_destination = helper.create_working_folder()
170
171
    origin = '%s/valid.txt' % folder
172
    shutil.copyfile(helper.get_file('valid.txt'), origin)
173
174
    helper.reset_dbs()
175
    dest_path1 = elodie.import_file(origin, folder_destination, False, False, False)
176
    dest_path2 = elodie.import_file(origin, folder_destination, False, False, False)
177
    helper.restore_dbs()
178
179
    shutil.rmtree(folder)
180
    shutil.rmtree(folder_destination)
181
182
    assert dest_path1 is not None
183
    assert dest_path2 is None
184
185 View Code Duplication
def test_import_file_allow_duplicate_true():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
186
    temporary_folder, folder = helper.create_working_folder()
187
    temporary_folder_destination, folder_destination = helper.create_working_folder()
188
189
    origin = '%s/valid.txt' % folder
190
    shutil.copyfile(helper.get_file('valid.txt'), origin)
191
192
    helper.reset_dbs()
193
    dest_path1 = elodie.import_file(origin, folder_destination, False, False, True)
194
    dest_path2 = elodie.import_file(origin, folder_destination, False, False, True)
195
    helper.restore_dbs()
196
197
    shutil.rmtree(folder)
198
    shutil.rmtree(folder_destination)
199
200
    assert dest_path1 is not None
201
    assert dest_path2 is not None
202
    assert dest_path1 == dest_path2
203
204 View Code Duplication
def test_import_file_send_to_trash_false():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
205
    temporary_folder, folder = helper.create_working_folder()
206
    temporary_folder_destination, folder_destination = helper.create_working_folder()
207
208
    origin = '%s/valid.txt' % folder
209
    shutil.copyfile(helper.get_file('valid.txt'), origin)
210
211
    helper.reset_dbs()
212
    dest_path1 = elodie.import_file(origin, folder_destination, False, False, False)
213
    assert os.path.isfile(origin), origin
214
    helper.restore_dbs()
215
216
    shutil.rmtree(folder)
217
    shutil.rmtree(folder_destination)
218
219
    assert dest_path1 is not None
220
221 View Code Duplication
def test_import_file_send_to_trash_true():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
222
    raise SkipTest("Temporarily disable send2trash test gh-230")
223
224
    temporary_folder, folder = helper.create_working_folder()
225
    temporary_folder_destination, folder_destination = helper.create_working_folder()
226
227
    origin = '%s/valid.txt' % folder
228
    shutil.copyfile(helper.get_file('valid.txt'), origin)
229
230
    helper.reset_dbs()
231
    dest_path1 = elodie.import_file(origin, folder_destination, False, True, False)
232
    assert not os.path.isfile(origin), origin
233
    helper.restore_dbs()
234
235
    shutil.rmtree(folder)
236
    shutil.rmtree(folder_destination)
237
238
    assert dest_path1 is not None
239
240 View Code Duplication
def test_import_destination_in_source():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
241
    temporary_folder, folder = helper.create_working_folder()
242
    folder_destination = '{}/destination'.format(folder)
243
    os.mkdir(folder_destination)
244
245
    origin = '%s/plain.jpg' % folder
246
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
247
248
    helper.reset_dbs()
249
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
250
    helper.restore_dbs()
251
252
    shutil.rmtree(folder)
253
254
    assert dest_path is None, dest_path
255
256 View Code Duplication
def test_import_destination_in_source_gh_287():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
257
    temporary_folder, folder = helper.create_working_folder()
258
    folder_destination = '{}-destination'.format(folder)
259
    os.mkdir(folder_destination)
260
261
    origin = '%s/video.mov' % folder
262
    shutil.copyfile(helper.get_file('video.mov'), origin)
263
264
    helper.reset_dbs()
265
    dest_path = elodie.import_file(origin, folder_destination, False, False, False)
266
    helper.restore_dbs()
267
268
    shutil.rmtree(folder)
269
270
    assert dest_path is not None, dest_path
271
272 View Code Duplication
def test_import_invalid_file_exit_code():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
273
    temporary_folder, folder = helper.create_working_folder()
274
    temporary_folder_destination, folder_destination = helper.create_working_folder()
275
276
    # use a good and bad
277
    origin_invalid = '%s/invalid.jpg' % folder
278
    shutil.copyfile(helper.get_file('invalid.jpg'), origin_invalid)
279
280
    origin_valid = '%s/valid.jpg' % folder
281
    shutil.copyfile(helper.get_file('plain.jpg'), origin_valid)
282
283
    helper.reset_dbs()
284
    runner = CliRunner()
285
    result = runner.invoke(elodie._import, ['--destination', folder_destination, origin_invalid, origin_valid])
286
    helper.restore_dbs()
287
288
    shutil.rmtree(folder)
289
    shutil.rmtree(folder_destination)
290
291
    assert result.exit_code == 1, result.exit_code
292
293 View Code Duplication
def test_update_location_on_audio():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
294
    temporary_folder, folder = helper.create_working_folder()
295
    temporary_folder_destination, folder_destination = helper.create_working_folder()
296
297
    origin = '%s/audio.m4a' % folder
298
    shutil.copyfile(helper.get_file('audio.m4a'), origin)
299
300
    audio = Audio(origin)
301
    metadata = audio.get_metadata()
302
303
    helper.reset_dbs()
304
    status = elodie.update_location(audio, origin, 'Sunnyvale, CA')
305
    helper.restore_dbs()
306
307
    audio_processed = Audio(origin)
308
    metadata_processed = audio_processed.get_metadata()
309
310
    shutil.rmtree(folder)
311
    shutil.rmtree(folder_destination)
312
313
    assert status == True, status
314
    assert metadata['latitude'] != metadata_processed['latitude'], metadata_processed['latitude']
315
    assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
316
    assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
317
318 View Code Duplication
def test_update_location_on_photo():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
319
    temporary_folder, folder = helper.create_working_folder()
320
    temporary_folder_destination, folder_destination = helper.create_working_folder()
321
322
    origin = '%s/plain.jpg' % folder
323
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
324
325
    photo = Photo(origin)
326
    metadata = photo.get_metadata()
327
328
    helper.reset_dbs()
329
    status = elodie.update_location(photo, origin, 'Sunnyvale, CA')
330
    helper.restore_dbs()
331
332
    photo_processed = Photo(origin)
333
    metadata_processed = photo_processed.get_metadata()
334
335
    shutil.rmtree(folder)
336
    shutil.rmtree(folder_destination)
337
338
    assert status == True, status
339
    assert metadata['latitude'] != metadata_processed['latitude']
340
    assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
341
    assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
342
343 View Code Duplication
def test_update_location_on_text():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
344
    temporary_folder, folder = helper.create_working_folder()
345
    temporary_folder_destination, folder_destination = helper.create_working_folder()
346
347
    origin = '%s/text.txt' % folder
348
    shutil.copyfile(helper.get_file('text.txt'), origin)
349
350
    text = Text(origin)
351
    metadata = text.get_metadata()
352
353
    helper.reset_dbs()
354
    status = elodie.update_location(text, origin, 'Sunnyvale, CA')
355
    helper.restore_dbs()
356
357
    text_processed = Text(origin)
358
    metadata_processed = text_processed.get_metadata()
359
360
    shutil.rmtree(folder)
361
    shutil.rmtree(folder_destination)
362
363
    assert status == True, status
364
    assert metadata['latitude'] != metadata_processed['latitude']
365
    assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
366
    assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
367
368 View Code Duplication
def test_update_location_on_video():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
369
    temporary_folder, folder = helper.create_working_folder()
370
    temporary_folder_destination, folder_destination = helper.create_working_folder()
371
372
    origin = '%s/video.mov' % folder
373
    shutil.copyfile(helper.get_file('video.mov'), origin)
374
375
    video = Video(origin)
376
    metadata = video.get_metadata()
377
378
    helper.reset_dbs()
379
    status = elodie.update_location(video, origin, 'Sunnyvale, CA')
380
    helper.restore_dbs()
381
382
    video_processed = Video(origin)
383
    metadata_processed = video_processed.get_metadata()
384
385
    shutil.rmtree(folder)
386
    shutil.rmtree(folder_destination)
387
388
    assert status == True, status
389
    assert metadata['latitude'] != metadata_processed['latitude']
390
    assert helper.isclose(metadata_processed['latitude'], 37.36883), metadata_processed['latitude']
391
    assert helper.isclose(metadata_processed['longitude'], -122.03635), metadata_processed['longitude']
392
393 View Code Duplication
def test_update_time_on_audio():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
394
    temporary_folder, folder = helper.create_working_folder()
395
    temporary_folder_destination, folder_destination = helper.create_working_folder()
396
397
    origin = '%s/audio.m4a' % folder
398
    shutil.copyfile(helper.get_file('audio.m4a'), origin)
399
400
    audio = Audio(origin)
401
    metadata = audio.get_metadata()
402
403
    helper.reset_dbs()
404
    status = elodie.update_time(audio, origin, '2000-01-01 12:00:00')
405
    helper.restore_dbs()
406
407
    audio_processed = Audio(origin)
408
    metadata_processed = audio_processed.get_metadata()
409
410
    shutil.rmtree(folder)
411
    shutil.rmtree(folder_destination)
412
413
    assert status == True, status
414
    assert metadata['date_taken'] != metadata_processed['date_taken']
415
    assert metadata_processed['date_taken'] == helper.time_convert((2000, 1, 1, 12, 0, 0, 5, 1, 0)), metadata_processed['date_taken']
416
417 View Code Duplication
def test_update_time_on_photo():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
418
    temporary_folder, folder = helper.create_working_folder()
419
    temporary_folder_destination, folder_destination = helper.create_working_folder()
420
421
    origin = '%s/plain.jpg' % folder
422
    shutil.copyfile(helper.get_file('plain.jpg'), origin)
423
424
    photo = Photo(origin)
425
    metadata = photo.get_metadata()
426
427
    helper.reset_dbs()
428
    status = elodie.update_time(photo, origin, '2000-01-01 12:00:00')
429
    helper.restore_dbs()
430
431
    photo_processed = Photo(origin)
432
    metadata_processed = photo_processed.get_metadata()
433
434
    shutil.rmtree(folder)
435
    shutil.rmtree(folder_destination)
436
437
    assert status == True, status
438
    assert metadata['date_taken'] != metadata_processed['date_taken']
439
    assert metadata_processed['date_taken'] == helper.time_convert((2000, 1, 1, 12, 0, 0, 5, 1, 0)), metadata_processed['date_taken']
440
441 View Code Duplication
def test_update_time_on_text():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
442
    temporary_folder, folder = helper.create_working_folder()
443
    temporary_folder_destination, folder_destination = helper.create_working_folder()
444
445
    origin = '%s/text.txt' % folder
446
    shutil.copyfile(helper.get_file('text.txt'), origin)
447
448
    text = Text(origin)
449
    metadata = text.get_metadata()
450
451
    helper.reset_dbs()
452
    status = elodie.update_time(text, origin, '2000-01-01 12:00:00')
453
    helper.restore_dbs()
454
455
    text_processed = Text(origin)
456
    metadata_processed = text_processed.get_metadata()
457
458
    shutil.rmtree(folder)
459
    shutil.rmtree(folder_destination)
460
461
    assert status == True, status
462
    assert metadata['date_taken'] != metadata_processed['date_taken']
463
    assert metadata_processed['date_taken'] == helper.time_convert((2000, 1, 1, 12, 0, 0, 5, 1, 0)), metadata_processed['date_taken']
464
465 View Code Duplication
def test_update_time_on_video():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
466
    temporary_folder, folder = helper.create_working_folder()
467
    temporary_folder_destination, folder_destination = helper.create_working_folder()
468
469
    origin = '%s/video.mov' % folder
470
    shutil.copyfile(helper.get_file('video.mov'), origin)
471
472
    video = Video(origin)
473
    metadata = video.get_metadata()
474
475
    helper.reset_dbs()
476
    status = elodie.update_time(video, origin, '2000-01-01 12:00:00')
477
    helper.restore_dbs()
478
479
    video_processed = Video(origin)
480
    metadata_processed = video_processed.get_metadata()
481
482
    shutil.rmtree(folder)
483
    shutil.rmtree(folder_destination)
484
485
    assert status == True, status
486
    assert metadata['date_taken'] != metadata_processed['date_taken']
487
    assert metadata_processed['date_taken'] == helper.time_convert((2000, 1, 1, 12, 0, 0, 5, 1, 0)), metadata_processed['date_taken']
488
489
def test_update_with_directory_passed_in():
490
    temporary_folder, folder = helper.create_working_folder()
491
    temporary_folder_destination, folder_destination = helper.create_working_folder()
492
493
    origin = '%s/valid.txt' % folder
494
    shutil.copyfile(helper.get_file('valid.txt'), origin)
495
496
    helper.reset_dbs()
497
    runner = CliRunner()
498
    result = runner.invoke(elodie._import, ['--destination', folder_destination, folder])
499
    runner2 = CliRunner()
500
    result = runner2.invoke(elodie._update, ['--album', 'test', folder_destination])
501
    helper.restore_dbs()
502
503
    updated_file_path = "{}/2016-04-Apr/test/2016-04-07_11-15-26-valid-sample-title.txt".format(folder_destination)
504
    updated_file_exists = os.path.isfile(updated_file_path)
505
506
    shutil.rmtree(folder)
507
    shutil.rmtree(folder_destination)
508
509
    assert updated_file_exists, updated_file_path
510
511 View Code Duplication
def test_update_invalid_file_exit_code():
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
512
    temporary_folder, folder = helper.create_working_folder()
513
    temporary_folder_destination, folder_destination = helper.create_working_folder()
514
515
    # use a good and bad
516
    origin_invalid = '%s/invalid.jpg' % folder
517
    shutil.copyfile(helper.get_file('invalid.jpg'), origin_invalid)
518
519
    origin_valid = '%s/valid.jpg' % folder
520
    shutil.copyfile(helper.get_file('plain.jpg'), origin_valid)
521
522
    helper.reset_dbs()
523
    runner = CliRunner()
524
    result = runner.invoke(elodie._update, ['--album', 'test', origin_invalid, origin_valid])
525
    helper.restore_dbs()
526
527
    shutil.rmtree(folder)
528
    shutil.rmtree(folder_destination)
529
530
    assert result.exit_code == 1, result.exit_code
531
532
def test_regenerate_db_invalid_source():
533
    runner = CliRunner()
534
    result = runner.invoke(elodie._generate_db, ['--source', '/invalid/path'])
535
    assert result.exit_code == 1, result.exit_code
536
537
def test_regenerate_valid_source():
538
    temporary_folder, folder = helper.create_working_folder()
539
540
    origin = '%s/valid.txt' % folder
541
    shutil.copyfile(helper.get_file('valid.txt'), origin)
542
543
    helper.reset_dbs()
544
    runner = CliRunner()
545
    result = runner.invoke(elodie._generate_db, ['--source', folder])
546
    db = Db()
547
    helper.restore_dbs()
548
549
    shutil.rmtree(folder)
550
551
    assert result.exit_code == 0, result.exit_code
552
    assert '3c19a5d751cf19e093b7447297731124d9cc987d3f91a9d1872c3b1c1b15639a' in db.hash_db, db.hash_db
553
554
def test_regenerate_valid_source_with_invalid_files():
555
    temporary_folder, folder = helper.create_working_folder()
556
557
    origin_valid = '%s/valid.txt' % folder
558
    shutil.copyfile(helper.get_file('valid.txt'), origin_valid)
559
    origin_invalid = '%s/invalid.invalid' % folder
560
    shutil.copyfile(helper.get_file('invalid.invalid'), origin_invalid)
561
562
    helper.reset_dbs()
563
    runner = CliRunner()
564
    result = runner.invoke(elodie._generate_db, ['--source', folder])
565
    db = Db()
566
    helper.restore_dbs()
567
568
    shutil.rmtree(folder)
569
570
    assert result.exit_code == 0, result.exit_code
571
    assert '3c19a5d751cf19e093b7447297731124d9cc987d3f91a9d1872c3b1c1b15639a' in db.hash_db, db.hash_db
572
    assert 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' not in db.hash_db, db.hash_db
573
574
def test_verify_ok():
575
    temporary_folder, folder = helper.create_working_folder()
576
577
    origin = '%s/valid.txt' % folder
578
    shutil.copyfile(helper.get_file('valid.txt'), origin)
579
580
    helper.reset_dbs()
581
    runner = CliRunner()
582
    runner.invoke(elodie._generate_db, ['--source', folder])
583
    result = runner.invoke(elodie._verify)
584
    helper.restore_dbs()
585
586
    shutil.rmtree(folder)
587
588
    assert 'Success         1' in result.output, result.output
589
    assert 'Error           0' in result.output, result.output
590
591
def test_verify_error():
592
    temporary_folder, folder = helper.create_working_folder()
593
594
    origin = '%s/valid.txt' % folder
595
    shutil.copyfile(helper.get_file('valid.txt'), origin)
596
597
    helper.reset_dbs()
598
    runner = CliRunner()
599
    runner.invoke(elodie._generate_db, ['--source', folder])
600
    with open(origin, 'w') as f:
601
        f.write('changed text')
602
    result = runner.invoke(elodie._verify)
603
    helper.restore_dbs()
604
605
    shutil.rmtree(folder)
606
607
    assert origin in result.output, result.output
608
    assert 'Error           1' in result.output, result.output
609
610
@mock.patch('elodie.config.config_file', '%s/config.ini-cli-batch-plugin-googlephotos' % gettempdir())
611
def test_cli_batch_plugin_googlephotos():
612
    auth_file = helper.get_file('plugins/googlephotos/auth_file.json')
613
    secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json')
614
    config_string = """
615
    [Plugins]
616
    plugins=GooglePhotos
617
618
    [PluginGooglePhotos]
619
    auth_file={}
620
    secrets_file={}
621
            """
622
    config_string_fmt = config_string.format(
623
        auth_file,
624
        secrets_file
625
    )
626
    with open('%s/config.ini-cli-batch-plugin-googlephotos' % gettempdir(), 'w') as f:
627
        f.write(config_string_fmt)
628
629
    if hasattr(load_config, 'config'):
630
        del load_config.config
631
632
    final_file_path_1 = helper.get_file('plain.jpg')
633
    final_file_path_2 = helper.get_file('no-exif.jpg')
634
    sample_metadata_1 = Photo(final_file_path_1).get_metadata()
635
    sample_metadata_2 = Photo(final_file_path_2).get_metadata()
636
    gp = GooglePhotos()
637
    gp.after('', '', final_file_path_1, sample_metadata_1)
638
    gp.after('', '', final_file_path_2, sample_metadata_1)
639
640
    if hasattr(load_config, 'config'):
641
        del load_config.config
642
643
    runner = CliRunner()
644
    result = runner.invoke(elodie._batch)
645
    assert "elodie/elodie/tests/files/plain.jpg uploaded successfully.\"}\n" in result.output, result.output
646
    assert "elodie/elodie/tests/files/no-exif.jpg uploaded successfully.\"}\n" in result.output, result.output
647
648
def test_cli_debug_import():
649
    runner = CliRunner()
650
    # import
651
    result = runner.invoke(elodie._import, ['--destination', '/does/not/exist', '/does/not/exist'])
652
    assert "Could not find /does/not/exist\n" not in result.output, result.output
653
    result = runner.invoke(elodie._import, ['--destination', '/does/not/exist', '--debug', '/does/not/exist'])
654
    assert "Could not find /does/not/exist\n" in result.output, result.output
655
656
def test_cli_debug_update():
657
    runner = CliRunner()
658
    # update
659
    result = runner.invoke(elodie._update, ['--location', 'foobar', '/does/not/exist'])
660
    assert "Could not find /does/not/exist\n" not in result.output, result.output
661
    result = runner.invoke(elodie._update, ['--location', 'foobar', '--debug', '/does/not/exist'])
662
    assert "Could not find /does/not/exist\n" in result.output, result.output
663