From 362b1b1363b538eefb75aaf879e955fe38290243 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Tue, 31 Oct 2017 23:22:03 -0700 Subject: [PATCH] Add tests to reproduce error when source path has folder component of numbers #239 (#241) --- elodie/tests/filesystem_test.py | 32 ++++++++++++++++++++++++++++++++ elodie/tests/helper.py | 14 ++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/elodie/tests/filesystem_test.py b/elodie/tests/filesystem_test.py index 6bf5aef..c609789 100644 --- a/elodie/tests/filesystem_test.py +++ b/elodie/tests/filesystem_test.py @@ -226,6 +226,38 @@ def test_get_folder_path_with_location(): assert path == os.path.join('2015-12-Dec','Sunnyvale'), path +def test_get_folder_path_with_int_in_source_path(): + # gh-239 + filesystem = FileSystem() + temporary_folder, folder = helper.create_working_folder('int') + + origin = os.path.join(folder,'plain.jpg') + shutil.copyfile(helper.get_file('plain.jpg'), origin) + + media = Photo(origin) + path = filesystem.get_folder_path(media.get_metadata()) + + assert path == os.path.join('2015-12-Dec','Unknown Location'), path + +@mock.patch('elodie.config.config_file', '%s/config.ini-int-in-path' % gettempdir()) +def test_get_folder_path_with_int_in_config_component(): + # gh-239 + with open('%s/config.ini-int-in-path' % gettempdir(), 'w') as f: + f.write(""" +[Directory] +date=%Y +full_path=%date + """) + if hasattr(load_config, 'config'): + del load_config.config + filesystem = FileSystem() + media = Photo(helper.get_file('plain.jpg')) + path = filesystem.get_folder_path(media.get_metadata()) + if hasattr(load_config, 'config'): + del load_config.config + + assert path == os.path.join('2015'), path + @mock.patch('elodie.config.config_file', '%s/config.ini-original-default-unknown-location' % gettempdir()) def test_get_folder_path_with_original_default_unknown_location(): with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f: diff --git a/elodie/tests/helper.py b/elodie/tests/helper.py index 07673fb..bc17951 100644 --- a/elodie/tests/helper.py +++ b/elodie/tests/helper.py @@ -27,9 +27,9 @@ def checksum(file_path, blocksize=65536): return hasher.hexdigest() return None -def create_working_folder(): +def create_working_folder(format=None): temporary_folder = tempfile.gettempdir() - folder = os.path.join(temporary_folder, random_string(10), random_string(10)) + folder = os.path.join(temporary_folder, random_string(10, format), random_string(10, format)) os.makedirs(folder) return (temporary_folder, folder) @@ -81,8 +81,14 @@ def populate_folder(number_of_files, include_invalid=False): return folder -def random_string(length): - return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(length)) +def random_string(length, format=None): + format_choice = string.ascii_uppercase + string.digits + if format == 'int': + format_choice = string.digits + elif format == 'str': + format_choice = string.asci_uppercase + + return ''.join(random.SystemRandom().choice(format_choice) for _ in range(length)) def random_decimal(): return random.random()