Update test files to work with recent backend refactoring changes.
Test changes in internal/services/dashboard_service_test.go:
- Fix method name casing for FilterHiddenCollections
- Change from filterHiddenCollections (lowercase 'f')
- Change to FilterHiddenCollections (uppercase 'F')
- Matches exported method signature in DashboardService
- Line 57: Update test call to use correct exported method
Test changes in internal/handlers/dashboard_test.go:
- Update getViewAllURL test to match simplified function signature
- Remove queryType parameter from test call
- Function now only takes collectionName parameter
- Aligns with refactoring to use /collections/{id} routing
- Line 178: Update test call to use new signature
These fixes ensure tests compile and run correctly after the
collection detail page refactoring where:
1. getViewAllURL() was simplified to return /collections/{id}
2. System collections now use the same routing as user collections
183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
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)
|
|
assert.Equal(t, tt.expected, result, "getViewAllURL mismatch")
|
|
})
|
|
}
|
|
}
|