add dao testing cases
This commit is contained in:
34
modules/auth/container/container.go
Executable file
34
modules/auth/container/container.go
Executable file
@@ -0,0 +1,34 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"atom/container"
|
||||
"atom/modules/auth/controller"
|
||||
"atom/modules/auth/dao"
|
||||
"atom/modules/auth/routes"
|
||||
"log"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
// controller
|
||||
if err := container.Container.Provide(controller.NewRoleController); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(controller.NewPermissionController); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
//service
|
||||
|
||||
// dao
|
||||
if err := container.Container.Provide(dao.NewRoleDao); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := container.Container.Provide(routes.NewRoute, dig.Group("route")); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
23
modules/auth/controller/permission.go
Executable file
23
modules/auth/controller/permission.go
Executable file
@@ -0,0 +1,23 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"atom/providers/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PermissionController interface {
|
||||
GetName(*gin.Context) (string, error)
|
||||
}
|
||||
|
||||
type permissionControllerImpl struct {
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func NewPermissionController(conf *config.Config) PermissionController {
|
||||
return &permissionControllerImpl{conf: conf}
|
||||
}
|
||||
|
||||
func (c *permissionControllerImpl) GetName(ctx *gin.Context) (string, error) {
|
||||
return "Permission",nil
|
||||
}
|
||||
28
modules/auth/controller/role.go
Executable file
28
modules/auth/controller/role.go
Executable file
@@ -0,0 +1,28 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"atom/modules/auth/dto"
|
||||
"atom/providers/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type RoleController interface {
|
||||
GetName(*gin.Context) (string, error)
|
||||
}
|
||||
|
||||
type roleControllerImpl struct {
|
||||
conf *config.Config
|
||||
}
|
||||
|
||||
func NewRoleController(conf *config.Config) RoleController {
|
||||
return &roleControllerImpl{conf: conf}
|
||||
}
|
||||
|
||||
func (c *roleControllerImpl) GetName(ctx *gin.Context) (string, error) {
|
||||
return "Role", nil
|
||||
}
|
||||
|
||||
func (c *roleControllerImpl) Create(ctx *gin.Context, req *dto.RoleCreateRequest) error {
|
||||
return nil
|
||||
}
|
||||
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)
|
||||
})
|
||||
}
|
||||
8
modules/auth/dto/role.go
Normal file
8
modules/auth/dto/role.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package dto
|
||||
|
||||
type RoleCreateRequest struct {
|
||||
UUID string `json:"alias,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ParentID uint `json:"parent_id,omitempty"`
|
||||
DefaultRouter string `json:"default_router,omitempty"`
|
||||
}
|
||||
43
modules/auth/routes/routes.go
Executable file
43
modules/auth/routes/routes.go
Executable file
@@ -0,0 +1,43 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"atom/contracts"
|
||||
"atom/modules/auth/controller"
|
||||
"atom/providers/http"
|
||||
|
||||
"github.com/rogeecn/gen"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
svc *http.Service
|
||||
role controller.RoleController
|
||||
permission controller.PermissionController
|
||||
}
|
||||
|
||||
func NewRoute(
|
||||
svc *http.Service,
|
||||
role controller.RoleController,
|
||||
permission controller.PermissionController,
|
||||
) contracts.Route {
|
||||
return &Route{
|
||||
svc: svc,
|
||||
role: role,
|
||||
permission: permission,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Route) Register() {
|
||||
group := r.svc.Engine.Group("auth")
|
||||
{
|
||||
roleGroup := group.Group("role")
|
||||
{
|
||||
roleGroup.GET("/roles", gen.DataFunc(r.role.GetName))
|
||||
}
|
||||
|
||||
permissionGroup := group.Group("permission")
|
||||
{
|
||||
permissionGroup.GET("/permissions", gen.DataFunc(r.permission.GetName))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
20
modules/auth/service/role.go
Executable file
20
modules/auth/service/role.go
Executable file
@@ -0,0 +1,20 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type RoleService interface {
|
||||
Create(ctx context.Context) error
|
||||
}
|
||||
|
||||
type roleService struct {
|
||||
}
|
||||
|
||||
func NewRoleService() RoleService {
|
||||
return &roleService{}
|
||||
}
|
||||
|
||||
func (svc *roleService) Create(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
_ "atom/modules/auth/container"
|
||||
_ "atom/modules/resources/container"
|
||||
_ "atom/modules/system/container"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user