test: add comprehensive test coverage for dashboard
Phase 11 - Unit and Integration Tests Service Layer Tests (dashboard_service_test.go): - Test filterHiddenCollections with multiple scenarios - Test reorderCollections with custom orders - Test sortByPriority sorting logic - All 6 tests passing Handler Tests (dashboard_test.go): - Test BuildSections type conversion - Test textToString helper function - Test getViewAllURL mapping - All 7 tests passing Preview Tests (collections_preview_test.go): - Test preview endpoint validation - Test limit validation - Test rule validation - 6 test scenarios Integration Tests (dashboard_integration_test.go): - Test GET /api/dashboard/sections end-to-end - Test PUT /api/dashboard/preferences - Test POST /api/dashboard/restore-system-collection - Test authentication and validation - 9 test scenarios total Part of Carousel Dashboard Plan completion
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bookhoard/internal/database"
|
||||
"bookhoard/internal/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuildSections_ConvertsServiceTypesToHandlerTypes(t *testing.T) {
|
||||
itemUUID := uuid.New()
|
||||
|
||||
serviceSections := []services.DashboardSection{
|
||||
{
|
||||
CollectionID: uuid.UUID{},
|
||||
CollectionName: "continue-reading",
|
||||
QueryType: "continue-reading",
|
||||
Priority: 1,
|
||||
IsSystem: true,
|
||||
Title: "Continue Reading",
|
||||
Description: "Books you're currently reading",
|
||||
Icon: "📖",
|
||||
Items: []database.MediaItems{
|
||||
{
|
||||
ID: pgtype.UUID{Bytes: itemUUID, Valid: true},
|
||||
Title: "Test Book",
|
||||
Author: pgtype.Text{String: "Test Author", Valid: true},
|
||||
CoverImagePath: pgtype.Text{String: "/path/to/cover.jpg", Valid: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
CollectionID: uuid.UUID{},
|
||||
CollectionName: "my-favorites",
|
||||
QueryType: "filter",
|
||||
Priority: 10,
|
||||
IsSystem: false,
|
||||
Title: "My Favorites",
|
||||
Description: "My favorite books",
|
||||
Icon: "⭐",
|
||||
Items: []database.MediaItems{},
|
||||
},
|
||||
}
|
||||
|
||||
result := BuildSections(serviceSections)
|
||||
|
||||
assert.Equal(t, 2, len(result), "should have 2 sections")
|
||||
|
||||
section1 := result[0]
|
||||
assert.Equal(t, "continue-reading", section1.ID, "ID should match")
|
||||
assert.True(t, section1.IsSystem, "IsSystem should be boolean true")
|
||||
assert.Equal(t, "Continue Reading", section1.Title, "Title should be string")
|
||||
assert.Equal(t, "Books you're currently reading", section1.Description, "Description should be string")
|
||||
assert.Equal(t, "📖", section1.Icon, "Icon should be string")
|
||||
assert.Equal(t, 1, section1.Priority, "Priority should be number")
|
||||
assert.Equal(t, "/section/continue-reading", section1.ViewAllURL, "ViewAllURL should match system collection")
|
||||
|
||||
section2 := result[1]
|
||||
assert.Equal(t, "my-favorites", section2.ID)
|
||||
assert.False(t, section2.IsSystem, "IsSystem should be boolean false")
|
||||
assert.Equal(t, "My Favorites", section2.Title)
|
||||
assert.Equal(t, "", section2.ViewAllURL, "ViewAllURL should be empty for user collections")
|
||||
}
|
||||
|
||||
func TestBuildSections_ConvertsItemsCorrectly(t *testing.T) {
|
||||
itemUUID := uuid.New()
|
||||
|
||||
serviceSections := []services.DashboardSection{
|
||||
{
|
||||
CollectionID: uuid.UUID{},
|
||||
CollectionName: "continue-reading",
|
||||
QueryType: "continue-reading",
|
||||
Priority: 1,
|
||||
IsSystem: true,
|
||||
Title: "Continue Reading",
|
||||
Description: "Books you're currently reading",
|
||||
Icon: "📖",
|
||||
Items: []database.MediaItems{
|
||||
{
|
||||
ID: pgtype.UUID{Bytes: itemUUID, Valid: true},
|
||||
Title: "Test Book",
|
||||
Author: pgtype.Text{String: "Test Author", Valid: true},
|
||||
CoverImagePath: pgtype.Text{String: "/path/to/cover.jpg", Valid: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result := BuildSections(serviceSections)
|
||||
|
||||
assert.Equal(t, 1, len(result), "should have 1 section")
|
||||
section := result[0]
|
||||
assert.Equal(t, 1, len(section.Items), "should have 1 item")
|
||||
|
||||
item := section.Items[0]
|
||||
assert.Equal(t, itemUUID.String(), item.MediaItemID, "MediaItemID should match")
|
||||
assert.Equal(t, "Test Book", item.Title, "Title should be string")
|
||||
assert.Equal(t, "Test Author", item.Author, "Author should be string")
|
||||
assert.Equal(t, "/path/to/cover.jpg", item.CoverImagePath, "CoverImagePath should be string")
|
||||
}
|
||||
|
||||
func TestTextToString_Valid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input pgtype.Text
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "valid text",
|
||||
input: pgtype.Text{String: "hello", Valid: true},
|
||||
expected: "hello",
|
||||
},
|
||||
{
|
||||
name: "empty text",
|
||||
input: pgtype.Text{String: "", Valid: true},
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "invalid text",
|
||||
input: pgtype.Text{String: "ignored", Valid: false},
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := textToString(tt.input)
|
||||
assert.Equal(t, tt.expected, result, "textToString result mismatch")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetViewAllURL_SystemCollections(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
collectionName string
|
||||
queryType string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "continue-reading",
|
||||
collectionName: "continue-reading",
|
||||
queryType: "continue-reading",
|
||||
expected: "/section/continue-reading",
|
||||
},
|
||||
{
|
||||
name: "recently-added",
|
||||
collectionName: "recently-added",
|
||||
queryType: "recently-added",
|
||||
expected: "/section/recently-added",
|
||||
},
|
||||
{
|
||||
name: "recently-read",
|
||||
collectionName: "recently-read",
|
||||
queryType: "recently-read",
|
||||
expected: "/history",
|
||||
},
|
||||
{
|
||||
name: "not-started",
|
||||
collectionName: "not-started",
|
||||
queryType: "not-started",
|
||||
expected: "/section/not-started",
|
||||
},
|
||||
{
|
||||
name: "user collection",
|
||||
collectionName: "my-favorites",
|
||||
queryType: "filter",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := getViewAllURL(tt.collectionName, tt.queryType)
|
||||
assert.Equal(t, tt.expected, result, "getViewAllURL mismatch")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user