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"
"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, ", "))
}
})
}