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
This commit is contained in:
2026-03-26 14:38:26 -04:00
parent a900c78faf
commit 0298c589b1
+17 -9
View File
@@ -6,6 +6,7 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@@ -387,14 +388,14 @@ func TestCollectionSearchLibraryFilter(t *testing.T) {
query string query string
libraryID string libraryID string
expectedCount int 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", query: "Harry",
libraryID: "", libraryID: "",
expectedCount: 2, expectedCount: 0, // Skip exact count check (dev database has dynamic data)
shouldContain: "", // Either book shouldContain: book1ID + "," + book2ID, // Both test books should be present
}, },
{ {
name: "filter library 1", name: "filter library 1",
@@ -447,14 +448,21 @@ func TestCollectionSearchLibraryFilter(t *testing.T) {
} }
if tt.shouldContain != "" { 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 { for _, book := range result {
if book["id"] == tt.shouldContain { resultIDs[book["id"].(string)] = true
found = true }
break
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, ", "))
} }
}) })
} }