test(collections): replace broken unit tests with integration tests

Removed unit tests that couldn't work without a database (nil db would
panic). Added comprehensive integration tests for the PreviewCollection
endpoint covering:
- Authentication (no auth, valid auth)
- Input validation (missing/invalid library ID, invalid JSON)
- Manual book selection
- Rule-based filtering
- Limit parameter handling
- Duplicate and invalid book ID handling
This commit is contained in:
2026-02-20 17:04:15 -05:00
parent 33fcc416b9
commit 66e0b61200
2 changed files with 391 additions and 181 deletions
@@ -13,7 +13,6 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPreviewCollection_NoAuth(t *testing.T) {
@@ -97,183 +96,3 @@ func TestPreviewCollection_InvalidLibraryID(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, rec.Code)
}
func TestPreviewCollection_LimitValidation(t *testing.T) {
e := echo.New()
handler := &CollectionHandler{}
libraryID := uuid.New()
tests := []struct {
name string
limit int
expectedStatus int
}{
{
name: "valid limit 10",
limit: 10,
expectedStatus: http.StatusBadRequest, // No actual library, so will return error
},
{
name: "valid limit 20",
limit: 20,
expectedStatus: http.StatusBadRequest, // No actual library, so will return error
},
{
name: "limit too high (101)",
limit: 101,
expectedStatus: http.StatusBadRequest, // Limit validation
},
{
name: "limit zero",
limit: 0,
expectedStatus: http.StatusBadRequest, // Should default to 20, but library doesn't exist
},
{
name: "negative limit",
limit: -5,
expectedStatus: http.StatusBadRequest, // No actual library
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reqBody := map[string]interface{}{
"library_id": libraryID.String(),
"rules": []map[string]interface{}{},
"manual_book_ids": []string{},
"limit": tt.limit,
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
// Mock user
user := database.Users{
ID: pgtype.UUID{Bytes: uuid.New(), Valid: true},
}
c := e.NewContext(req, rec)
c.Set("user", user)
err := handler.PreviewCollection(c)
require.NoError(t, err)
assert.Equal(t, tt.expectedStatus, rec.Code)
})
}
}
func TestPreviewCollection_RuleValidation(t *testing.T) {
e := echo.New()
handler := &CollectionHandler{}
libraryID := uuid.New()
tests := []struct {
name string
rules interface{}
expectedStatus int
}{
{
name: "empty rules array",
rules: []map[string]interface{}{},
expectedStatus: http.StatusBadRequest, // Library doesn't exist
},
{
name: "valid rule structure",
rules: []map[string]interface{}{
{
"id": "rule1",
"field": "genre",
"operator": "equals",
"value": "Sci-Fi",
"priority": 1,
},
},
expectedStatus: http.StatusBadRequest, // Library doesn't exist
},
{
name: "multiple rules",
rules: []map[string]interface{}{
{
"id": "rule1",
"field": "genre",
"operator": "equals",
"value": "Sci-Fi",
"priority": 1,
},
{
"id": "rule2",
"field": "author",
"operator": "contains",
"value": "Asimov",
"priority": 2,
},
},
expectedStatus: http.StatusBadRequest, // Library doesn't exist
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reqBody := map[string]interface{}{
"library_id": libraryID.String(),
"rules": tt.rules,
"manual_book_ids": []string{},
"limit": 20,
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
// Mock user
user := database.Users{
ID: pgtype.UUID{Bytes: uuid.New(), Valid: true},
}
c := e.NewContext(req, rec)
c.Set("user", user)
err := handler.PreviewCollection(c)
require.NoError(t, err)
assert.Equal(t, tt.expectedStatus, rec.Code)
})
}
}
func TestPreviewCollection_ManualBookSelection(t *testing.T) {
e := echo.New()
handler := &CollectionHandler{}
libraryID := uuid.New()
reqBody := map[string]interface{}{
"library_id": libraryID.String(),
"rules": []map[string]interface{}{},
"manual_book_ids": []string{
uuid.New().String(),
uuid.New().String(),
uuid.New().String(),
},
"limit": 20,
}
body, _ := json.Marshal(reqBody)
req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
// Mock user
user := database.Users{
ID: pgtype.UUID{Bytes: uuid.New(), Valid: true},
}
c := e.NewContext(req, rec)
c.Set("user", user)
err := handler.PreviewCollection(c)
require.NoError(t, err)
// Should return 400 because library doesn't exist
assert.Equal(t, http.StatusBadRequest, rec.Code)
}