index_test.go 2.7 KB

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