add dao testing cases

This commit is contained in:
yanghao05
2023-02-04 18:53:17 +08:00
parent 1c2b861ac7
commit 2fb6bba903
21 changed files with 464 additions and 12 deletions

View File

@@ -40,7 +40,7 @@ func (m *Migration20230131_165509CreateSysRole) table() interface{} {
type SysRole struct { type SysRole struct {
gorm.Model 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:角色名"` // 角色名 Name string `gorm:"comment:角色名"` // 角色名
ParentId *uint `gorm:"comment:父角色ID"` // 父角色ID ParentId *uint `gorm:"comment:父角色ID"` // 父角色ID
DefaultRouter string `gorm:"comment:默认菜单;default:dashboard"` // 默认菜单(默认dashboard) DefaultRouter string `gorm:"comment:默认菜单;default:dashboard"` // 默认菜单(默认dashboard)

View File

@@ -18,7 +18,7 @@ type SysRole struct {
CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` 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"` 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"` 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"` // 角色名 Name string `gorm:"column:name;type:varchar(191)" json:"name"` // 角色名
ParentID uint64 `gorm:"column:parent_id;type:bigint(20) unsigned" json:"parent_id"` // 父角色ID 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"` // 默认菜单 DefaultRouter string `gorm:"column:default_router;type:varchar(191);default:dashboard" json:"default_router"` // 默认菜单

View File

@@ -31,7 +31,7 @@ func newSysRole(db *gorm.DB, opts ...gen.DOOption) sysRole {
_sysRole.CreatedAt = field.NewTime(tableName, "created_at") _sysRole.CreatedAt = field.NewTime(tableName, "created_at")
_sysRole.UpdatedAt = field.NewTime(tableName, "updated_at") _sysRole.UpdatedAt = field.NewTime(tableName, "updated_at")
_sysRole.DeletedAt = field.NewField(tableName, "deleted_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.Name = field.NewString(tableName, "name")
_sysRole.ParentID = field.NewUint64(tableName, "parent_id") _sysRole.ParentID = field.NewUint64(tableName, "parent_id")
_sysRole.DefaultRouter = field.NewString(tableName, "default_router") _sysRole.DefaultRouter = field.NewString(tableName, "default_router")
@@ -49,7 +49,7 @@ type sysRole struct {
CreatedAt field.Time CreatedAt field.Time
UpdatedAt field.Time UpdatedAt field.Time
DeletedAt field.Field DeletedAt field.Field
Alias_ field.String // 角色Alias UUID field.String // 角色Alias
Name field.String // 角色名 Name field.String // 角色名
ParentID field.Uint64 // 父角色ID ParentID field.Uint64 // 父角色ID
DefaultRouter field.String // 默认菜单 DefaultRouter field.String // 默认菜单
@@ -73,7 +73,7 @@ func (s *sysRole) updateTableName(table string) *sysRole {
s.CreatedAt = field.NewTime(table, "created_at") s.CreatedAt = field.NewTime(table, "created_at")
s.UpdatedAt = field.NewTime(table, "updated_at") s.UpdatedAt = field.NewTime(table, "updated_at")
s.DeletedAt = field.NewField(table, "deleted_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.Name = field.NewString(table, "name")
s.ParentID = field.NewUint64(table, "parent_id") s.ParentID = field.NewUint64(table, "parent_id")
s.DefaultRouter = field.NewString(table, "default_router") s.DefaultRouter = field.NewString(table, "default_router")
@@ -104,7 +104,7 @@ func (s *sysRole) fillFieldMap() {
s.fieldMap["created_at"] = s.CreatedAt s.fieldMap["created_at"] = s.CreatedAt
s.fieldMap["updated_at"] = s.UpdatedAt s.fieldMap["updated_at"] = s.UpdatedAt
s.fieldMap["deleted_at"] = s.DeletedAt s.fieldMap["deleted_at"] = s.DeletedAt
s.fieldMap["alias"] = s.Alias_ s.fieldMap["uuid"] = s.UUID
s.fieldMap["name"] = s.Name s.fieldMap["name"] = s.Name
s.fieldMap["parent_id"] = s.ParentID s.fieldMap["parent_id"] = s.ParentID
s.fieldMap["default_router"] = s.DefaultRouter s.fieldMap["default_router"] = s.DefaultRouter

7
go.mod
View File

@@ -14,8 +14,10 @@ require (
github.com/rogeecn/fabfile v1.3.0 github.com/rogeecn/fabfile v1.3.0
github.com/rogeecn/gen v1.0.4 github.com/rogeecn/gen v1.0.4
github.com/satori/go.uuid v1.2.0 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/cobra v1.5.0
github.com/spf13/viper v1.15.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/dig v1.15.0
go.uber.org/zap v1.21.0 go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
@@ -30,6 +32,7 @@ require (
require ( require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/casbin/casbin/v2 v2.55.1 // 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/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/glebarez/go-sqlite v1.19.1 // 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-sql/sqlexp v0.1.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/google/uuid v1.3.0 // 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/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // 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/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // 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/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.16 // 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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // 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/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/sirupsen/logrus v1.8.1 // 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/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect

12
go.sum
View File

@@ -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 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/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 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 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 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/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 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 h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= 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= 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.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/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/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.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/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= 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/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.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/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/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/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= 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.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 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 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= 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-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-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-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-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-20220511200225-c6db032c6c88/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/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-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-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-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-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-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=

View 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)
}
}

View 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
View 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
View 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
View 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
View 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
View 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
View 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
}

View File

@@ -1,6 +1,7 @@
package modules package modules
import ( import (
_ "atom/modules/auth/container"
_ "atom/modules/resources/container" _ "atom/modules/resources/container"
_ "atom/modules/system/container" _ "atom/modules/system/container"
) )

View File

View File

@@ -3,6 +3,7 @@ package config
import ( import (
"atom/container" "atom/container"
"atom/utils" "atom/utils"
"atom/utils/fs"
"log" "log"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -34,10 +35,12 @@ func Load() (*Config, error) {
return nil, err 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.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("$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 viper.AddConfigPath(".") // optionally look for config in the working directory
// Find and read the config file // Find and read the config file
if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file

View File

@@ -9,5 +9,6 @@ import (
_ "atom/providers/http" _ "atom/providers/http"
_ "atom/providers/jwt" _ "atom/providers/jwt"
_ "atom/providers/log" _ "atom/providers/log"
_ "atom/providers/query"
_ "atom/providers/single_flight" _ "atom/providers/single_flight"
) )

11
utils/db.go Normal file
View File

@@ -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))
}

View File

@@ -1,11 +1,8 @@
package fs package fs
import ( import (
"atom/providers/log"
"errors" "errors"
"os" "os"
"go.uber.org/zap"
) )
func PathExists(path string) (bool, error) { func PathExists(path string) (bool, error) {
@@ -29,9 +26,7 @@ func CreateDir(dirs ...string) (err error) {
return err return err
} }
if !exist { if !exist {
log.Debug("create directory" + v)
if err := os.MkdirAll(v, os.ModePerm); err != nil { if err := os.MkdirAll(v, os.ModePerm); err != nil {
log.Error("create directory"+v, zap.Any(" error:", err))
return err return err
} }
} }

View File

@@ -63,3 +63,12 @@ func FileExist(path string) bool {
} }
return !os.IsNotExist(err) 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
}

49
utils/fs/file_test.go Normal file
View File

@@ -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)
}
})
}
}