Skip to content

Commit

Permalink
test: cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorpeJosh committed Oct 23, 2023
1 parent 0051790 commit 26886bc
Showing 1 changed file with 58 additions and 71 deletions.
129 changes: 58 additions & 71 deletions image_sorting_tool/tests/test_image_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down Expand Up @@ -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 = [
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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]
Expand All @@ -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(
Expand Down Expand Up @@ -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 = [
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 = [
Expand All @@ -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()

Expand All @@ -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)

0 comments on commit 26886bc

Please sign in to comment.