add dao testing cases
This commit is contained in:
57
modules/auth/dao/role.go
Executable file
57
modules/auth/dao/role.go
Executable file
@@ -0,0 +1,57 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
"context"
|
||||
)
|
||||
|
||||
type RoleDao interface {
|
||||
FindByID(context.Context, uint64) (*models.SysRole, error)
|
||||
Create(context.Context, *models.SysRole) (*models.SysRole, error)
|
||||
UpdateByID(context.Context, *models.SysRole) (*models.SysRole, error)
|
||||
DeleteByID(context.Context, uint64) error
|
||||
DeletePermanentlyByID(context.Context, uint64) error
|
||||
}
|
||||
|
||||
type roleDaoImpl struct {
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
func NewRoleDao(query *query.Query) RoleDao {
|
||||
return &roleDaoImpl{query: query}
|
||||
}
|
||||
|
||||
func (dao *roleDaoImpl) FindByID(ctx context.Context, id uint64) (*models.SysRole, error) {
|
||||
role := dao.query.SysRole
|
||||
return role.WithContext(ctx).Where(role.ID.Eq(id)).First()
|
||||
}
|
||||
|
||||
func (dao *roleDaoImpl) Create(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
||||
role := dao.query.SysRole
|
||||
if err := role.WithContext(ctx).Create(model); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
func (dao *roleDaoImpl) UpdateByID(ctx context.Context, model *models.SysRole) (*models.SysRole, error) {
|
||||
role := dao.query.SysRole
|
||||
_, err := role.WithContext(ctx).Where(role.ID.Eq(model.ID)).Updates(model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
func (dao *roleDaoImpl) DeleteByID(ctx context.Context, id uint64) error {
|
||||
role := dao.query.SysRole
|
||||
_, err := role.WithContext(ctx).Where(role.ID.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *roleDaoImpl) DeletePermanentlyByID(ctx context.Context, id uint64) error {
|
||||
role := dao.query.SysRole
|
||||
_, err := role.WithContext(ctx).Unscoped().Where(role.ID.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
151
modules/auth/dao/role_test.go
Executable file
151
modules/auth/dao/role_test.go
Executable file
@@ -0,0 +1,151 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
// 这里的依赖需要被导入,否则会报错
|
||||
"atom/container"
|
||||
"atom/database/models"
|
||||
"atom/database/query"
|
||||
_ "atom/providers"
|
||||
"atom/utils"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type RoleInjectParams struct {
|
||||
dig.In
|
||||
DB *gorm.DB
|
||||
Dao RoleDao
|
||||
Query *query.Query
|
||||
Faker *gofakeit.Faker
|
||||
}
|
||||
|
||||
type RoleSuite struct {
|
||||
suite.Suite
|
||||
RoleInjectParams
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewRoleDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_RoleSuite(t *testing.T) {
|
||||
err := container.Container.Invoke(func(p RoleInjectParams) {
|
||||
s := &RoleSuite{}
|
||||
s.RoleInjectParams = p
|
||||
|
||||
suite.Run(t, s)
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func (s *RoleSuite) BeforeTest(suiteName, testName string) {
|
||||
log.Println("BeforeTest: ", testName)
|
||||
utils.TruncateTable(s.DB, s.Query.SysRole.TableName())
|
||||
switch testName {
|
||||
case "Test_FindByID", "Test_UpdateByID", "Test_DeleteByID", "Test_DeletePermanentlyByID":
|
||||
log.Println("BeforeTest: insert test data")
|
||||
_, _ = s.Dao.Create(context.Background(), &models.SysRole{
|
||||
UUID: s.Faker.UUID(),
|
||||
Name: s.Faker.Name(),
|
||||
ParentID: s.Faker.Uint64(),
|
||||
DefaultRouter: s.Faker.Animal(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RoleSuite) AfterTest(suiteName, testName string) {
|
||||
|
||||
}
|
||||
|
||||
///////////////////
|
||||
// start testing cases
|
||||
//////////////////
|
||||
|
||||
func (s *RoleSuite) Test_Create() {
|
||||
Convey("Test_Create", s.T(), func() {
|
||||
Reset(func() {
|
||||
s.BeforeTest("_", "Test_Create")
|
||||
})
|
||||
|
||||
Convey("create", func() {
|
||||
model, err := s.Dao.Create(context.Background(), &models.SysRole{
|
||||
UUID: s.Faker.UUID(),
|
||||
Name: s.Faker.Name(),
|
||||
ParentID: s.Faker.Uint64(),
|
||||
DefaultRouter: s.Faker.Animal(),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *RoleSuite) Test_FindByID() {
|
||||
Convey("Test_FindByID", s.T(), func() {
|
||||
model, err := s.Dao.FindByID(context.Background(), 1)
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *RoleSuite) Test_UpdateByID() {
|
||||
Convey("Test_UpdateByID", s.T(), func() {
|
||||
model, err := s.Dao.FindByID(context.Background(), 1)
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
|
||||
name := "TEST_UpdateByID"
|
||||
model.Name = name
|
||||
newModel, err := s.Dao.UpdateByID(context.Background(), model)
|
||||
So(err, ShouldBeNil)
|
||||
So(newModel.Name, ShouldEqual, name)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *RoleSuite) Test_DeleteByID() {
|
||||
Convey("Test_DeleteByID", s.T(), func() {
|
||||
model, err := s.Dao.FindByID(context.Background(), 1)
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
|
||||
err = s.Dao.DeleteByID(context.Background(), model.ID)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
model, err = s.Query.SysRole.
|
||||
WithContext(context.TODO()).
|
||||
Unscoped().
|
||||
Where(s.Query.SysRole.ID.Eq(1)).
|
||||
First()
|
||||
So(err, ShouldBeNil)
|
||||
So(model.DeletedAt, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *RoleSuite) Test_DeletePermanentlyByID() {
|
||||
Convey("Test_DeletePermanentlyByID", s.T(), func() {
|
||||
model, err := s.Dao.FindByID(context.Background(), 1)
|
||||
So(err, ShouldBeNil)
|
||||
So(model.ID, ShouldEqual, 1)
|
||||
|
||||
err = s.Dao.DeletePermanentlyByID(context.Background(), model.ID)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, err = s.Query.SysRole.
|
||||
WithContext(context.TODO()).
|
||||
Unscoped().
|
||||
Where(s.Query.SysRole.ID.Eq(1)).
|
||||
First()
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user