From 0298c589b162274a33a2e088f62521a872e5b9c4 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Thu, 26 Mar 2026 14:38:26 -0400 Subject: [PATCH] Fix library_id filter test for dev database compatibility Update TestCollectionSearchLibraryFilter to check for specific test books rather than exact counts, making tests resilient to changing dev database data. Changes: - Modified "no filter" test case to check both test books are present - Enhanced shouldContain to support comma-separated book ID lists - Added strings import for ID list processing - Skip exact count check when expectedCount is 0 Rationale: The library_id filter was working correctly. The test failure was due to running against a dev database with pre-existing data. When no library_id filter is provided, the API correctly returns all visible books across all libraries, not just test-created books. This validates that the filter works correctly while being resilient to dynamic dev database content. Fixes: #test-isolation-library-filter --- cmd/server/tests/search_test.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/server/tests/search_test.go b/cmd/server/tests/search_test.go index e51e17b..2aa38e2 100644 --- a/cmd/server/tests/search_test.go +++ b/cmd/server/tests/search_test.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/http/httptest" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -387,14 +388,14 @@ func TestCollectionSearchLibraryFilter(t *testing.T) { query string libraryID string expectedCount int - shouldContain string + shouldContain string // Comma-separated list of book IDs to check }{ { - name: "no filter - both books", + name: "no filter - both test books present", query: "Harry", libraryID: "", - expectedCount: 2, - shouldContain: "", // Either book + expectedCount: 0, // Skip exact count check (dev database has dynamic data) + shouldContain: book1ID + "," + book2ID, // Both test books should be present }, { name: "filter library 1", @@ -447,14 +448,21 @@ func TestCollectionSearchLibraryFilter(t *testing.T) { } if tt.shouldContain != "" { - found := false + // shouldContain can be comma-separated list of book IDs + requiredIDs := strings.Split(tt.shouldContain, ",") + resultIDs := make(map[string]bool) for _, book := range result { - if book["id"] == tt.shouldContain { - found = true - break + resultIDs[book["id"].(string)] = true + } + + missingIDs := []string{} + for _, requiredID := range requiredIDs { + if !resultIDs[requiredID] { + missingIDs = append(missingIDs, requiredID) } } - require.True(t, found, "Expected book %s not found in results", tt.shouldContain) + + require.Empty(t, missingIDs, "Expected books %s not found in results", strings.Join(missingIDs, ", ")) } }) }