gorm/schema/index_test.go

110 lines
3.0 KiB
Go
Raw Normal View History

2020-02-21 18:51:38 +03:00
package schema_test
import (
"reflect"
"sync"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm/schema"
2020-02-21 18:51:38 +03:00
)
type UserIndex struct {
2020-04-03 02:57:52 +03:00
Name string `gorm:"index"`
Name2 string `gorm:"index:idx_name,unique"`
Name3 string `gorm:"index:,sort:desc,collate:utf8,type:btree,length:10,where:name3 != 'jinzhu'"`
2020-07-03 19:36:27 +03:00
Name4 string `gorm:"uniqueIndex"`
2020-04-03 02:57:52 +03:00
Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"`
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
Age int64 `gorm:"index:profile,expression:ABS(age)"`
2020-07-01 06:47:46 +03:00
OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
2020-04-03 02:57:52 +03:00
MemberNumber string `gorm:"index:idx_id"`
2020-02-21 18:51:38 +03:00
}
func TestParseIndex(t *testing.T) {
2020-02-24 03:51:35 +03:00
user, err := schema.Parse(&UserIndex{}, &sync.Map{}, schema.NamingStrategy{})
2020-02-21 18:51:38 +03:00
if err != nil {
2020-02-22 14:41:01 +03:00
t.Fatalf("failed to parse user index, got error %v", err)
2020-02-21 18:51:38 +03:00
}
results := map[string]schema.Index{
"idx_user_indices_name": {
Name: "idx_user_indices_name",
Fields: []schema.IndexOption{{}},
},
"idx_name": {
Name: "idx_name",
Class: "UNIQUE",
Fields: []schema.IndexOption{{}},
},
"idx_user_indices_name3": {
2020-02-21 19:02:05 +03:00
Name: "idx_user_indices_name3",
Type: "btree",
Where: "name3 != 'jinzhu'",
2020-02-21 18:51:38 +03:00
Fields: []schema.IndexOption{{
Sort: "desc",
Collate: "utf8",
Length: 10,
}},
},
"idx_user_indices_name4": {
Name: "idx_user_indices_name4",
Class: "UNIQUE",
Fields: []schema.IndexOption{{}},
},
"idx_user_indices_name5": {
2020-02-21 19:02:05 +03:00
Name: "idx_user_indices_name5",
Class: "FULLTEXT",
Comment: "hello , world",
Where: "age > 10",
Fields: []schema.IndexOption{{}},
2020-02-21 18:51:38 +03:00
},
"profile": {
2020-02-21 19:02:05 +03:00
Name: "profile",
Comment: "hello , world",
Where: "age > 10",
Fields: []schema.IndexOption{{}, {
2020-02-22 06:15:51 +03:00
Expression: "ABS(age)",
2020-02-21 18:51:38 +03:00
}},
},
2020-04-03 02:57:52 +03:00
"idx_id": {
Name: "idx_id",
Fields: []schema.IndexOption{{}, {}},
},
2020-07-01 06:47:46 +03:00
"idx_oid": {
Name: "idx_oid",
Class: "UNIQUE",
Fields: []schema.IndexOption{{}},
},
2020-02-21 18:51:38 +03:00
}
indices := user.ParseIndexes()
for k, result := range results {
v, ok := indices[k]
if !ok {
2020-04-03 02:57:52 +03:00
t.Fatalf("Failed to found index %v from parsed indices %+v", k, indices)
2020-02-21 18:51:38 +03:00
}
2020-02-21 19:02:05 +03:00
for _, name := range []string{"Name", "Class", "Type", "Where", "Comment"} {
if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() {
t.Errorf(
"index %v %v should equal, expects %v, got %v",
k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(),
)
}
2020-02-21 18:51:38 +03:00
}
for idx, ef := range result.Fields {
rf := v.Fields[idx]
2020-02-21 19:02:05 +03:00
for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
2020-02-21 18:51:38 +03:00
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {
2020-02-21 19:02:05 +03:00
t.Errorf(
"index %v field #%v's %v should equal, expects %v, got %v", k, idx+1, name,
reflect.ValueOf(ef).FieldByName(name).Interface(), reflect.ValueOf(rf).FieldByName(name).Interface(),
)
2020-02-21 18:51:38 +03:00
}
}
}
}
}