107 lines
3.1 KiB
Go
107 lines
3.1 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
|
|
"quyun/v2/app/commands/testx"
|
|
creator_dto "quyun/v2/app/http/v1/dto"
|
|
"quyun/v2/database"
|
|
"quyun/v2/database/models"
|
|
"quyun/v2/pkg/consts"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
"github.com/stretchr/testify/suite"
|
|
"go.ipao.vip/atom/contracts"
|
|
"go.uber.org/dig"
|
|
)
|
|
|
|
type CreatorTestSuiteInjectParams struct {
|
|
dig.In
|
|
|
|
DB *sql.DB
|
|
Initials []contracts.Initial `group:"initials"`
|
|
}
|
|
|
|
type CreatorTestSuite struct {
|
|
suite.Suite
|
|
CreatorTestSuiteInjectParams
|
|
}
|
|
|
|
func Test_Creator(t *testing.T) {
|
|
providers := testx.Default().With(Provide)
|
|
|
|
testx.Serve(providers, t, func(p CreatorTestSuiteInjectParams) {
|
|
suite.Run(t, &CreatorTestSuite{CreatorTestSuiteInjectParams: p})
|
|
})
|
|
}
|
|
|
|
func (s *CreatorTestSuite) Test_Apply() {
|
|
Convey("Apply", s.T(), func() {
|
|
ctx := s.T().Context()
|
|
database.Truncate(ctx, s.DB, models.TableNameTenant, models.TableNameTenantUser, models.TableNameUser)
|
|
|
|
u := &models.User{Username: "creator1", Phone: "13700000001"}
|
|
models.UserQuery.WithContext(ctx).Create(u)
|
|
ctx = context.WithValue(ctx, consts.CtxKeyUser, u.ID)
|
|
|
|
Convey("should create tenant", func() {
|
|
form := &creator_dto.ApplyForm{
|
|
Name: "My Channel",
|
|
}
|
|
err := Creator.Apply(ctx, form)
|
|
So(err, ShouldBeNil)
|
|
|
|
t, _ := models.TenantQuery.WithContext(ctx).Where(models.TenantQuery.UserID.Eq(u.ID)).First()
|
|
So(t, ShouldNotBeNil)
|
|
So(t.Name, ShouldEqual, "My Channel")
|
|
So(t.Status, ShouldEqual, consts.TenantStatusPendingVerify)
|
|
|
|
// Check admin role
|
|
tu, _ := models.TenantUserQuery.WithContext(ctx).Where(models.TenantUserQuery.TenantID.Eq(t.ID)).First()
|
|
So(tu, ShouldNotBeNil)
|
|
// Role is array, check contains? Or first element?
|
|
// types.Array is likely []T.
|
|
So(len(tu.Role), ShouldEqual, 1)
|
|
So(tu.Role[0], ShouldEqual, consts.TenantUserRoleTenantAdmin)
|
|
})
|
|
})
|
|
}
|
|
|
|
func (s *CreatorTestSuite) Test_CreateContent() {
|
|
Convey("CreateContent", s.T(), func() {
|
|
ctx := s.T().Context()
|
|
database.Truncate(ctx, s.DB, models.TableNameTenant, models.TableNameContent, models.TableNameContentAsset, models.TableNameContentPrice, models.TableNameUser)
|
|
|
|
u := &models.User{Username: "creator2", Phone: "13700000002"}
|
|
models.UserQuery.WithContext(ctx).Create(u)
|
|
ctx = context.WithValue(ctx, consts.CtxKeyUser, u.ID)
|
|
|
|
// Create Tenant manually
|
|
t := &models.Tenant{UserID: u.ID, Name: "Channel 2", Code: "123", Status: consts.TenantStatusVerified}
|
|
models.TenantQuery.WithContext(ctx).Create(t)
|
|
|
|
Convey("should create content and assets", func() {
|
|
form := &creator_dto.ContentCreateForm{
|
|
Title: "New Song",
|
|
Genre: "audio",
|
|
Price: 9.99,
|
|
// MediaIDs: ... need media asset
|
|
}
|
|
err := Creator.CreateContent(ctx, form)
|
|
So(err, ShouldBeNil)
|
|
|
|
c, _ := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.Title.Eq("New Song")).First()
|
|
So(c, ShouldNotBeNil)
|
|
So(c.UserID, ShouldEqual, u.ID)
|
|
So(c.TenantID, ShouldEqual, t.ID)
|
|
|
|
// Check Price
|
|
p, _ := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(c.ID)).First()
|
|
So(p, ShouldNotBeNil)
|
|
So(p.PriceAmount, ShouldEqual, 999)
|
|
})
|
|
})
|
|
}
|