| Total Complexity | 177 |
| Total Lines | 1315 |
| Duplicated Lines | 56.88 % |
| Changes | 0 | ||
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:
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(): |
|
|
|
|||
| 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(): |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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()) |
|
| 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 | @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()) |
|
| 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()) |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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(): |
|
| 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 | media = Video(origin) |
||
| 917 | media.set_album('test_album') |
||
| 918 | media.set_title('test_title') |
||
| 919 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 920 | |||
| 921 | destination_checksum = helper.checksum(destination) |
||
| 922 | |||
| 923 | shutil.rmtree(folder) |
||
| 924 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
| 925 | |||
| 926 | assert origin_checksum_preprocess is not None |
||
| 927 | assert origin_checksum is not None |
||
| 928 | assert destination_checksum is not None |
||
| 929 | assert origin_checksum_preprocess == origin_checksum |
||
| 930 | 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 |
||
| 931 | |||
| 932 | @mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir()) |
||
| 933 | def test_process_file_fallback_folder(): |
||
| 934 | with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f: |
||
| 935 | f.write(""" |
||
| 936 | [Directory] |
||
| 937 | date=%Y-%m |
||
| 938 | full_path=%date/%album|"fallback" |
||
| 939 | """) |
||
| 940 | |||
| 941 | if hasattr(load_config, 'config'): |
||
| 942 | del load_config.config |
||
| 943 | filesystem = FileSystem() |
||
| 944 | temporary_folder, folder = helper.create_working_folder() |
||
| 945 | |||
| 946 | origin = os.path.join(folder,'plain.jpg') |
||
| 947 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 948 | |||
| 949 | media = Photo(origin) |
||
| 950 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 951 | if hasattr(load_config, 'config'): |
||
| 952 | del load_config.config |
||
| 953 | |||
| 954 | assert helper.path_tz_fix(os.path.join('2015-12', 'fallback', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
| 955 | |||
| 956 | shutil.rmtree(folder) |
||
| 957 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
| 958 | |||
| 959 | @mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir()) |
||
| 960 | def test_process_twice_more_than_two_levels_of_directories(): |
||
| 961 | with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f: |
||
| 962 | f.write(""" |
||
| 963 | [Directory] |
||
| 964 | year=%Y |
||
| 965 | month=%m |
||
| 966 | day=%d |
||
| 967 | full_path=%year/%month/%day |
||
| 968 | """) |
||
| 969 | |||
| 970 | if hasattr(load_config, 'config'): |
||
| 971 | del load_config.config |
||
| 972 | |||
| 973 | filesystem = FileSystem() |
||
| 974 | temporary_folder, folder = helper.create_working_folder() |
||
| 975 | |||
| 976 | origin = os.path.join(folder,'plain.jpg') |
||
| 977 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 978 | |||
| 979 | media = Photo(origin) |
||
| 980 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 981 | if hasattr(load_config, 'config'): |
||
| 982 | del load_config.config |
||
| 983 | |||
| 984 | assert helper.path_tz_fix(os.path.join('2015','12','05', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
| 985 | |||
| 986 | if hasattr(load_config, 'config'): |
||
| 987 | del load_config.config |
||
| 988 | |||
| 989 | media_second = Photo(destination) |
||
| 990 | media_second.set_title('foo') |
||
| 991 | destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True) |
||
| 992 | |||
| 993 | if hasattr(load_config, 'config'): |
||
| 994 | del load_config.config |
||
| 995 | |||
| 996 | assert destination.replace('.jpg', '-foo.jpg') == destination_second, destination_second |
||
| 997 | |||
| 998 | shutil.rmtree(folder) |
||
| 999 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
| 1000 | |||
| 1001 | def test_process_existing_file_without_changes(): |
||
| 1002 | # gh-210 |
||
| 1003 | filesystem = FileSystem() |
||
| 1004 | temporary_folder, folder = helper.create_working_folder() |
||
| 1005 | |||
| 1006 | origin = os.path.join(folder,'plain.jpg') |
||
| 1007 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 1008 | |||
| 1009 | media = Photo(origin) |
||
| 1010 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 1011 | |||
| 1012 | assert helper.path_tz_fix(os.path.join('2015-12-Dec', 'Unknown Location', '2015-12-05_00-59-26-plain.jpg')) in destination, destination |
||
| 1013 | |||
| 1014 | media_second = Photo(destination) |
||
| 1015 | destination_second = filesystem.process_file(destination, temporary_folder, media_second, allowDuplicate=True) |
||
| 1016 | |||
| 1017 | assert destination_second is None, destination_second |
||
| 1018 | |||
| 1019 | shutil.rmtree(folder) |
||
| 1020 | shutil.rmtree(os.path.dirname(os.path.dirname(destination))) |
||
| 1021 | |||
| 1022 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-plugin-throw-error' % gettempdir()) |
|
| 1023 | def test_process_file_with_plugin_throw_error(): |
||
| 1024 | with open('%s/config.ini-plugin-throw-error' % gettempdir(), 'w') as f: |
||
| 1025 | f.write(""" |
||
| 1026 | [Plugins] |
||
| 1027 | plugins=ThrowError |
||
| 1028 | """) |
||
| 1029 | |||
| 1030 | if hasattr(load_config, 'config'): |
||
| 1031 | del load_config.config |
||
| 1032 | |||
| 1033 | filesystem = FileSystem() |
||
| 1034 | temporary_folder, folder = helper.create_working_folder() |
||
| 1035 | |||
| 1036 | origin = os.path.join(folder,'plain.jpg') |
||
| 1037 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 1038 | |||
| 1039 | media = Photo(origin) |
||
| 1040 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 1041 | |||
| 1042 | if hasattr(load_config, 'config'): |
||
| 1043 | del load_config.config |
||
| 1044 | |||
| 1045 | assert destination is None, destination |
||
| 1046 | |||
| 1047 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-plugin-runtime-error' % gettempdir()) |
|
| 1048 | def test_process_file_with_plugin_runtime_error(): |
||
| 1049 | with open('%s/config.ini-plugin-runtime-error' % gettempdir(), 'w') as f: |
||
| 1050 | f.write(""" |
||
| 1051 | [Plugins] |
||
| 1052 | plugins=RuntimeError |
||
| 1053 | """) |
||
| 1054 | if hasattr(load_config, 'config'): |
||
| 1055 | del load_config.config |
||
| 1056 | |||
| 1057 | filesystem = FileSystem() |
||
| 1058 | temporary_folder, folder = helper.create_working_folder() |
||
| 1059 | |||
| 1060 | origin = os.path.join(folder,'plain.jpg') |
||
| 1061 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 1062 | |||
| 1063 | media = Photo(origin) |
||
| 1064 | destination = filesystem.process_file(origin, temporary_folder, media, allowDuplicate=True) |
||
| 1065 | |||
| 1066 | if hasattr(load_config, 'config'): |
||
| 1067 | del load_config.config |
||
| 1068 | |||
| 1069 | assert '2015-12-Dec/Unknown Location/2015-12-05_00-59-26-plain.jpg' in destination, destination |
||
| 1070 | |||
| 1071 | View Code Duplication | def test_set_utime_with_exif_date(): |
|
| 1072 | filesystem = FileSystem() |
||
| 1073 | temporary_folder, folder = helper.create_working_folder() |
||
| 1074 | |||
| 1075 | origin = os.path.join(folder,'photo.jpg') |
||
| 1076 | shutil.copyfile(helper.get_file('plain.jpg'), origin) |
||
| 1077 | |||
| 1078 | media_initial = Photo(origin) |
||
| 1079 | metadata_initial = media_initial.get_metadata() |
||
| 1080 | |||
| 1081 | initial_stat = os.stat(origin) |
||
| 1082 | initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime)) |
||
| 1083 | initial_checksum = helper.checksum(origin) |
||
| 1084 | |||
| 1085 | assert initial_time != time.mktime(metadata_initial['date_taken']) |
||
| 1086 | |||
| 1087 | filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path()) |
||
| 1088 | final_stat = os.stat(origin) |
||
| 1089 | final_checksum = helper.checksum(origin) |
||
| 1090 | |||
| 1091 | media_final = Photo(origin) |
||
| 1092 | metadata_final = media_final.get_metadata() |
||
| 1093 | |||
| 1094 | shutil.rmtree(folder) |
||
| 1095 | |||
| 1096 | assert initial_stat.st_mtime != final_stat.st_mtime |
||
| 1097 | assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']) |
||
| 1098 | assert initial_checksum == final_checksum |
||
| 1099 | |||
| 1100 | View Code Duplication | def test_set_utime_without_exif_date(): |
|
| 1101 | filesystem = FileSystem() |
||
| 1102 | temporary_folder, folder = helper.create_working_folder() |
||
| 1103 | |||
| 1104 | origin = os.path.join(folder,'photo.jpg') |
||
| 1105 | shutil.copyfile(helper.get_file('no-exif.jpg'), origin) |
||
| 1106 | |||
| 1107 | media_initial = Photo(origin) |
||
| 1108 | metadata_initial = media_initial.get_metadata() |
||
| 1109 | |||
| 1110 | initial_stat = os.stat(origin) |
||
| 1111 | initial_time = int(min(initial_stat.st_mtime, initial_stat.st_ctime)) |
||
| 1112 | initial_checksum = helper.checksum(origin) |
||
| 1113 | |||
| 1114 | assert initial_time == time.mktime(metadata_initial['date_taken']) |
||
| 1115 | |||
| 1116 | filesystem.set_utime_from_metadata(media_initial.get_metadata(), media_initial.get_file_path()) |
||
| 1117 | final_stat = os.stat(origin) |
||
| 1118 | final_checksum = helper.checksum(origin) |
||
| 1119 | |||
| 1120 | media_final = Photo(origin) |
||
| 1121 | metadata_final = media_final.get_metadata() |
||
| 1122 | |||
| 1123 | shutil.rmtree(folder) |
||
| 1124 | |||
| 1125 | assert initial_time == final_stat.st_mtime |
||
| 1126 | assert final_stat.st_mtime == time.mktime(metadata_final['date_taken']), (final_stat.st_mtime, time.mktime(metadata_final['date_taken'])) |
||
| 1127 | assert initial_checksum == final_checksum |
||
| 1128 | |||
| 1129 | def test_should_exclude_with_no_exclude_arg(): |
||
| 1130 | filesystem = FileSystem() |
||
| 1131 | result = filesystem.should_exclude('/some/path') |
||
| 1132 | assert result == False, result |
||
| 1133 | |||
| 1134 | def test_should_exclude_with_non_matching_regex(): |
||
| 1135 | filesystem = FileSystem() |
||
| 1136 | result = filesystem.should_exclude('/some/path', {re.compile('foobar')}) |
||
| 1137 | assert result == False, result |
||
| 1138 | |||
| 1139 | def test_should_exclude_with_matching_regex(): |
||
| 1140 | filesystem = FileSystem() |
||
| 1141 | result = filesystem.should_exclude('/some/path', {re.compile('some')}) |
||
| 1142 | assert result == True, result |
||
| 1143 | |||
| 1144 | def test_should_not_exclude_with_multiple_with_non_matching_regex(): |
||
| 1145 | filesystem = FileSystem() |
||
| 1146 | result = filesystem.should_exclude('/some/path', {re.compile('foobar'), re.compile('dne')}) |
||
| 1147 | assert result == False, result |
||
| 1148 | |||
| 1149 | def test_should_exclude_with_multiple_with_one_matching_regex(): |
||
| 1150 | filesystem = FileSystem() |
||
| 1151 | result = filesystem.should_exclude('/some/path', {re.compile('foobar'), re.compile('some')}) |
||
| 1152 | assert result == True, result |
||
| 1153 | |||
| 1154 | def test_should_exclude_with_complex_matching_regex(): |
||
| 1155 | filesystem = FileSystem() |
||
| 1156 | result = filesystem.should_exclude('/var/folders/j9/h192v5v95gd_fhpv63qzyd1400d9ct/T/T497XPQH2R/UATR2GZZTX/2016-04-Apr/London/2016-04-07_11-15-26-valid-sample-title.txt', {re.compile('London.*\.txt$')}) |
||
| 1157 | assert result == True, result |
||
| 1158 | |||
| 1159 | @mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir()) |
||
| 1160 | def test_get_folder_path_definition_default(): |
||
| 1161 | if hasattr(load_config, 'config'): |
||
| 1162 | del load_config.config |
||
| 1163 | filesystem = FileSystem() |
||
| 1164 | path_definition = filesystem.get_folder_path_definition() |
||
| 1165 | if hasattr(load_config, 'config'): |
||
| 1166 | del load_config.config |
||
| 1167 | |||
| 1168 | assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition |
||
| 1169 | |||
| 1170 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir()) |
|
| 1171 | def test_get_folder_path_definition_date_location(): |
||
| 1172 | with open('%s/config.ini-date-location' % gettempdir(), 'w') as f: |
||
| 1173 | f.write(""" |
||
| 1174 | [Directory] |
||
| 1175 | date=%Y-%m-%d |
||
| 1176 | location=%country |
||
| 1177 | full_path=%date/%location |
||
| 1178 | """) |
||
| 1179 | |||
| 1180 | if hasattr(load_config, 'config'): |
||
| 1181 | del load_config.config |
||
| 1182 | filesystem = FileSystem() |
||
| 1183 | path_definition = filesystem.get_folder_path_definition() |
||
| 1184 | expected = [ |
||
| 1185 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
| 1186 | ] |
||
| 1187 | if hasattr(load_config, 'config'): |
||
| 1188 | del load_config.config |
||
| 1189 | |||
| 1190 | assert path_definition == expected, path_definition |
||
| 1191 | |||
| 1192 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
| 1193 | def test_get_folder_path_definition_location_date(): |
||
| 1194 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
| 1195 | f.write(""" |
||
| 1196 | [Directory] |
||
| 1197 | date=%Y-%m-%d |
||
| 1198 | location=%country |
||
| 1199 | full_path=%location/%date |
||
| 1200 | """) |
||
| 1201 | |||
| 1202 | if hasattr(load_config, 'config'): |
||
| 1203 | del load_config.config |
||
| 1204 | filesystem = FileSystem() |
||
| 1205 | path_definition = filesystem.get_folder_path_definition() |
||
| 1206 | expected = [ |
||
| 1207 | [('location', '%country')], [('date', '%Y-%m-%d')] |
||
| 1208 | ] |
||
| 1209 | if hasattr(load_config, 'config'): |
||
| 1210 | del load_config.config |
||
| 1211 | |||
| 1212 | assert path_definition == expected, path_definition |
||
| 1213 | |||
| 1214 | @mock.patch('elodie.config.config_file', '%s/config.ini-cached' % gettempdir()) |
||
| 1215 | def test_get_folder_path_definition_cached(): |
||
| 1216 | with open('%s/config.ini-cached' % gettempdir(), 'w') as f: |
||
| 1217 | f.write(""" |
||
| 1218 | [Directory] |
||
| 1219 | date=%Y-%m-%d |
||
| 1220 | location=%country |
||
| 1221 | full_path=%date/%location |
||
| 1222 | """) |
||
| 1223 | |||
| 1224 | if hasattr(load_config, 'config'): |
||
| 1225 | del load_config.config |
||
| 1226 | filesystem = FileSystem() |
||
| 1227 | path_definition = filesystem.get_folder_path_definition() |
||
| 1228 | expected = [ |
||
| 1229 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
| 1230 | ] |
||
| 1231 | |||
| 1232 | assert path_definition == expected, path_definition |
||
| 1233 | |||
| 1234 | with open('%s/config.ini-cached' % gettempdir(), 'w') as f: |
||
| 1235 | f.write(""" |
||
| 1236 | [Directory] |
||
| 1237 | date=%uncached |
||
| 1238 | location=%uncached |
||
| 1239 | full_path=%date/%location |
||
| 1240 | """) |
||
| 1241 | if hasattr(load_config, 'config'): |
||
| 1242 | del load_config.config |
||
| 1243 | filesystem = FileSystem() |
||
| 1244 | path_definition = filesystem.get_folder_path_definition() |
||
| 1245 | expected = [ |
||
| 1246 | [('date', '%Y-%m-%d')], [('location', '%country')] |
||
| 1247 | ] |
||
| 1248 | if hasattr(load_config, 'config'): |
||
| 1249 | del load_config.config |
||
| 1250 | |||
| 1251 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
| 1252 | def test_get_folder_path_definition_with_more_than_two_levels(): |
||
| 1253 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
| 1254 | f.write(""" |
||
| 1255 | [Directory] |
||
| 1256 | year=%Y |
||
| 1257 | month=%m |
||
| 1258 | day=%d |
||
| 1259 | full_path=%year/%month/%day |
||
| 1260 | """) |
||
| 1261 | |||
| 1262 | if hasattr(load_config, 'config'): |
||
| 1263 | del load_config.config |
||
| 1264 | filesystem = FileSystem() |
||
| 1265 | path_definition = filesystem.get_folder_path_definition() |
||
| 1266 | expected = [ |
||
| 1267 | [('year', '%Y')], [('month', '%m')], [('day', '%d')] |
||
| 1268 | ] |
||
| 1269 | if hasattr(load_config, 'config'): |
||
| 1270 | del load_config.config |
||
| 1271 | |||
| 1272 | assert path_definition == expected, path_definition |
||
| 1273 | |||
| 1274 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir()) |
|
| 1275 | def test_get_folder_path_definition_with_only_one_level(): |
||
| 1276 | with open('%s/config.ini-location-date' % gettempdir(), 'w') as f: |
||
| 1277 | f.write(""" |
||
| 1278 | [Directory] |
||
| 1279 | year=%Y |
||
| 1280 | full_path=%year |
||
| 1281 | """) |
||
| 1282 | |||
| 1283 | if hasattr(load_config, 'config'): |
||
| 1284 | del load_config.config |
||
| 1285 | filesystem = FileSystem() |
||
| 1286 | path_definition = filesystem.get_folder_path_definition() |
||
| 1287 | expected = [ |
||
| 1288 | [('year', '%Y')] |
||
| 1289 | ] |
||
| 1290 | if hasattr(load_config, 'config'): |
||
| 1291 | del load_config.config |
||
| 1292 | |||
| 1293 | assert path_definition == expected, path_definition |
||
| 1294 | |||
| 1295 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-multi-level-custom' % gettempdir()) |
|
| 1296 | def test_get_folder_path_definition_multi_level_custom(): |
||
| 1297 | with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f: |
||
| 1298 | f.write(""" |
||
| 1299 | [Directory] |
||
| 1300 | year=%Y |
||
| 1301 | month=%M |
||
| 1302 | full_path=%year/%album|%month|%"foo"/%month |
||
| 1303 | """) |
||
| 1304 | |||
| 1305 | if hasattr(load_config, 'config'): |
||
| 1306 | del load_config.config |
||
| 1307 | filesystem = FileSystem() |
||
| 1308 | path_definition = filesystem.get_folder_path_definition() |
||
| 1309 | |||
| 1310 | expected = [[('year', '%Y')], [('album', ''), ('month', '%M'), ('"foo"', '')], [('month', '%M')]] |
||
| 1311 | if hasattr(load_config, 'config'): |
||
| 1312 | del load_config.config |
||
| 1313 | |||
| 1314 | assert path_definition == expected, path_definition |
||
| 1315 |