gorm/tests/migrate_test.go

30 lines
725 B
Go
Raw Normal View History

2020-05-23 18:50:48 +03:00
package tests_test
2020-02-22 14:41:01 +03:00
import (
2020-02-22 19:18:12 +03:00
"math/rand"
2020-02-22 14:41:01 +03:00
"testing"
2020-02-22 19:18:12 +03:00
"time"
2020-02-22 14:41:01 +03:00
2020-05-23 18:50:48 +03:00
. "github.com/jinzhu/gorm/tests"
2020-02-22 14:41:01 +03:00
)
2020-05-23 18:50:48 +03:00
func TestMigrate(t *testing.T) {
2020-02-22 18:08:20 +03:00
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
2020-02-22 19:18:12 +03:00
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
2020-02-22 14:41:01 +03:00
2020-05-23 18:50:48 +03:00
if err := DB.Migrator().DropTable(allModels...); err != nil {
2020-02-22 19:18:12 +03:00
t.Errorf("Failed to drop table, got error %v", err)
2020-02-22 15:57:29 +03:00
}
2020-05-23 18:50:48 +03:00
if err := DB.AutoMigrate(allModels...); err != nil {
2020-02-22 15:57:29 +03:00
t.Errorf("Failed to auto migrate, but got error %v", err)
}
2020-02-22 14:41:01 +03:00
for _, m := range allModels {
2020-05-23 18:50:48 +03:00
if !DB.Migrator().HasTable(m) {
2020-02-22 15:57:29 +03:00
t.Errorf("Failed to create table for %#v", m)
2020-02-22 14:41:01 +03:00
}
}
}