120 lines
3.4 KiB
Plaintext
120 lines
3.4 KiB
Plaintext
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ========== Article test helpers ==========
|
|
|
|
// setupArticleService creates ArticleRepo + ArticleService test instances.
|
|
func setupArticleService(t *testing.T) (*gorm.DB, *repository.ArticleRepo, *ArticleService) {
|
|
t.Helper()
|
|
db := setupServiceTestDB(t)
|
|
repo := repository.NewArticleRepo(db)
|
|
svc := NewArticleService(repo)
|
|
return db, repo, svc
|
|
}
|
|
|
|
// createArticleServiceTestArticle creates a test article in the given portal/account context.
|
|
func createArticleServiceTestArticle(t *testing.T, db *gorm.DB, accountID, portalID uint, authorID uint, status model.ArticleStatus) *model.Article {
|
|
t.Helper()
|
|
authorIDPtr := &authorID
|
|
slug := "test-article-" + string(status)
|
|
article := &model.Article{
|
|
PortalID: portalID,
|
|
AccountID: accountID,
|
|
AuthorID: authorIDPtr,
|
|
Title: "Test Article",
|
|
Slug: slug,
|
|
Content: "Test content",
|
|
Status: string(status),
|
|
}
|
|
if err := db.Create(article).Error; err != nil {
|
|
t.Fatalf("failed to create test article: %v", err)
|
|
}
|
|
return article
|
|
}
|
|
|
|
// ========== BulkActions ==========
|
|
|
|
func TestArticleService_BulkActions_Publish(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
// Create a portal
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-pub",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusDraft)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "publish",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
updated, err := svc.Get(context.Background(), account.ID, article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(model.ArticleStatusPublished), updated.Status)
|
|
}
|
|
|
|
func TestArticleService_BulkActions_Archive(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-archive",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusPublished)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "archive",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
updated, err := svc.Get(context.Background(), account.ID, article.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(model.ArticleStatusArchived), updated.Status)
|
|
}
|
|
|
|
func TestArticleService_BulkActions_Delete(t *testing.T) {
|
|
db, _, svc := setupArticleService(t)
|
|
account := createTestAccount(t, db)
|
|
|
|
portal := &model.Portal{
|
|
AccountID: account.ID,
|
|
Name: "Test Portal",
|
|
Slug: "test-portal-bulk-delete",
|
|
}
|
|
require.NoError(t, db.Create(portal).Error)
|
|
|
|
article := createArticleServiceTestArticle(t, db, account.ID, portal.ID, 1, model.ArticleStatusDraft)
|
|
|
|
err := svc.BulkAction(context.Background(), account.ID, &ArticleBulkActionRequest{
|
|
Action: "delete",
|
|
ArticleIDs: []uint{article.ID},
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = svc.Get(context.Background(), account.ID, article.ID)
|
|
assert.Error(t, err) // deleted article should not be found
|
|
} |