diff --git a/image_sorting_tool/tests/test_image_sort.py b/image_sorting_tool/tests/test_image_sort.py index 6dd7505..fdd868a 100644 --- a/image_sorting_tool/tests/test_image_sort.py +++ b/image_sorting_tool/tests/test_image_sort.py @@ -24,6 +24,26 @@ ] +@pytest.fixture(name="test_setup") +def fixture_setup_tmp_dirs_and_sorter(tmp_path): + """ + Creates a tmp directory and a sorter initialized with these directories + It then cleans up sorter after test completes + """ + # Create tmp directories for the test + tmp_src = os.path.abspath(os.path.join(tmp_path, "src/")) + tmp_dst = os.path.abspath(os.path.join(tmp_path, "dst/")) + os.mkdir(tmp_src) + os.mkdir(tmp_dst) + sorter = ImageSort(tmp_src, tmp_dst, None) + + # Run test + yield tmp_src, tmp_dst, sorter + + # Cleanup child threads + sorter.cleanup() + + @pytest.mark.parametrize( "test_extensions,expected_sort", [ @@ -53,14 +73,11 @@ ), ], ) # Tests each filetype indiviually and then collectively -def test_sort_images(tmp_path, test_extensions, expected_sort): +def test_sort_images(test_setup, test_extensions, expected_sort): """Test that the tool can sort the images in the test assets directory""" - # Create tmp directories for the test - tmp_src = os.path.abspath(os.path.join(tmp_path, "src/")) - tmp_dst = os.path.abspath(os.path.join(tmp_path, "dst/")) - os.mkdir(tmp_src) - os.mkdir(tmp_dst) + # Expand test setup + tmp_src, tmp_dst, sorter = test_setup # Add the tmp directory to the ground truths sorted_gt = [ @@ -72,7 +89,6 @@ def test_sort_images(tmp_path, test_extensions, expected_sort): shutil.copy2(asset, tmp_src) # Run the sorting - sorter = ImageSort(tmp_src, tmp_dst, None) sorter.ext_to_sort = test_extensions sorter.find_images() sorter.run_parallel_sorting() @@ -84,14 +100,7 @@ def test_sort_images(tmp_path, test_extensions, expected_sort): sorted_list.append(os.path.join(root_path, file_name)) # Check sorted images where sorted correctly - sorted_list.sort() - sorted_gt.sort() - assert len(sorted_list) == len(sorted_gt) - assert all( - sort_path == gt_path for sort_path, gt_path in zip(sorted_list, sorted_gt) - ) - # Cleanup child threads - sorter.cleanup() + assert set(sorted_list) == set(sorted_gt) @pytest.mark.parametrize( @@ -102,16 +111,18 @@ def test_sort_images(tmp_path, test_extensions, expected_sort): ([".mp4"]), ([".gif"]), (JPEG_EXTENSIONS + [".png", ".mp4", ".gif"]), + ([]), ], ) -def test_find_other_files(tmp_path, test_extensions): +def test_find_other_files(test_setup, test_extensions): """Test that the tool can find unsorted files in the test assets directory""" + # Expand test setup + tmp_path, _, sorter = test_setup # Copy test assets to a tmp_path tmp_assets = [shutil.copy2(asset, tmp_path) for asset in MIXED_TEST_ASSETS] # Run finding tool - sorter = ImageSort(tmp_path, tmp_path, None) sorter.ext_to_sort = test_extensions sorter.find_images() sorter_found_list = [sorter.files_list[i].fullpath for i in sorter.other_list] @@ -122,17 +133,8 @@ def test_find_other_files(tmp_path, test_extensions): if not asset.lower().endswith(tuple(test_extensions)): unsorted_assets.append(asset) - # Ensure finder found right number of images - assert len(unsorted_assets) == len(sorter_found_list) - - # Sort found images lists and compare to the source list to ensure it is identical - unsorted_assets.sort() - sorter_found_list.sort() - print(f"unsorted_assets : {unsorted_assets}") - print(f"other_list : {sorter_found_list}") - assert all(src == found for src, found in zip(unsorted_assets, sorter_found_list)) - # Cleanup child threads - sorter.cleanup() + # Ensure finder found images correctly + assert set(unsorted_assets) == set(sorter_found_list) @pytest.mark.parametrize( @@ -203,17 +205,27 @@ def test_find_other_files(tmp_path, test_extensions): "2017/05/20170512_184655.png", ], ), + ( # No extensions to sort + [], + [ + "other_files/no_exif.jpg", + "other_files/no_exif20000101-010101.jpg", + "other_files/pass_0.JPG", + "other_files/pass_1.JPG", + "other_files/Screenshot 2017-05-12 18.46.55.png", + "other_files/text.txt", + "other_files/20180930_165600.mp4", + "other_files/Animated_2018-0305_093556.gif", + ], + ), ], ) # Tests each filetype indiviually and then collectively -def test_copy_images(tmp_path, test_extensions, expected_result): +def test_copy_images(test_setup, test_extensions, expected_result): """Test that the tool can copy the unsorted files in the test assets directory when the user selects this option. """ - # Create tmp directories for the test - tmp_src = os.path.abspath(os.path.join(tmp_path, "src/")) - tmp_dst = os.path.abspath(os.path.join(tmp_path, "dst/")) - os.mkdir(tmp_src) - os.mkdir(tmp_dst) + # Expand setup + tmp_src, tmp_dst, sorter = test_setup # Add the tmp directory to the ground truths expected_result = [ @@ -223,7 +235,6 @@ def test_copy_images(tmp_path, test_extensions, expected_result): # Copy test assets to a tmp_path for asset in MIXED_TEST_ASSETS: shutil.copy2(asset, tmp_src) - sorter = ImageSort(tmp_src, tmp_dst, None) sorter.ext_to_sort = test_extensions # Enable the copy feature sorter.copy_unsorted = True @@ -239,24 +250,13 @@ def test_copy_images(tmp_path, test_extensions, expected_result): sorted_list.append(os.path.join(root_path, file_name)) # Check unsortable files were copied correctly - sorted_list.sort() - expected_result.sort() - assert len(sorted_list) == len(expected_result) - print(f"sorted_list : {sorted_list}") - print(f"expected result : {expected_result}") - for result, exp_result in zip(sorted_list, expected_result): - print(result, exp_result) - assert all( - sort_path == gt_path for sort_path, gt_path in zip(sorted_list, expected_result) - ) - # Cleanup child threads - sorter.cleanup() + assert set(sorted_list) == set(expected_result) @pytest.mark.parametrize( - "test_extensions,expected_result,test_assets", + "test_extensions, expected_result, test_assets", [ - ( + ( # Test burst shot images are renamed correctly JPEG_EXTENSIONS, [ "2013/04/20130408_131738_001.jpeg", @@ -267,10 +267,11 @@ def test_copy_images(tmp_path, test_extensions, expected_result): "2013/04/20130408_131738_006.jpeg", "2013/04/20130408_131738_007.jpeg", "2013/04/20130408_131738_008.jpeg", + "2013/04/20130408_131738_009.jpeg", ], BURST_TEST_ASSETS, ), - ( + ( # Test that mixed assets without duplicates still behaves correctly JPEG_EXTENSIONS + [".png", ".mp4", ".gif"], [ "failed_to_sort/no_exif.jpg", @@ -284,18 +285,15 @@ def test_copy_images(tmp_path, test_extensions, expected_result): MIXED_TEST_ASSETS, ), ], -) # Tests each filetype indiviually and then collectively +) def test_rename_duplicates_images( - tmp_path, test_extensions, expected_result, test_assets + test_setup, test_extensions, expected_result, test_assets ): """Test that the tool can rename duplicates in the test assets directory when the user selects this option. """ - # Create tmp directories for the test - tmp_src = os.path.abspath(os.path.join(tmp_path, "src/")) - tmp_dst = os.path.abspath(os.path.join(tmp_path, "dst/")) - os.mkdir(tmp_src) - os.mkdir(tmp_dst) + # Expand test setup + tmp_src, tmp_dst, sorter = test_setup # Add the tmp directory to the ground truths expected_result = [ @@ -305,9 +303,9 @@ def test_rename_duplicates_images( # Copy test assets to a tmp_path for asset in test_assets: shutil.copy2(asset, tmp_src) - sorter = ImageSort(tmp_src, tmp_dst, None) sorter.ext_to_sort = test_extensions - # Enable the copy feature + + # Enable the rename feature sorter.rename_duplicates = True sorter.find_images() @@ -321,15 +319,4 @@ def test_rename_duplicates_images( sorted_list.append(os.path.join(root_path, file_name)) # Check duplicate files were copied correctly - sorted_list.sort() - expected_result.sort() - print(f"sorted_list : {sorted_list}") - print(f"expected result : {expected_result}") - assert len(sorted_list) == len(expected_result) - for result, exp_result in zip(sorted_list, expected_result): - print(result, exp_result) - assert all( - sort_path == gt_path for sort_path, gt_path in zip(sorted_list, expected_result) - ) - # Cleanup child threads - sorter.cleanup() + assert set(sorted_list) == set(expected_result)