github.com/systematiccaos/gorm@v1.22.6/schema/index_test.go (about) 1 package schema_test 2 3 import ( 4 "reflect" 5 "sync" 6 "testing" 7 8 "github.com/systematiccaos/gorm/schema" 9 ) 10 11 type UserIndex struct { 12 Name string `gorm:"index"` 13 Name2 string `gorm:"index:idx_name,unique"` 14 Name3 string `gorm:"index:,sort:desc,collate:utf8,type:btree,length:10,where:name3 != 'jinzhu'"` 15 Name4 string `gorm:"uniqueIndex"` 16 Name5 int64 `gorm:"index:,class:FULLTEXT,comment:hello \\, world,where:age > 10"` 17 Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"` 18 Age int64 `gorm:"index:profile,expression:ABS(age),option:WITH PARSER parser_name"` 19 OID int64 `gorm:"index:idx_id;index:idx_oid,unique"` 20 MemberNumber string `gorm:"index:idx_id,priority:1"` 21 } 22 23 func TestParseIndex(t *testing.T) { 24 user, err := schema.Parse(&UserIndex{}, &sync.Map{}, schema.NamingStrategy{}) 25 if err != nil { 26 t.Fatalf("failed to parse user index, got error %v", err) 27 } 28 29 results := map[string]schema.Index{ 30 "idx_user_indices_name": { 31 Name: "idx_user_indices_name", 32 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name"}}}, 33 }, 34 "idx_name": { 35 Name: "idx_name", 36 Class: "UNIQUE", 37 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2"}}}, 38 }, 39 "idx_user_indices_name3": { 40 Name: "idx_user_indices_name3", 41 Type: "btree", 42 Where: "name3 != 'jinzhu'", 43 Fields: []schema.IndexOption{{ 44 Field: &schema.Field{Name: "Name3"}, 45 Sort: "desc", 46 Collate: "utf8", 47 Length: 10, 48 }}, 49 }, 50 "idx_user_indices_name4": { 51 Name: "idx_user_indices_name4", 52 Class: "UNIQUE", 53 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4"}}}, 54 }, 55 "idx_user_indices_name5": { 56 Name: "idx_user_indices_name5", 57 Class: "FULLTEXT", 58 Comment: "hello , world", 59 Where: "age > 10", 60 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name5"}}}, 61 }, 62 "profile": { 63 Name: "profile", 64 Comment: "hello , world", 65 Where: "age > 10", 66 Option: "WITH PARSER parser_name", 67 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, { 68 Field: &schema.Field{Name: "Age"}, 69 Expression: "ABS(age)", 70 }}, 71 }, 72 "idx_id": { 73 Name: "idx_id", 74 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID"}}}, 75 }, 76 "idx_oid": { 77 Name: "idx_oid", 78 Class: "UNIQUE", 79 Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}}, 80 }, 81 } 82 83 indices := user.ParseIndexes() 84 85 for k, result := range results { 86 v, ok := indices[k] 87 if !ok { 88 t.Fatalf("Failed to found index %v from parsed indices %+v", k, indices) 89 } 90 91 for _, name := range []string{"Name", "Class", "Type", "Where", "Comment", "Option"} { 92 if reflect.ValueOf(result).FieldByName(name).Interface() != reflect.ValueOf(v).FieldByName(name).Interface() { 93 t.Errorf( 94 "index %v %v should equal, expects %v, got %v", 95 k, name, reflect.ValueOf(result).FieldByName(name).Interface(), reflect.ValueOf(v).FieldByName(name).Interface(), 96 ) 97 } 98 } 99 100 for idx, ef := range result.Fields { 101 rf := v.Fields[idx] 102 if rf.Field.Name != ef.Field.Name { 103 t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name) 104 } 105 106 for _, name := range []string{"Expression", "Sort", "Collate", "Length"} { 107 if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() { 108 t.Errorf( 109 "index %v field #%v's %v should equal, expects %v, got %v", k, idx+1, name, 110 reflect.ValueOf(ef).FieldByName(name).Interface(), reflect.ValueOf(rf).FieldByName(name).Interface(), 111 ) 112 } 113 } 114 } 115 } 116 }