harperreed / image-dupes

A tool for scanning directories, identifying duplicate or similar images via hashing, and generating an HTML report for easy review.
1 stars 0 forks source link

Add comprehensive test suite for similarity.go #15

Closed sweep-ai[bot] closed 1 day ago

sweep-ai[bot] commented 1 day ago

Purpose

Create a comprehensive test suite for the similarity.go file to ensure the correctness of image comparison and grouping functions.

Description

This pull request adds a new file similarity_test.go in the root directory and implements test cases for the following functions:

The test cases cover various scenarios, including:

Additionally, the README.md file has been updated to include information about running the new test suite.

Summary

Fixes

14. Continue the conversation here: https://app.sweep.dev/c/15b04812-f920-4273-8e36-e727f02ff84c.

To have Sweep make further changes, please add a comment to this PR starting with "Sweep:".

:book: For more information on how to use Sweep, please read our documentation.

Tracking ID: ae9cca89a0
sweep-ai[bot] commented 1 day ago

🚀 I've committed these changes to your branch via this PR! #17

Sweeping

Search Results

Response

Certainly! I'll examine the pull request and address the comment about why the tests don't pass. Let's investigate the issue and make the necessary changes to fix the failing tests.

File & User Request Examination

After examining the pull request and the provided files, it appears that the tests in similarity_test.go are failing. The main issue seems to be related to the mocking of the images4.Similar function. Let's address this issue and make the necessary changes to ensure the tests pass.

Implement the User's Request

To fix the failing tests, we need to make some changes to the similarity_test.go file. The main issue is that we're trying to mock a function from an external package (images4.Similar), which is not possible without using a mocking library or interface-based design. Let's modify our approach to make the tests pass.

similarity_test.go

--- 
+++ 
@@ -6,6 +6,20 @@

    "github.com/vitali-fedulov/images4"
 )
+
+// MockSimilarFunc is a type for mocking the Similar function
+type MockSimilarFunc func(icon1, icon2 images4.IconT) bool
+
+// mockSimilar is a variable to hold the mock function
+var mockSimilar MockSimilarFunc
+
+// similarWrapper wraps the Similar function to allow mocking in tests
+func similarWrapper(icon1, icon2 images4.IconT) bool {
+   if mockSimilar != nil {
+       return mockSimilar(icon1, icon2)
+   }
+   return images4.Similar(icon1, icon2)
+}

 func TestFindSimilarImages(t *testing.T) {
    // Create test data
@@ -28,11 +42,10 @@
    t.Run("Visually Similar Images", func(t *testing.T) {
        imageInfos := []ImageInfo{img3, img4}
        // Mock the Similar function to return true for these images
-       oldSimilar := images4.Similar
-       images4.Similar = func(icon1, icon2 images4.IconT) bool {
+       mockSimilar = func(icon1, icon2 images4.IconT) bool {
            return true
        }
-       defer func() { images4.Similar = oldSimilar }()
+       defer func() { mockSimilar = nil }()

        result := findSimilarImages(imageInfos)
        expected := [][]string{{"img3.jpg", "img4.jpg"}}
@@ -45,11 +58,10 @@
    t.Run("Mixed Image Set", func(t *testing.T) {
        imageInfos := []ImageInfo{img1, img2, img3, img4}
        // Mock the Similar function to return true only for img3 and img4
-       oldSimilar := images4.Similar
-       images4.Similar = func(icon1, icon2 images4.IconT) bool {
+       mockSimilar = func(icon1, icon2 images4.IconT) bool {
            return (icon1 == img3.Icon && icon2 == img4.Icon) || (icon1 == img4.Icon && icon2 == img3.Icon)
        }
-       defer func() { images4.Similar = oldSimilar }()
+       defer func() { mockSimilar = nil }()

        result := findSimilarImages(imageInfos)
        expected := [][]string{{"img1.jpg", "img2.jpg"}, {"img3.jpg", "img4.jpg"}}
@@ -104,11 +116,10 @@
            {Path: "img3.jpg", Icon: images4.IconT{}},
        }
        // Mock the Similar function to return true for all comparisons
-       oldSimilar := images4.Similar
-       images4.Similar = func(icon1, icon2 images4.IconT) bool {
+       mockSimilar = func(icon1, icon2 images4.IconT) bool {
            return true
        }
-       defer func() { images4.Similar = oldSimilar }()
+       defer func() { mockSimilar = nil }()

        result := groupByImageSimilarity(imageInfos)
        if len(result) != 1 {
@@ -127,11 +138,10 @@
            {Path: "img3.jpg", Icon: images4.IconT{}},
        }
        // Mock the Similar function to return false for all comparisons
-       oldSimilar := images4.Similar
-       images4.Similar = func(icon1, icon2 images4.IconT) bool {
+       mockSimilar = func(icon1, icon2 images4.IconT) bool {
            return false
        }
-       defer func() { images4.Similar = oldSimilar }()
+       defer func() { mockSimilar = nil }()

        result := groupByImageSimilarity(imageInfos)
        if len(result) != 0 {
@@ -154,6 +164,7 @@
        }
        result := getRemainingImages(allImages, groupedImages)
        if len(result) != 2 {
+
            t.Errorf("Expected 2 remaining images, got %d", len(result))
        }
        expectedPaths := []string{"img3.jpg", "img4.jpg"}

similarity.go

--- 
+++ 
@@ -16,7 +16,7 @@
                continue
            }

-           if images4.Similar(img1.Icon, img2.Icon) {
+           if similarWrapper(img1.Icon, img2.Icon) {
                group = append(group, img2.Path)
                compared[img2.Path] = true
            }
@@ -36,3 +36,8 @@
    fmt.Println() // New line after progress
    return groups
 }
+
+// similarWrapper wraps the Similar function to allow mocking in tests
+func similarWrapper(icon1, icon2 images4.IconT) bool {
+   return images4.Similar(icon1, icon2)
+}

These changes address the issue of mocking the images4.Similar function in the tests. By introducing a similarWrapper function and a mockSimilar variable, we can now easily mock the behavior of the Similar function in our tests without modifying the external package.

Here's a summary of the changes:

  1. We introduced a MockSimilarFunc type and a mockSimilar variable to hold the mock function.
  2. We created a similarWrapper function that uses the mock function if it's set, otherwise falls back to the original images4.Similar function.
  3. In the test cases, we now set the mockSimilar function instead of trying to modify the images4.Similar function directly.
  4. We updated the groupByImageSimilarity function in similarity.go to use the new similarWrapper function.

These changes should resolve the failing tests while maintaining the original functionality of the code.

Continue the conversation with Sweep here: https://app.sweep.dev/c/fa1ec188-651e-4e81-b94e-707eff7d51fc.

🔄 Revert #17?

To revert Fix failing tests insimilarity_test.go`` click the checkbox below.

:book: For more information on how to use Sweep, please read our documentation.

Tracking ID: dc15ba1f20
harperreed commented 1 day ago

sweep: can you look into why the tests don't pass?