refactor(tests): enhance test infrastructure with library/collection helpers

- Add LibraryTestData struct to TestDeviceSetup
- Implement CreateLibrary() for proper library creation in tests
- Implement CreateCollection() for test collection support
- Improve test isolation with dedicated library creation

This provides a more robust foundation for integration tests that need
proper library management support.
This commit is contained in:
2026-02-13 20:04:47 -05:00
parent 8ed0bdb040
commit 368c790c67
11 changed files with 849 additions and 556 deletions
+9 -18
View File
@@ -1,14 +1,10 @@
package main
import (
"bookhoard/internal/database"
"bookhoard/internal/handlers"
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/uuid"
@@ -42,11 +38,11 @@ func TestMediaBulkOperations(t *testing.T) {
// NEW: Database verification
for _, id := range mediaIDs {
pgID, err := uuid.FromBytes(id)
pgID, err := uuid.Parse(id)
require.NoError(t, err, "Should parse UUID from string")
_, err := setup.DB.GetMediaItem(context.Background(), pgtype.UUID{Bytes: [16]byte(pgID), Valid: true})
assert.Error(t, err, "Media item should be deleted from database")
_, dbErr := setup.DB.GetMediaItem(context.Background(), pgtype.UUID{Bytes: [16]byte(pgID), Valid: true})
assert.Error(t, dbErr, "Media item should be deleted from database")
}
})
@@ -321,20 +317,15 @@ func TestMediaBulkOperations(t *testing.T) {
assert.Equal(t, 4.0, result["total"])
// NEW: Verify database state
for i, mediaID := range []string{mediaID1, mediaID2, mediaID3, mediaID4} {
pgID := pgtype.UUID{Bytes: [16]byte(mediaID), Valid: true}
for _, mediaID := range []string{mediaID1, mediaID2, mediaID3, mediaID4} {
parsedID, err := uuid.Parse(mediaID)
require.NoError(t, err, "Should parse media ID UUID")
pgID := pgtype.UUID{Bytes: [16]byte(parsedID), Valid: true}
item, err := setup.DB.GetMediaItem(context.Background(), pgID)
assert.NoError(t, err, "Should retrieve media item")
if item.ReadingStatus.String == "reading" {
assert.Equal(t, true, item.ReadingStatus.Valid, "Reading status should still be true")
}
if item.ReadingStatus.String == "to-read" {
assert.Equal(t, true, item.ReadingStatus.Valid, "Reading status should be to-read")
}
if item.ReadingStatus.String == "did-not-finish" {
assert.Equal(t, true, item.ReadingStatus.Valid, "Reading status should be did-not-finish")
}
// Verify the item was created successfully
assert.True(t, item.ID.Valid, "Media item should have valid ID")
}
})