feat: 重构内容服务,优化数据预加载和用户互动功能;增加租户关注功能及相关测试用例
This commit is contained in:
@@ -158,3 +158,104 @@ func (s *ContentTestSuite) Test_CreateComment() {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_Library() {
|
||||
Convey("Library", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
database.Truncate(ctx, s.DB, models.TableNameContent, models.TableNameContentAccess, models.TableNameUser, models.TableNameContentAsset, models.TableNameMediaAsset)
|
||||
|
||||
// User
|
||||
u := &models.User{Username: "user_lib", Phone: "13900000002"}
|
||||
models.UserQuery.WithContext(ctx).Create(u)
|
||||
ctx = context.WithValue(ctx, consts.CtxKeyUser, u.ID)
|
||||
|
||||
// Content
|
||||
c := &models.Content{TenantID: 1, UserID: u.ID, Title: "Paid Content", Genre: "video"}
|
||||
models.ContentQuery.WithContext(ctx).Create(c)
|
||||
|
||||
// Asset (Video & Cover)
|
||||
assetVid := &models.MediaAsset{TenantID: 1, UserID: u.ID, Type: consts.MediaAssetTypeVideo, ObjectKey: "video.mp4"}
|
||||
assetImg := &models.MediaAsset{TenantID: 1, UserID: u.ID, Type: consts.MediaAssetTypeImage, ObjectKey: "cover.jpg"}
|
||||
models.MediaAssetQuery.WithContext(ctx).Create(assetVid, assetImg)
|
||||
|
||||
models.ContentAssetQuery.WithContext(ctx).Create(
|
||||
&models.ContentAsset{ContentID: c.ID, AssetID: assetVid.ID, Role: consts.ContentAssetRoleMain},
|
||||
&models.ContentAsset{ContentID: c.ID, AssetID: assetImg.ID, Role: consts.ContentAssetRoleCover},
|
||||
)
|
||||
|
||||
// Access
|
||||
models.ContentAccessQuery.WithContext(ctx).Create(&models.ContentAccess{
|
||||
TenantID: 1, UserID: u.ID, ContentID: c.ID, Status: "active",
|
||||
})
|
||||
|
||||
Convey("should get library content with details", func() {
|
||||
list, err := Content.GetLibrary(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(list), ShouldEqual, 1)
|
||||
So(list[0].Title, ShouldEqual, "Paid Content")
|
||||
So(list[0].Type, ShouldEqual, "video")
|
||||
So(list[0].Cover, ShouldEndWith, "cover.jpg")
|
||||
So(list[0].IsPurchased, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_Interact() {
|
||||
Convey("Interact", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
database.Truncate(ctx, s.DB, models.TableNameContent, models.TableNameUserContentAction, models.TableNameUser)
|
||||
|
||||
// User & Content
|
||||
u := &models.User{Username: "user_act", Phone: "13900000003"}
|
||||
models.UserQuery.WithContext(ctx).Create(u)
|
||||
c := &models.Content{TenantID: 1, UserID: u.ID, Title: "Liked Content", Likes: 0}
|
||||
models.ContentQuery.WithContext(ctx).Create(c)
|
||||
|
||||
ctx = context.WithValue(ctx, consts.CtxKeyUser, u.ID)
|
||||
|
||||
Convey("Like flow", func() {
|
||||
// Add Like
|
||||
err := Content.AddLike(ctx, cast.ToString(c.ID))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Verify count
|
||||
cReload, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(c.ID)).First()
|
||||
So(cReload.Likes, ShouldEqual, 1)
|
||||
|
||||
// Get Likes
|
||||
likes, err := Content.GetLikes(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(likes), ShouldEqual, 1)
|
||||
So(likes[0].ID, ShouldEqual, cast.ToString(c.ID))
|
||||
|
||||
// Remove Like
|
||||
err = Content.RemoveLike(ctx, cast.ToString(c.ID))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Verify count
|
||||
cReload, _ = models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(c.ID)).First()
|
||||
So(cReload.Likes, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("Favorite flow", func() {
|
||||
// Add Favorite
|
||||
err := Content.AddFavorite(ctx, cast.ToString(c.ID))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Get Favorites
|
||||
favs, err := Content.GetFavorites(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(favs), ShouldEqual, 1)
|
||||
So(favs[0].ID, ShouldEqual, cast.ToString(c.ID))
|
||||
|
||||
// Remove Favorite
|
||||
err = Content.RemoveFavorite(ctx, cast.ToString(c.ID))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// Get Favorites
|
||||
favs, err = Content.GetFavorites(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(favs), ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user