Skip to content

Commit 72695c5

Browse files
committed
[go-gorm#6372] Fix failed test case
1 parent 68dccca commit 72695c5

File tree

2 files changed

+69
-63
lines changed

2 files changed

+69
-63
lines changed

tests/automigration_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package tests_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestSmartAutoMigrateColumnNullable(t *testing.T) {
9+
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
10+
11+
type UserMigrateColumn struct {
12+
ID uint
13+
Name string
14+
Salary float64
15+
Bonus float64 `gorm:"not null"`
16+
Stock float64
17+
Birthday time.Time `gorm:"precision:4"`
18+
}
19+
20+
DB.Migrator().DropTable(&UserMigrateColumn{})
21+
22+
DB.AutoMigrate(&UserMigrateColumn{})
23+
24+
type UserMigrateColumn2 struct {
25+
ID uint
26+
Name string `gorm:"size:128"`
27+
Salary float64 `gorm:"precision:2"`
28+
Bonus float64
29+
Stock float64 `gorm:"not null"`
30+
Birthday time.Time `gorm:"precision:2"`
31+
NameIgnoreMigration string `gorm:"size:100"`
32+
}
33+
34+
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
35+
t.Fatalf("failed to auto migrate, got error: %v", err)
36+
}
37+
38+
columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
39+
if err != nil {
40+
t.Fatalf("failed to get column types, got error: %v", err)
41+
}
42+
43+
for _, columnType := range columnTypes {
44+
switch columnType.Name() {
45+
case "name":
46+
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 {
47+
t.Fatalf("name's length should be 128, but got %v", length)
48+
}
49+
case "salary":
50+
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
51+
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
52+
}
53+
case "bonus":
54+
// allow to change non-nullable to nullable
55+
if nullable, _ := columnType.Nullable(); !nullable {
56+
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
57+
}
58+
case "stock":
59+
// do not allow to change nullable to non-nullable
60+
if nullable, _ := columnType.Nullable(); !nullable {
61+
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
62+
}
63+
case "birthday":
64+
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
65+
t.Fatalf("birthday's precision should be 2, but got %v", precision)
66+
}
67+
}
68+
}
69+
}

tests/migrate_test.go

-63
Original file line numberDiff line numberDiff line change
@@ -1945,66 +1945,3 @@ func TestMigrateWithUniqueIndexAndUnique(t *testing.T) {
19451945
}
19461946
}
19471947
}
1948-
1949-
func TestSmartMigrateColumnNullable(t *testing.T) {
1950-
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
1951-
1952-
type UserMigrateColumn struct {
1953-
ID uint
1954-
Name string
1955-
Salary float64
1956-
Bonus float64 `gorm:"not null"`
1957-
Stock float64
1958-
Birthday time.Time `gorm:"precision:4"`
1959-
}
1960-
1961-
DB.Migrator().DropTable(&UserMigrateColumn{})
1962-
1963-
DB.AutoMigrate(&UserMigrateColumn{})
1964-
1965-
type UserMigrateColumn2 struct {
1966-
ID uint
1967-
Name string `gorm:"size:128"`
1968-
Salary float64 `gorm:"precision:2"`
1969-
Bonus float64
1970-
Stock float64 `gorm:"not null"`
1971-
Birthday time.Time `gorm:"precision:2"`
1972-
NameIgnoreMigration string `gorm:"size:100"`
1973-
}
1974-
1975-
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
1976-
t.Fatalf("failed to auto migrate, got error: %v", err)
1977-
}
1978-
1979-
columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
1980-
if err != nil {
1981-
t.Fatalf("failed to get column types, got error: %v", err)
1982-
}
1983-
1984-
for _, columnType := range columnTypes {
1985-
switch columnType.Name() {
1986-
case "name":
1987-
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 {
1988-
t.Fatalf("name's length should be 128, but got %v", length)
1989-
}
1990-
case "salary":
1991-
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
1992-
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
1993-
}
1994-
case "bonus":
1995-
// allow to change non-nullable to nullable
1996-
if nullable, _ := columnType.Nullable(); !nullable {
1997-
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
1998-
}
1999-
case "stock":
2000-
// do not allow to change nullable to non-nullable
2001-
if nullable, _ := columnType.Nullable(); !nullable {
2002-
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
2003-
}
2004-
case "birthday":
2005-
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
2006-
t.Fatalf("birthday's precision should be 2, but got %v", precision)
2007-
}
2008-
}
2009-
}
2010-
}

0 commit comments

Comments
 (0)