Files
gochat/internal/service/portal_service_test.go_BAK
T
2026-06-04 15:44:48 +08:00

100 lines
2.8 KiB
Plaintext

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPortalService_Create(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal, err := svc.Create(context.Background(), account.ID, &CreatePortalRequest{
Name: "My Portal",
Slug: "my-portal",
Description: "A test portal",
Locale: "en",
})
require.NoError(t, err)
assert.Equal(t, "My Portal", portal.Name)
assert.Equal(t, "my-portal", portal.Slug)
assert.Equal(t, account.ID, portal.AccountID)
assert.False(t, portal.Archived)
}
func TestPortalService_GetByID(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
found, err := svc.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, portal.Name, found.Name)
assert.Equal(t, portal.Slug, found.Slug)
}
func TestPortalService_GetByID_NotFound(t *testing.T) {
_, _, svc := setupPortalService(t)
_, err := svc.GetByID(context.Background(), 99999)
assert.Error(t, err)
}
func TestPortalService_Update(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
updated, err := svc.Update(context.Background(), portal.ID, &UpdatePortalRequest{
Name: "Updated Portal",
Description: "Updated description",
})
require.NoError(t, err)
assert.Equal(t, "Updated Portal", updated.Name)
assert.Equal(t, "Updated description", updated.Description)
}
func TestPortalService_Update_Archive(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
archived := true
updated, err := svc.Update(context.Background(), portal.ID, &UpdatePortalRequest{
Archived: &archived,
})
require.NoError(t, err)
assert.True(t, updated.Archived)
}
func TestPortalService_Delete(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
portal := createTestPortal(t, db, account.ID)
err := svc.Delete(context.Background(), portal.ID)
require.NoError(t, err)
_, err = svc.GetByID(context.Background(), portal.ID)
assert.Error(t, err)
}
func TestPortalService_ListByAccountID(t *testing.T) {
db, _, svc := setupPortalService(t)
account := createTestAccount(t, db)
// Create 2 portals
_, err := svc.Create(context.Background(), account.ID, &CreatePortalRequest{Name: "P1", Slug: "p1", Locale: "en"})
require.NoError(t, err)
_, err = svc.Create(context.Background(), account.ID, &CreatePortalRequest{Name: "P2", Slug: "p2", Locale: "en"})
require.NoError(t, err)
portals, count, err := svc.ListByAccountID(context.Background(), account.ID, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(2), count)
assert.Len(t, portals, 2)
}