|
1 | 1 | package sqlserver_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "os"
|
5 | 6 | "reflect"
|
6 | 7 | "testing"
|
@@ -249,3 +250,56 @@ func TestMigrator_MigrateColumnComment(t *testing.T) {
|
249 | 250 | t.Logf("got comments: %#v", gotComments)
|
250 | 251 | }
|
251 | 252 | }
|
| 253 | + |
| 254 | +func TestMigrator_GetIndexes(t *testing.T) { |
| 255 | + db, err := gorm.Open(sqlserver.Open(sqlserverDSN)) |
| 256 | + if err != nil { |
| 257 | + t.Fatal(err) |
| 258 | + } |
| 259 | + dm := db.Debug().Migrator() |
| 260 | + |
| 261 | + type testTableIndex struct { |
| 262 | + Test uint64 `gorm:"index"` |
| 263 | + } |
| 264 | + type testTableUnique struct { |
| 265 | + ID string `gorm:"index:unique_id,class:UNIQUE,where:id IS NOT NULL"` |
| 266 | + } |
| 267 | + type testTablePrimaryKey struct { |
| 268 | + ID string `gorm:"primaryKey"` |
| 269 | + } |
| 270 | + |
| 271 | + type args struct { |
| 272 | + value interface{} |
| 273 | + } |
| 274 | + tests := []struct { |
| 275 | + name string |
| 276 | + args args |
| 277 | + wantErr bool |
| 278 | + }{ |
| 279 | + {name: "index", args: args{value: new(testTableIndex)}}, |
| 280 | + {name: "unique", args: args{value: new(testTableUnique)}}, |
| 281 | + {name: "primaryKey", args: args{value: new(testTablePrimaryKey)}}, |
| 282 | + } |
| 283 | + for _, tt := range tests { |
| 284 | + t.Run(tt.name, func(t *testing.T) { |
| 285 | + if err = dm.AutoMigrate(tt.args.value); err != nil { |
| 286 | + t.Error(err) |
| 287 | + } |
| 288 | + got, gotErr := dm.GetIndexes(tt.args.value) |
| 289 | + if (gotErr != nil) != tt.wantErr { |
| 290 | + t.Errorf("GetIndexes() error = %v, wantErr %v", gotErr, tt.wantErr) |
| 291 | + return |
| 292 | + } |
| 293 | + for _, index := range got { |
| 294 | + _, validUnique := index.Unique() |
| 295 | + _, validPK := index.PrimaryKey() |
| 296 | + indexBytes, _ := json.Marshal(index) |
| 297 | + if index.Name() == "" && !validUnique && !validPK { |
| 298 | + t.Errorf("GetIndexes() got = %s empty", indexBytes) |
| 299 | + } else { |
| 300 | + t.Logf("GetIndexes() got = %s", indexBytes) |
| 301 | + } |
| 302 | + } |
| 303 | + }) |
| 304 | + } |
| 305 | +} |
0 commit comments