167 lines
4.3 KiB
Go
167 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/repository"
|
|
)
|
|
|
|
// ========== Test Setup ==========
|
|
|
|
func setupFolderService(t *testing.T) (*FolderService, func()) {
|
|
t.Helper()
|
|
db := setupServiceTestDB(t)
|
|
repo := repository.NewFolderRepo(db)
|
|
svc := NewFolderService(repo)
|
|
return svc, func() {}
|
|
}
|
|
|
|
// Helper: create a full chain of account -> portal for folder tests
|
|
func setupFolderTestChain(t *testing.T) (*FolderService, uint, uint) {
|
|
t.Helper()
|
|
db := setupServiceTestDB(t)
|
|
repo := repository.NewFolderRepo(db)
|
|
svc := NewFolderService(repo)
|
|
|
|
account := createTestAccount(t, db)
|
|
portal := createTestPortal(t, db, account.ID)
|
|
|
|
return svc, portal.ID, account.ID
|
|
}
|
|
|
|
// ========== Create ==========
|
|
|
|
func TestFolderService_Create(t *testing.T) {
|
|
svc, portalID, _ := setupFolderTestChain(t)
|
|
|
|
req := &CreateFolderRequest{
|
|
Name: "Documentation",
|
|
}
|
|
|
|
folder, err := svc.Create(context.Background(), portalID, req)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, folder.ID)
|
|
assert.Equal(t, portalID, folder.PortalID)
|
|
assert.Equal(t, "Documentation", folder.Name)
|
|
}
|
|
|
|
// ========== GetByID ==========
|
|
|
|
func TestFolderService_GetByID(t *testing.T) {
|
|
svc, portalID, _ := setupFolderTestChain(t)
|
|
|
|
req := &CreateFolderRequest{
|
|
Name: "API Docs",
|
|
}
|
|
created, err := svc.Create(context.Background(), portalID, req)
|
|
require.NoError(t, err)
|
|
|
|
folder, err := svc.GetByID(context.Background(), created.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, created.ID, folder.ID)
|
|
assert.Equal(t, "API Docs", folder.Name)
|
|
assert.Equal(t, portalID, folder.PortalID)
|
|
}
|
|
|
|
func TestFolderService_GetByID_NotFound(t *testing.T) {
|
|
svc, _, _ := setupFolderTestChain(t)
|
|
|
|
folder, err := svc.GetByID(context.Background(), 99999)
|
|
assert.Nil(t, folder)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// ========== Update ==========
|
|
|
|
func TestFolderService_Update(t *testing.T) {
|
|
svc, portalID, _ := setupFolderTestChain(t)
|
|
|
|
req := &CreateFolderRequest{
|
|
Name: "Old Name",
|
|
}
|
|
created, err := svc.Create(context.Background(), portalID, req)
|
|
require.NoError(t, err)
|
|
|
|
updateReq := &UpdateFolderRequest{
|
|
Name: "New Name",
|
|
}
|
|
updated, err := svc.Update(context.Background(), created.ID, updateReq)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "New Name", updated.Name)
|
|
assert.Equal(t, created.ID, updated.ID)
|
|
}
|
|
|
|
func TestFolderService_Update_NotFound(t *testing.T) {
|
|
svc, _, _ := setupFolderTestChain(t)
|
|
|
|
updateReq := &UpdateFolderRequest{
|
|
Name: "Does Not Matter",
|
|
}
|
|
updated, err := svc.Update(context.Background(), 99999, updateReq)
|
|
assert.Nil(t, updated)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// ========== Delete ==========
|
|
|
|
func TestFolderService_Delete(t *testing.T) {
|
|
svc, portalID, _ := setupFolderTestChain(t)
|
|
|
|
req := &CreateFolderRequest{
|
|
Name: "To Be Deleted",
|
|
}
|
|
created, err := svc.Create(context.Background(), portalID, req)
|
|
require.NoError(t, err)
|
|
|
|
err = svc.Delete(context.Background(), created.ID)
|
|
require.NoError(t, err)
|
|
|
|
// Verify it's actually gone
|
|
folder, err := svc.GetByID(context.Background(), created.ID)
|
|
assert.Nil(t, folder)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestFolderService_Delete_NotFound(t *testing.T) {
|
|
svc, _, _ := setupFolderTestChain(t)
|
|
|
|
// Delete checks existence first, so deleting a non-existent folder should error
|
|
err := svc.Delete(context.Background(), 99999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// ========== ListByPortalID ==========
|
|
|
|
func TestFolderService_ListByPortalID(t *testing.T) {
|
|
svc, portalID, _ := setupFolderTestChain(t)
|
|
|
|
// Create multiple folders
|
|
names := []string{"Folder A", "Folder B", "Folder C"}
|
|
for _, name := range names {
|
|
req := &CreateFolderRequest{Name: name}
|
|
_, err := svc.Create(context.Background(), portalID, req)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// List all (offset=0, limit=10)
|
|
folders, count, err := svc.ListByPortalID(context.Background(), portalID, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
assert.Len(t, folders, 3)
|
|
|
|
// List with pagination (offset=1, limit=2)
|
|
folders2, count2, err := svc.ListByPortalID(context.Background(), portalID, 1, 2)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), count2)
|
|
assert.Len(t, folders2, 2)
|
|
|
|
// List for a portal with no folders (use a different portal ID)
|
|
folders3, count3, err := svc.ListByPortalID(context.Background(), 88888, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(0), count3)
|
|
assert.Len(t, folders3, 0)
|
|
} |