feature 1.1.5 (#411)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -10,62 +10,156 @@ import (
|
||||
)
|
||||
|
||||
func TestGenCacheKeys(t *testing.T) {
|
||||
m, err := genCacheKeys(parser.Table{
|
||||
primaryField := &parser.Field{
|
||||
Name: stringx.From("id"),
|
||||
DataBaseType: "bigint",
|
||||
DataType: "int64",
|
||||
Comment: "自增id",
|
||||
SeqInIndex: 1,
|
||||
}
|
||||
mobileField := &parser.Field{
|
||||
Name: stringx.From("mobile"),
|
||||
DataBaseType: "varchar",
|
||||
DataType: "string",
|
||||
Comment: "手机号",
|
||||
SeqInIndex: 1,
|
||||
}
|
||||
classField := &parser.Field{
|
||||
Name: stringx.From("class"),
|
||||
DataBaseType: "varchar",
|
||||
DataType: "string",
|
||||
Comment: "班级",
|
||||
SeqInIndex: 1,
|
||||
}
|
||||
nameField := &parser.Field{
|
||||
Name: stringx.From("name"),
|
||||
DataBaseType: "varchar",
|
||||
DataType: "string",
|
||||
Comment: "姓名",
|
||||
SeqInIndex: 2,
|
||||
}
|
||||
primariCacheKey, uniqueCacheKey := genCacheKeys(parser.Table{
|
||||
Name: stringx.From("user"),
|
||||
PrimaryKey: parser.Primary{
|
||||
Field: parser.Field{
|
||||
Name: stringx.From("id"),
|
||||
DataBaseType: "bigint",
|
||||
DataType: "int64",
|
||||
IsPrimaryKey: true,
|
||||
IsUniqueKey: false,
|
||||
Comment: "自增id",
|
||||
},
|
||||
Field: *primaryField,
|
||||
AutoIncrement: true,
|
||||
},
|
||||
Fields: []parser.Field{
|
||||
{
|
||||
Name: stringx.From("mobile"),
|
||||
DataBaseType: "varchar",
|
||||
DataType: "string",
|
||||
IsPrimaryKey: false,
|
||||
IsUniqueKey: true,
|
||||
Comment: "手机号",
|
||||
UniqueIndex: map[string][]*parser.Field{
|
||||
"mobile_unique": []*parser.Field{
|
||||
mobileField,
|
||||
},
|
||||
{
|
||||
Name: stringx.From("name"),
|
||||
DataBaseType: "varchar",
|
||||
DataType: "string",
|
||||
IsPrimaryKey: false,
|
||||
IsUniqueKey: true,
|
||||
Comment: "姓名",
|
||||
"class_name_unique": []*parser.Field{
|
||||
classField,
|
||||
nameField,
|
||||
},
|
||||
},
|
||||
NormalIndex: nil,
|
||||
Fields: []*parser.Field{
|
||||
primaryField,
|
||||
mobileField,
|
||||
classField,
|
||||
nameField,
|
||||
{
|
||||
Name: stringx.From("createTime"),
|
||||
DataBaseType: "timestamp",
|
||||
DataType: "time.Time",
|
||||
IsPrimaryKey: false,
|
||||
IsUniqueKey: false,
|
||||
Comment: "创建时间",
|
||||
},
|
||||
{
|
||||
Name: stringx.From("updateTime"),
|
||||
DataBaseType: "timestamp",
|
||||
DataType: "time.Time",
|
||||
IsPrimaryKey: false,
|
||||
IsUniqueKey: false,
|
||||
Comment: "更新时间",
|
||||
},
|
||||
},
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
for fieldName, key := range m {
|
||||
name := stringx.From(fieldName)
|
||||
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix = "cache#User#%s#"`, name.ToCamel(), name.Untitle()), key.VarExpression)
|
||||
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix`, name.ToCamel()), key.Left)
|
||||
assert.Equal(t, fmt.Sprintf(`cache#User#%s#`, name.Untitle()), key.Right)
|
||||
assert.Equal(t, fmt.Sprintf(`user%sKey`, name.ToCamel()), key.Variable)
|
||||
assert.Equal(t, `user`+name.ToCamel()+`Key := fmt.Sprintf("%s%v", cacheUser`+name.ToCamel()+`Prefix,`+name.Untitle()+`)`, key.KeyExpression)
|
||||
}
|
||||
t.Run("primaryCacheKey", func(t *testing.T) {
|
||||
assert.Equal(t, true, func() bool {
|
||||
return cacheKeyEqual(primariCacheKey, Key{
|
||||
VarLeft: "cacheUserIdPrefix",
|
||||
VarRight: `"cache#user#id#"`,
|
||||
VarExpression: `cacheUserIdPrefix = "cache#user#id#"`,
|
||||
KeyLeft: "userIdKey",
|
||||
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
||||
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
|
||||
KeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
||||
DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
|
||||
FieldNameJoin: []string{"id"},
|
||||
})
|
||||
}())
|
||||
})
|
||||
|
||||
t.Run("uniqueCacheKey", func(t *testing.T) {
|
||||
assert.Equal(t, true, func() bool {
|
||||
expected := []Key{
|
||||
{
|
||||
VarLeft: "cacheUserClassNamePrefix",
|
||||
VarRight: `"cache#user#class#name#"`,
|
||||
VarExpression: `cacheUserClassNamePrefix = "cache#user#class#name#"`,
|
||||
KeyLeft: "userClassNameKey",
|
||||
KeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`,
|
||||
DataKeyRight: `fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
|
||||
KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, class, name)`,
|
||||
DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
|
||||
FieldNameJoin: []string{"class", "name"},
|
||||
},
|
||||
{
|
||||
VarLeft: "cacheUserMobilePrefix",
|
||||
VarRight: `"cache#user#mobile#"`,
|
||||
VarExpression: `cacheUserMobilePrefix = "cache#user#mobile#"`,
|
||||
KeyLeft: "userMobileKey",
|
||||
KeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
|
||||
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,
|
||||
KeyExpression: `userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
|
||||
DataKeyExpression: `userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,
|
||||
FieldNameJoin: []string{"mobile"},
|
||||
},
|
||||
}
|
||||
sort.Slice(uniqueCacheKey, func(i, j int) bool {
|
||||
return uniqueCacheKey[i].VarLeft < uniqueCacheKey[j].VarLeft
|
||||
})
|
||||
|
||||
if len(expected) != len(uniqueCacheKey) {
|
||||
return false
|
||||
}
|
||||
|
||||
for index, each := range uniqueCacheKey {
|
||||
expecting := expected[index]
|
||||
if !cacheKeyEqual(expecting, each) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}())
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func cacheKeyEqual(k1 Key, k2 Key) bool {
|
||||
k1Join := k1.FieldNameJoin
|
||||
k2Join := k2.FieldNameJoin
|
||||
sort.Strings(k1Join)
|
||||
sort.Strings(k2Join)
|
||||
if len(k1Join) != len(k2Join) {
|
||||
return false
|
||||
}
|
||||
|
||||
for index, each := range k1Join {
|
||||
k2Item := k2Join[index]
|
||||
if each != k2Item {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return k1.VarLeft == k2.VarLeft &&
|
||||
k1.VarRight == k2.VarRight &&
|
||||
k1.VarExpression == k2.VarExpression &&
|
||||
k1.KeyLeft == k2.KeyLeft &&
|
||||
k1.KeyRight == k2.KeyRight &&
|
||||
k1.DataKeyRight == k2.DataKeyRight &&
|
||||
k1.DataKeyExpression == k2.DataKeyExpression &&
|
||||
k1.KeyExpression == k2.KeyExpression
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user