[#6372] Fix failed test case
This commit is contained in:
parent
72695c5087
commit
b091b56cd2
@ -1,69 +0,0 @@
|
|||||||
package tests_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSmartAutoMigrateColumnNullable(t *testing.T) {
|
|
||||||
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
|
|
||||||
|
|
||||||
type UserMigrateColumn struct {
|
|
||||||
ID uint
|
|
||||||
Name string
|
|
||||||
Salary float64
|
|
||||||
Bonus float64 `gorm:"not null"`
|
|
||||||
Stock float64
|
|
||||||
Birthday time.Time `gorm:"precision:4"`
|
|
||||||
}
|
|
||||||
|
|
||||||
DB.Migrator().DropTable(&UserMigrateColumn{})
|
|
||||||
|
|
||||||
DB.AutoMigrate(&UserMigrateColumn{})
|
|
||||||
|
|
||||||
type UserMigrateColumn2 struct {
|
|
||||||
ID uint
|
|
||||||
Name string `gorm:"size:128"`
|
|
||||||
Salary float64 `gorm:"precision:2"`
|
|
||||||
Bonus float64
|
|
||||||
Stock float64 `gorm:"not null"`
|
|
||||||
Birthday time.Time `gorm:"precision:2"`
|
|
||||||
NameIgnoreMigration string `gorm:"size:100"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
|
|
||||||
t.Fatalf("failed to auto migrate, got error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get column types, got error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, columnType := range columnTypes {
|
|
||||||
switch columnType.Name() {
|
|
||||||
case "name":
|
|
||||||
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 {
|
|
||||||
t.Fatalf("name's length should be 128, but got %v", length)
|
|
||||||
}
|
|
||||||
case "salary":
|
|
||||||
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
|
||||||
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
|
|
||||||
}
|
|
||||||
case "bonus":
|
|
||||||
// allow to change non-nullable to nullable
|
|
||||||
if nullable, _ := columnType.Nullable(); !nullable {
|
|
||||||
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
|
|
||||||
}
|
|
||||||
case "stock":
|
|
||||||
// do not allow to change nullable to non-nullable
|
|
||||||
if nullable, _ := columnType.Nullable(); !nullable {
|
|
||||||
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
|
|
||||||
}
|
|
||||||
case "birthday":
|
|
||||||
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
|
||||||
t.Fatalf("birthday's precision should be 2, but got %v", precision)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -148,6 +148,8 @@ func TestSmartMigrateColumn(t *testing.T) {
|
|||||||
ID uint
|
ID uint
|
||||||
Name string
|
Name string
|
||||||
Salary float64
|
Salary float64
|
||||||
|
Bonus float64 `gorm:"not null"`
|
||||||
|
Stock float64
|
||||||
Birthday time.Time `gorm:"precision:4"`
|
Birthday time.Time `gorm:"precision:4"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,8 +159,10 @@ func TestSmartMigrateColumn(t *testing.T) {
|
|||||||
|
|
||||||
type UserMigrateColumn2 struct {
|
type UserMigrateColumn2 struct {
|
||||||
ID uint
|
ID uint
|
||||||
Name string `gorm:"size:128"`
|
Name string `gorm:"size:128"`
|
||||||
Salary float64 `gorm:"precision:2"`
|
Salary float64 `gorm:"precision:2"`
|
||||||
|
Bonus float64
|
||||||
|
Stock float64 `gorm:"not null"`
|
||||||
Birthday time.Time `gorm:"precision:2"`
|
Birthday time.Time `gorm:"precision:2"`
|
||||||
NameIgnoreMigration string `gorm:"size:100"`
|
NameIgnoreMigration string `gorm:"size:100"`
|
||||||
}
|
}
|
||||||
@ -182,6 +186,16 @@ func TestSmartMigrateColumn(t *testing.T) {
|
|||||||
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
||||||
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
|
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
|
||||||
}
|
}
|
||||||
|
case "bonus":
|
||||||
|
// allow to change non-nullable to nullable
|
||||||
|
if nullable, _ := columnType.Nullable(); !nullable {
|
||||||
|
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
|
||||||
|
}
|
||||||
|
case "stock":
|
||||||
|
// do not allow to change nullable to non-nullable
|
||||||
|
if nullable, _ := columnType.Nullable(); !nullable {
|
||||||
|
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
|
||||||
|
}
|
||||||
case "birthday":
|
case "birthday":
|
||||||
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
|
||||||
t.Fatalf("birthday's precision should be 2, but got %v", precision)
|
t.Fatalf("birthday's precision should be 2, but got %v", precision)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user