160 lines
4.2 KiB
Go
160 lines
4.2 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
"quyun/v2/app/commands/testx"
|
|
content_dto "quyun/v2/app/http/v1/dto"
|
|
"quyun/v2/app/requests"
|
|
"quyun/v2/database"
|
|
"quyun/v2/database/models"
|
|
"quyun/v2/pkg/consts"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
"github.com/spf13/cast"
|
|
"github.com/stretchr/testify/suite"
|
|
"go.ipao.vip/atom/contracts"
|
|
"go.uber.org/dig"
|
|
)
|
|
|
|
type ContentTestSuiteInjectParams struct {
|
|
dig.In
|
|
|
|
DB *sql.DB
|
|
Initials []contracts.Initial `group:"initials"`
|
|
}
|
|
|
|
type ContentTestSuite struct {
|
|
suite.Suite
|
|
ContentTestSuiteInjectParams
|
|
}
|
|
|
|
func Test_Content(t *testing.T) {
|
|
providers := testx.Default().With(Provide)
|
|
|
|
testx.Serve(providers, t, func(p ContentTestSuiteInjectParams) {
|
|
suite.Run(t, &ContentTestSuite{ContentTestSuiteInjectParams: p})
|
|
})
|
|
}
|
|
|
|
func (s *ContentTestSuite) Test_List() {
|
|
Convey("List", s.T(), func() {
|
|
ctx := s.T().Context()
|
|
database.Truncate(ctx, s.DB, models.TableNameContent, models.TableNameUser)
|
|
|
|
// Create Author
|
|
author := &models.User{Nickname: "Author1", Username: "author1", Phone: "13800000001"}
|
|
models.UserQuery.WithContext(ctx).Create(author)
|
|
|
|
// Create Contents
|
|
c1 := &models.Content{
|
|
TenantID: 1,
|
|
UserID: author.ID,
|
|
Title: "Content A",
|
|
Status: consts.ContentStatusPublished,
|
|
Genre: "video",
|
|
}
|
|
c2 := &models.Content{
|
|
TenantID: 1,
|
|
UserID: author.ID,
|
|
Title: "Content B",
|
|
Status: consts.ContentStatusDraft, // Draft
|
|
Genre: "video",
|
|
}
|
|
models.ContentQuery.WithContext(ctx).Create(c1, c2)
|
|
|
|
Convey("should list only published contents", func() {
|
|
filter := &content_dto.ContentListFilter{
|
|
TenantID: "1",
|
|
Pagination: requests.Pagination{
|
|
Page: 1,
|
|
Limit: 10,
|
|
},
|
|
}
|
|
res, err := Content.List(ctx, filter)
|
|
So(err, ShouldBeNil)
|
|
So(res.Total, ShouldEqual, 1)
|
|
items := res.Items.([]content_dto.ContentItem)
|
|
So(items[0].Title, ShouldEqual, "Content A")
|
|
So(items[0].AuthorName, ShouldEqual, "Author1")
|
|
})
|
|
})
|
|
}
|
|
|
|
func (s *ContentTestSuite) Test_Get() {
|
|
Convey("Get", s.T(), func() {
|
|
ctx := s.T().Context()
|
|
database.Truncate(ctx, s.DB, models.TableNameContent, models.TableNameMediaAsset, models.TableNameContentAsset, models.TableNameUser)
|
|
|
|
// Author
|
|
author := &models.User{Nickname: "Author1", Username: "author1", Phone: "13800000002"}
|
|
models.UserQuery.WithContext(ctx).Create(author)
|
|
|
|
// Asset
|
|
asset := &models.MediaAsset{
|
|
TenantID: 1,
|
|
UserID: author.ID,
|
|
ObjectKey: "test.mp4",
|
|
Type: consts.MediaAssetTypeVideo,
|
|
}
|
|
models.MediaAssetQuery.WithContext(ctx).Create(asset)
|
|
|
|
// Content
|
|
content := &models.Content{
|
|
TenantID: 1,
|
|
UserID: author.ID,
|
|
Title: "Detail Content",
|
|
Status: consts.ContentStatusPublished,
|
|
}
|
|
models.ContentQuery.WithContext(ctx).Create(content)
|
|
|
|
// Link Asset
|
|
ca := &models.ContentAsset{
|
|
TenantID: 1,
|
|
UserID: author.ID,
|
|
ContentID: content.ID,
|
|
AssetID: asset.ID,
|
|
Sort: 1,
|
|
}
|
|
models.ContentAssetQuery.WithContext(ctx).Create(ca)
|
|
|
|
Convey("should get detail with assets", func() {
|
|
detail, err := Content.Get(ctx, cast.ToString(content.ID))
|
|
So(err, ShouldBeNil)
|
|
So(detail.Title, ShouldEqual, "Detail Content")
|
|
So(detail.AuthorName, ShouldEqual, "Author1")
|
|
So(len(detail.MediaUrls), ShouldEqual, 1)
|
|
So(detail.MediaUrls[0].URL, ShouldEndWith, "test.mp4")
|
|
})
|
|
})
|
|
}
|
|
|
|
func (s *ContentTestSuite) Test_CreateComment() {
|
|
Convey("CreateComment", s.T(), func() {
|
|
ctx := s.T().Context()
|
|
database.Truncate(ctx, s.DB, models.TableNameContent, models.TableNameComment, models.TableNameUser)
|
|
|
|
// User & Content
|
|
u := &models.User{Username: "user1", Phone: "13900000001"}
|
|
models.UserQuery.WithContext(ctx).Create(u)
|
|
c := &models.Content{TenantID: 1, UserID: u.ID, Title: "C"}
|
|
models.ContentQuery.WithContext(ctx).Create(c)
|
|
|
|
// Auth context
|
|
ctx = context.WithValue(ctx, consts.CtxKeyUser, u.ID)
|
|
|
|
Convey("should create comment", func() {
|
|
form := &content_dto.CommentCreateForm{
|
|
Content: "Nice!",
|
|
}
|
|
err := Content.CreateComment(ctx, cast.ToString(c.ID), form)
|
|
So(err, ShouldBeNil)
|
|
|
|
count, _ := models.CommentQuery.WithContext(ctx).Where(models.CommentQuery.ContentID.Eq(c.ID)).Count()
|
|
So(count, ShouldEqual, 1)
|
|
})
|
|
})
|
|
}
|