feat: add medias
This commit is contained in:
280
backend/modules/medias/service_test.go
Normal file
280
backend/modules/medias/service_test.go
Normal file
@@ -0,0 +1,280 @@
|
||||
package medias
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"backend/database/models/qvyun/public/model"
|
||||
"backend/database/models/qvyun/public/table"
|
||||
"backend/fixtures"
|
||||
dbUtil "backend/pkg/db"
|
||||
"backend/pkg/pg"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestService_GetUserBoughtMedias(t *testing.T) {
|
||||
Convey("TestService_GetUserBoughtMedias", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "user_medias"), ShouldBeNil)
|
||||
|
||||
Convey("insert some data", func() {
|
||||
items := []model.UserMedias{
|
||||
{UserID: 1, TenantID: 1, MediaID: 1, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 2, Price: 10},
|
||||
{UserID: 1, TenantID: 1, MediaID: 3, Price: 10},
|
||||
}
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.INSERT(tbl.UserID, tbl.TenantID, tbl.MediaID, tbl.Price).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("get user bought medias", func() {
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
ids, err := svc.GetUserBoughtMedias(context.TODO(), 1, 1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
for _, id := range ids {
|
||||
So(lo.Contains([]int64{1, 2, 3}, id), ShouldBeTrue)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_List(t *testing.T) {
|
||||
Convey("TestService_list", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
|
||||
Convey("truncate all tables", func() {
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "medias", "media_resources", "user_medias"), ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("insert user_medias data", func() {
|
||||
items := []model.UserMedias{
|
||||
{UserID: 1, TenantID: 1, MediaID: 1, Price: 10},
|
||||
}
|
||||
|
||||
tbl := table.UserMedias
|
||||
stmt := tbl.INSERT(tbl.UserID, tbl.TenantID, tbl.MediaID, tbl.Price).MODELS(items)
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("insert medias data", func() {
|
||||
items := []model.Medias{
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title1",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title2",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
TenantID: 1,
|
||||
Title: "title3",
|
||||
Description: "hello",
|
||||
Price: 100,
|
||||
Publish: false,
|
||||
},
|
||||
}
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.INSERT(
|
||||
tbl.TenantID,
|
||||
tbl.Title,
|
||||
tbl.Description,
|
||||
tbl.Price,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 3,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.INSERT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("get list", func() {
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
items, err := svc.List(context.TODO(), 1, 1, &ListFilter{
|
||||
Bought: lo.ToPtr(true),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.Logf("items: %+v", items)
|
||||
So(items, ShouldHaveLength, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_List1(t *testing.T) {
|
||||
Convey("TestService_list", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
defer db.Close()
|
||||
|
||||
tbl := table.Medias
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.Publish.EQ(Bool(true)).AND(
|
||||
tbl.TenantID.EQ(Int(1)),
|
||||
),
|
||||
).
|
||||
WHERE(tbl.ID.EQ(Int(1))).
|
||||
ORDER_BY(tbl.ID.DESC())
|
||||
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
type list struct {
|
||||
Media model.Medias `alias:"ListItem.*"`
|
||||
}
|
||||
|
||||
var dest []ListItem
|
||||
err = stmt.QueryContext(context.TODO(), db, &dest)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
t.Logf("dest: %+v", dest)
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_DecorateListResources(t *testing.T) {
|
||||
Convey("TestService_DecorateListResources", t, func() {
|
||||
db, err := fixtures.GetDB()
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
defer db.Close()
|
||||
|
||||
Convey("truncate all tables", func() {
|
||||
So(dbUtil.TruncateAllTables(context.TODO(), db, "medias", "media_resources"), ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("create media's resources", func() {
|
||||
items := []model.MediaResources{
|
||||
{
|
||||
MediaID: 1,
|
||||
Type: pg.MediaTypeVideo,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
{
|
||||
MediaID: 2,
|
||||
Type: pg.MediaTypeAudio,
|
||||
Source: lo.ToPtr("http://www.baidu.com"),
|
||||
Size: 100,
|
||||
Publish: true,
|
||||
},
|
||||
}
|
||||
|
||||
tbl := table.MediaResources
|
||||
stmt := tbl.INSERT(
|
||||
tbl.MediaID,
|
||||
tbl.Type,
|
||||
tbl.Size,
|
||||
tbl.Publish,
|
||||
).MODELS(items)
|
||||
t.Log(stmt.DebugSql())
|
||||
|
||||
_, err := stmt.Exec(db)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("decorate list resources", func() {
|
||||
items := []ListItem{
|
||||
{Medias: model.Medias{ID: 1}},
|
||||
{Medias: model.Medias{ID: 2}},
|
||||
}
|
||||
|
||||
svc := &Service{db: db}
|
||||
So(svc.Prepare(), ShouldBeNil)
|
||||
|
||||
items, err := svc.DecorateListResources(context.TODO(), 1, 1, items)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
for _, item := range items {
|
||||
So(item.MediaResources, ShouldHaveLength, 1)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user