forked from jinzhu/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix null time not allowed in mysql5.7 test error
- Loading branch information
Showing
5 changed files
with
54 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,9 +371,9 @@ func TestTransaction(t *testing.T) { | |
} | ||
|
||
func TestRow(t *testing.T) { | ||
user1 := User{Name: "RowUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} | ||
user2 := User{Name: "RowUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} | ||
user3 := User{Name: "RowUser3", Age: 20, Birthday: now.MustParse("2020-1-1")} | ||
user1 := User{Name: "RowUser1", Age: 1, Birthday: parseTime("2000-1-1")} | ||
user2 := User{Name: "RowUser2", Age: 10, Birthday: parseTime("2010-1-1")} | ||
user3 := User{Name: "RowUser3", Age: 20, Birthday: parseTime("2020-1-1")} | ||
DB.Save(&user1).Save(&user2).Save(&user3) | ||
|
||
row := DB.Table("users").Where("name = ?", user2.Name).Select("age").Row() | ||
|
@@ -385,9 +385,9 @@ func TestRow(t *testing.T) { | |
} | ||
|
||
func TestRows(t *testing.T) { | ||
user1 := User{Name: "RowsUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} | ||
user2 := User{Name: "RowsUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} | ||
user3 := User{Name: "RowsUser3", Age: 20, Birthday: now.MustParse("2020-1-1")} | ||
user1 := User{Name: "RowsUser1", Age: 1, Birthday: parseTime("2000-1-1")} | ||
user2 := User{Name: "RowsUser2", Age: 10, Birthday: parseTime("2010-1-1")} | ||
user3 := User{Name: "RowsUser3", Age: 20, Birthday: parseTime("2020-1-1")} | ||
DB.Save(&user1).Save(&user2).Save(&user3) | ||
|
||
rows, err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("name, age").Rows() | ||
|
@@ -409,9 +409,9 @@ func TestRows(t *testing.T) { | |
} | ||
|
||
func TestScanRows(t *testing.T) { | ||
user1 := User{Name: "ScanRowsUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} | ||
user2 := User{Name: "ScanRowsUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} | ||
user3 := User{Name: "ScanRowsUser3", Age: 20, Birthday: now.MustParse("2020-1-1")} | ||
user1 := User{Name: "ScanRowsUser1", Age: 1, Birthday: parseTime("2000-1-1")} | ||
user2 := User{Name: "ScanRowsUser2", Age: 10, Birthday: parseTime("2010-1-1")} | ||
user3 := User{Name: "ScanRowsUser3", Age: 20, Birthday: parseTime("2020-1-1")} | ||
DB.Save(&user1).Save(&user2).Save(&user3) | ||
|
||
rows, err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("name, age").Rows() | ||
|
@@ -439,9 +439,9 @@ func TestScanRows(t *testing.T) { | |
} | ||
|
||
func TestScan(t *testing.T) { | ||
user1 := User{Name: "ScanUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} | ||
user2 := User{Name: "ScanUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} | ||
user3 := User{Name: "ScanUser3", Age: 20, Birthday: now.MustParse("2020-1-1")} | ||
user1 := User{Name: "ScanUser1", Age: 1, Birthday: parseTime("2000-1-1")} | ||
user2 := User{Name: "ScanUser2", Age: 10, Birthday: parseTime("2010-1-1")} | ||
user3 := User{Name: "ScanUser3", Age: 20, Birthday: parseTime("2020-1-1")} | ||
DB.Save(&user1).Save(&user2).Save(&user3) | ||
|
||
type result struct { | ||
|
@@ -469,9 +469,9 @@ func TestScan(t *testing.T) { | |
} | ||
|
||
func TestRaw(t *testing.T) { | ||
user1 := User{Name: "ExecRawSqlUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} | ||
user2 := User{Name: "ExecRawSqlUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} | ||
user3 := User{Name: "ExecRawSqlUser3", Age: 20, Birthday: now.MustParse("2020-1-1")} | ||
user1 := User{Name: "ExecRawSqlUser1", Age: 1, Birthday: parseTime("2000-1-1")} | ||
user2 := User{Name: "ExecRawSqlUser2", Age: 10, Birthday: parseTime("2010-1-1")} | ||
user3 := User{Name: "ExecRawSqlUser3", Age: 20, Birthday: parseTime("2020-1-1")} | ||
DB.Save(&user1).Save(&user2).Save(&user3) | ||
|
||
type result struct { | ||
|
@@ -611,11 +611,12 @@ func TestTimeWithZone(t *testing.T) { | |
|
||
for index, vtime := range times { | ||
name := "time_with_zone_" + strconv.Itoa(index) | ||
user := User{Name: name, Birthday: vtime} | ||
user := User{Name: name, Birthday: &vtime} | ||
|
||
if !DialectHasTzSupport() { | ||
// If our driver dialect doesn't support TZ's, just use UTC for everything here. | ||
user.Birthday = vtime.UTC() | ||
utcBirthday := user.Birthday.UTC() | ||
user.Birthday = &utcBirthday | ||
} | ||
|
||
DB.Save(&user) | ||
|
@@ -758,7 +759,8 @@ func BenchmarkGorm(b *testing.B) { | |
b.N = 2000 | ||
for x := 0; x < b.N; x++ { | ||
e := strconv.Itoa(x) + "[email protected]" | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: time.Now()} | ||
now := time.Now() | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
// Insert | ||
DB.Save(&email) | ||
// Query | ||
|
@@ -782,7 +784,8 @@ func BenchmarkRawSql(b *testing.B) { | |
for x := 0; x < b.N; x++ { | ||
var id int64 | ||
e := strconv.Itoa(x) + "[email protected]" | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: time.Now()} | ||
now := time.Now() | ||
email := BigEmail{Email: e, UserAgent: "pc", RegisteredAt: &now} | ||
// Insert | ||
DB.QueryRow(insertSql, email.UserId, email.Email, email.UserAgent, email.RegisteredAt, time.Now(), time.Now()).Scan(&id) | ||
// Query | ||
|
@@ -794,3 +797,8 @@ func BenchmarkRawSql(b *testing.B) { | |
DB.Exec(deleteSql, id) | ||
} | ||
} | ||
|
||
func parseTime(str string) *time.Time { | ||
t := now.MustParse(str) | ||
return &t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ type User struct { | |
UserNum Num | ||
Name string `sql:"size:255"` | ||
Email string | ||
Birthday time.Time // Time | ||
Birthday *time.Time // Time | ||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically | ||
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically | ||
Emails []Email // Embedded structs | ||
|
@@ -333,9 +333,9 @@ func TestIndexes(t *testing.T) { | |
type BigEmail struct { | ||
Id int64 | ||
UserId int64 | ||
Email string `sql:"index:idx_email_agent"` | ||
UserAgent string `sql:"index:idx_email_agent"` | ||
RegisteredAt time.Time `sql:"unique_index"` | ||
Email string `sql:"index:idx_email_agent"` | ||
UserAgent string `sql:"index:idx_email_agent"` | ||
RegisteredAt *time.Time `sql:"unique_index"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
|
@@ -350,7 +350,8 @@ func TestAutoMigration(t *testing.T) { | |
t.Errorf("Auto Migrate should not raise any error") | ||
} | ||
|
||
DB.Save(&BigEmail{Email: "[email protected]", UserAgent: "pc", RegisteredAt: time.Now()}) | ||
now := time.Now() | ||
DB.Save(&BigEmail{Email: "[email protected]", UserAgent: "pc", RegisteredAt: &now}) | ||
|
||
scope := DB.NewScope(&BigEmail{}) | ||
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_agent") { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters