From 2fb6bba9034eb3accd92a880f5a749d9cfc80870 Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Sat, 4 Feb 2023 18:53:17 +0800 Subject: [PATCH] add dao testing cases --- .../20230131_165509_create_sys_role.go | 2 +- database/models/sys_roles.gen.go | 2 +- database/query/sys_roles.gen.go | 8 +- go.mod | 7 + go.sum | 12 ++ modules/auth/container/container.go | 34 ++++ modules/auth/controller/permission.go | 23 +++ modules/auth/controller/role.go | 28 ++++ modules/auth/dao/role.go | 57 +++++++ modules/auth/dao/role_test.go | 151 ++++++++++++++++++ modules/auth/dto/role.go | 8 + modules/auth/routes/routes.go | 43 +++++ modules/auth/service/role.go | 20 +++ modules/modules.go | 1 + modules/user/.keep | 0 providers/config/loader.go | 5 +- providers/provider.go | 1 + utils/db.go | 11 ++ utils/fs/dir.go | 5 - utils/fs/file.go | 9 ++ utils/fs/file_test.go | 49 ++++++ 21 files changed, 464 insertions(+), 12 deletions(-) create mode 100755 modules/auth/container/container.go create mode 100755 modules/auth/controller/permission.go create mode 100755 modules/auth/controller/role.go create mode 100755 modules/auth/dao/role.go create mode 100755 modules/auth/dao/role_test.go create mode 100644 modules/auth/dto/role.go create mode 100755 modules/auth/routes/routes.go create mode 100755 modules/auth/service/role.go delete mode 100644 modules/user/.keep create mode 100644 utils/db.go create mode 100644 utils/fs/file_test.go diff --git a/database/migrations/20230131_165509_create_sys_role.go b/database/migrations/20230131_165509_create_sys_role.go index db6dfc7..82ba148 100755 --- a/database/migrations/20230131_165509_create_sys_role.go +++ b/database/migrations/20230131_165509_create_sys_role.go @@ -40,7 +40,7 @@ func (m *Migration20230131_165509CreateSysRole) table() interface{} { type SysRole struct { gorm.Model - Alias string `gorm:"not null;unique;primary_key;comment:角色Alias;size:90"` // 角色ID + UUID string `gorm:"not null;unique;primary_key;comment:角色Alias;size:90"` // 角色UUID Name string `gorm:"comment:角色名"` // 角色名 ParentId *uint `gorm:"comment:父角色ID"` // 父角色ID DefaultRouter string `gorm:"comment:默认菜单;default:dashboard"` // 默认菜单(默认dashboard) diff --git a/database/models/sys_roles.gen.go b/database/models/sys_roles.gen.go index 702398a..b25393a 100644 --- a/database/models/sys_roles.gen.go +++ b/database/models/sys_roles.gen.go @@ -18,7 +18,7 @@ type SysRole struct { CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"` - Alias_ string `gorm:"column:alias;type:varchar(90);primaryKey" json:"alias"` // 角色Alias + UUID string `gorm:"column:uuid;type:varchar(90);primaryKey" json:"uuid"` // 角色Alias Name string `gorm:"column:name;type:varchar(191)" json:"name"` // 角色名 ParentID uint64 `gorm:"column:parent_id;type:bigint(20) unsigned" json:"parent_id"` // 父角色ID DefaultRouter string `gorm:"column:default_router;type:varchar(191);default:dashboard" json:"default_router"` // 默认菜单 diff --git a/database/query/sys_roles.gen.go b/database/query/sys_roles.gen.go index 6738e1c..525c92c 100644 --- a/database/query/sys_roles.gen.go +++ b/database/query/sys_roles.gen.go @@ -31,7 +31,7 @@ func newSysRole(db *gorm.DB, opts ...gen.DOOption) sysRole { _sysRole.CreatedAt = field.NewTime(tableName, "created_at") _sysRole.UpdatedAt = field.NewTime(tableName, "updated_at") _sysRole.DeletedAt = field.NewField(tableName, "deleted_at") - _sysRole.Alias_ = field.NewString(tableName, "alias") + _sysRole.UUID = field.NewString(tableName, "uuid") _sysRole.Name = field.NewString(tableName, "name") _sysRole.ParentID = field.NewUint64(tableName, "parent_id") _sysRole.DefaultRouter = field.NewString(tableName, "default_router") @@ -49,7 +49,7 @@ type sysRole struct { CreatedAt field.Time UpdatedAt field.Time DeletedAt field.Field - Alias_ field.String // 角色Alias + UUID field.String // 角色Alias Name field.String // 角色名 ParentID field.Uint64 // 父角色ID DefaultRouter field.String // 默认菜单 @@ -73,7 +73,7 @@ func (s *sysRole) updateTableName(table string) *sysRole { s.CreatedAt = field.NewTime(table, "created_at") s.UpdatedAt = field.NewTime(table, "updated_at") s.DeletedAt = field.NewField(table, "deleted_at") - s.Alias_ = field.NewString(table, "alias") + s.UUID = field.NewString(table, "uuid") s.Name = field.NewString(table, "name") s.ParentID = field.NewUint64(table, "parent_id") s.DefaultRouter = field.NewString(table, "default_router") @@ -104,7 +104,7 @@ func (s *sysRole) fillFieldMap() { s.fieldMap["created_at"] = s.CreatedAt s.fieldMap["updated_at"] = s.UpdatedAt s.fieldMap["deleted_at"] = s.DeletedAt - s.fieldMap["alias"] = s.Alias_ + s.fieldMap["uuid"] = s.UUID s.fieldMap["name"] = s.Name s.fieldMap["parent_id"] = s.ParentID s.fieldMap["default_router"] = s.DefaultRouter diff --git a/go.mod b/go.mod index 62c4750..0a398b3 100644 --- a/go.mod +++ b/go.mod @@ -14,8 +14,10 @@ require ( github.com/rogeecn/fabfile v1.3.0 github.com/rogeecn/gen v1.0.4 github.com/satori/go.uuid v1.2.0 + github.com/smartystreets/goconvey v1.7.2 github.com/spf13/cobra v1.5.0 github.com/spf13/viper v1.15.0 + github.com/stretchr/testify v1.8.1 go.uber.org/dig v1.15.0 go.uber.org/zap v1.21.0 golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b @@ -30,6 +32,7 @@ require ( require ( github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect github.com/casbin/casbin/v2 v2.55.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/glebarez/go-sqlite v1.19.1 // indirect @@ -43,6 +46,7 @@ require ( github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect @@ -56,6 +60,7 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/jtolds/gls v4.20.0+incompatible // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-isatty v0.0.16 // indirect @@ -64,8 +69,10 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect + github.com/smartystreets/assertions v1.2.0 // indirect github.com/spf13/afero v1.9.3 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index b70ed20..5333034 100644 --- a/go.sum +++ b/go.sum @@ -110,10 +110,12 @@ github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= +github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= +github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= @@ -192,6 +194,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -263,6 +267,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -348,6 +354,10 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= +github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= +github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= @@ -429,6 +439,7 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220511200225-c6db032c6c88/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= @@ -609,6 +620,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/modules/auth/container/container.go b/modules/auth/container/container.go new file mode 100755 index 0000000..456b021 --- /dev/null +++ b/modules/auth/container/container.go @@ -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) + } +} diff --git a/modules/auth/controller/permission.go b/modules/auth/controller/permission.go new file mode 100755 index 0000000..558e645 --- /dev/null +++ b/modules/auth/controller/permission.go @@ -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 +} diff --git a/modules/auth/controller/role.go b/modules/auth/controller/role.go new file mode 100755 index 0000000..341e579 --- /dev/null +++ b/modules/auth/controller/role.go @@ -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 +} diff --git a/modules/auth/dao/role.go b/modules/auth/dao/role.go new file mode 100755 index 0000000..0e2b595 --- /dev/null +++ b/modules/auth/dao/role.go @@ -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 +} diff --git a/modules/auth/dao/role_test.go b/modules/auth/dao/role_test.go new file mode 100755 index 0000000..1ec536f --- /dev/null +++ b/modules/auth/dao/role_test.go @@ -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) + }) +} diff --git a/modules/auth/dto/role.go b/modules/auth/dto/role.go new file mode 100644 index 0000000..3bff91a --- /dev/null +++ b/modules/auth/dto/role.go @@ -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"` +} diff --git a/modules/auth/routes/routes.go b/modules/auth/routes/routes.go new file mode 100755 index 0000000..4f98e74 --- /dev/null +++ b/modules/auth/routes/routes.go @@ -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)) + } + + } +} diff --git a/modules/auth/service/role.go b/modules/auth/service/role.go new file mode 100755 index 0000000..0eed7c3 --- /dev/null +++ b/modules/auth/service/role.go @@ -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 +} diff --git a/modules/modules.go b/modules/modules.go index aba8e88..5ac7bac 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -1,6 +1,7 @@ package modules import ( + _ "atom/modules/auth/container" _ "atom/modules/resources/container" _ "atom/modules/system/container" ) diff --git a/modules/user/.keep b/modules/user/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/providers/config/loader.go b/providers/config/loader.go index cb6d194..5542911 100644 --- a/providers/config/loader.go +++ b/providers/config/loader.go @@ -3,6 +3,7 @@ package config import ( "atom/container" "atom/utils" + "atom/utils/fs" "log" "github.com/pkg/errors" @@ -34,10 +35,12 @@ func Load() (*Config, error) { return nil, err } } + path, name, _ := fs.FilePathInfo(confFile) - viper.SetConfigName(confFile) // name of config file (without extension) + viper.SetConfigName(name) // name of config file (without extension) viper.SetConfigType("toml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath("$HOME/") // call multiple times to add many search paths + viper.AddConfigPath(path) // optionally look for config in the working directory viper.AddConfigPath(".") // optionally look for config in the working directory // Find and read the config file if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file diff --git a/providers/provider.go b/providers/provider.go index 3db26d7..ad63e8c 100644 --- a/providers/provider.go +++ b/providers/provider.go @@ -9,5 +9,6 @@ import ( _ "atom/providers/http" _ "atom/providers/jwt" _ "atom/providers/log" + _ "atom/providers/query" _ "atom/providers/single_flight" ) diff --git a/utils/db.go b/utils/db.go new file mode 100644 index 0000000..5a95f45 --- /dev/null +++ b/utils/db.go @@ -0,0 +1,11 @@ +package utils + +import ( + "fmt" + + "gorm.io/gorm" +) + +func TruncateTable(db *gorm.DB, table string) { + db.Exec(fmt.Sprintf("TRUNCATE TABLE %s", table)) +} diff --git a/utils/fs/dir.go b/utils/fs/dir.go index 6099aff..abd8385 100644 --- a/utils/fs/dir.go +++ b/utils/fs/dir.go @@ -1,11 +1,8 @@ package fs import ( - "atom/providers/log" "errors" "os" - - "go.uber.org/zap" ) func PathExists(path string) (bool, error) { @@ -29,9 +26,7 @@ func CreateDir(dirs ...string) (err error) { return err } if !exist { - log.Debug("create directory" + v) if err := os.MkdirAll(v, os.ModePerm); err != nil { - log.Error("create directory"+v, zap.Any(" error:", err)) return err } } diff --git a/utils/fs/file.go b/utils/fs/file.go index 11aa76d..4d44612 100644 --- a/utils/fs/file.go +++ b/utils/fs/file.go @@ -63,3 +63,12 @@ func FileExist(path string) bool { } return !os.IsNotExist(err) } + +func FilePathInfo(file string) (path, name, ext string) { + filename := filepath.Base(file) + + path = filepath.Dir(file) + ext = filepath.Ext(filename) + name = strings.TrimSuffix(filename, ext) + return +} diff --git a/utils/fs/file_test.go b/utils/fs/file_test.go new file mode 100644 index 0000000..356abfe --- /dev/null +++ b/utils/fs/file_test.go @@ -0,0 +1,49 @@ +package fs + +import "testing" + +func TestFilePathInfo(t *testing.T) { + type args struct { + file string + } + tests := []struct { + name string + args args + wantPath string + wantName string + wantExt string + }{ + { + "1.", + args{ + file: "/a/bc.ext", + }, + "/a", + "bc", + ".ext", + }, + { + "1.", + args{ + file: "/a/c/c/c/bc.ext", + }, + "/a/c/c/c", + "bc", + ".ext", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotPath, gotName, gotExt := FilePathInfo(tt.args.file) + if gotPath != tt.wantPath { + t.Errorf("FilePathInfo() gotPath = %v, want %v", gotPath, tt.wantPath) + } + if gotName != tt.wantName { + t.Errorf("FilePathInfo() gotName = %v, want %v", gotName, tt.wantName) + } + if gotExt != tt.wantExt { + t.Errorf("FilePathInfo() gotExt = %v, want %v", gotExt, tt.wantExt) + } + }) + } +}