mirror of https://github.com/go-gorm/gorm.git
Fix null time not allowed in mysql5.7 test error
This commit is contained in:
parent
39165d4980
commit
5d853fc53c
|
@ -9,7 +9,8 @@ import (
|
|||
|
||||
func TestCreate(t *testing.T) {
|
||||
float := 35.03554004971999
|
||||
user := User{Name: "CreateUser", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
|
||||
now := time.Now()
|
||||
user := User{Name: "CreateUser", Age: 18, Birthday: &now, UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
|
||||
|
||||
if !DB.NewRecord(user) || !DB.NewRecord(&user) {
|
||||
t.Error("User should be new record before create")
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
type CustomizeColumn struct {
|
||||
ID int64 `gorm:"column:mapped_id; primary_key:yes"`
|
||||
Name string `gorm:"column:mapped_name"`
|
||||
Date time.Time `gorm:"column:mapped_time"`
|
||||
Date *time.Time `gorm:"column:mapped_time"`
|
||||
}
|
||||
|
||||
// Make sure an ignored field does not interfere with another field's custom
|
||||
|
@ -36,7 +36,8 @@ func TestCustomizeColumn(t *testing.T) {
|
|||
}
|
||||
|
||||
expected := "foo"
|
||||
cc := CustomizeColumn{ID: 666, Name: expected, Date: time.Now()}
|
||||
now := time.Now()
|
||||
cc := CustomizeColumn{ID: 666, Name: expected, Date: &now}
|
||||
|
||||
if count := DB.Create(&cc).RowsAffected; count != 1 {
|
||||
t.Error("There should be one record be affected when create record")
|
||||
|
|
46
main_test.go
46
main_test.go
|
@ -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) + "benchmark@example.org"
|
||||
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) + "benchmark@example.org"
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -335,7 +335,7 @@ type BigEmail struct {
|
|||
UserId int64
|
||||
Email string `sql:"index:idx_email_agent"`
|
||||
UserAgent string `sql:"index:idx_email_agent"`
|
||||
RegisteredAt time.Time `sql:"unique_index"`
|
||||
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: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()})
|
||||
now := time.Now()
|
||||
DB.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: &now})
|
||||
|
||||
scope := DB.NewScope(&BigEmail{})
|
||||
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_agent") {
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"reflect"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/jinzhu/now"
|
||||
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -100,9 +99,9 @@ func TestFindAsSliceOfPointers(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSearchWithPlainSQL(t *testing.T) {
|
||||
user1 := User{Name: "PlainSqlUser1", Age: 1, Birthday: now.MustParse("2000-1-1")}
|
||||
user2 := User{Name: "PlainSqlUser2", Age: 10, Birthday: now.MustParse("2010-1-1")}
|
||||
user3 := User{Name: "PlainSqlUser3", Age: 20, Birthday: now.MustParse("2020-1-1")}
|
||||
user1 := User{Name: "PlainSqlUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
||||
user2 := User{Name: "PlainSqlUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
||||
user3 := User{Name: "PlainSqlUser3", Age: 20, Birthday: parseTime("2020-1-1")}
|
||||
DB.Save(&user1).Save(&user2).Save(&user3)
|
||||
scopedb := DB.Where("name LIKE ?", "%PlainSqlUser%")
|
||||
|
||||
|
@ -130,7 +129,7 @@ func TestSearchWithPlainSQL(t *testing.T) {
|
|||
t.Errorf("Should found 2 users age != 20, but got %v", len(users))
|
||||
}
|
||||
|
||||
scopedb.Where("birthday > ?", now.MustParse("2000-1-1")).Find(&users)
|
||||
scopedb.Where("birthday > ?", parseTime("2000-1-1")).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should found 2 users's birthday > 2000-1-1, but got %v", len(users))
|
||||
}
|
||||
|
@ -174,9 +173,9 @@ func TestSearchWithPlainSQL(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSearchWithStruct(t *testing.T) {
|
||||
user1 := User{Name: "StructSearchUser1", Age: 1, Birthday: now.MustParse("2000-1-1")}
|
||||
user2 := User{Name: "StructSearchUser2", Age: 10, Birthday: now.MustParse("2010-1-1")}
|
||||
user3 := User{Name: "StructSearchUser3", Age: 20, Birthday: now.MustParse("2020-1-1")}
|
||||
user1 := User{Name: "StructSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
||||
user2 := User{Name: "StructSearchUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
||||
user3 := User{Name: "StructSearchUser3", Age: 20, Birthday: parseTime("2020-1-1")}
|
||||
DB.Save(&user1).Save(&user2).Save(&user3)
|
||||
|
||||
if DB.Where(user1.Id).First(&User{}).RecordNotFound() {
|
||||
|
@ -221,10 +220,10 @@ func TestSearchWithStruct(t *testing.T) {
|
|||
|
||||
func TestSearchWithMap(t *testing.T) {
|
||||
companyID := 1
|
||||
user1 := User{Name: "MapSearchUser1", Age: 1, Birthday: now.MustParse("2000-1-1")}
|
||||
user2 := User{Name: "MapSearchUser2", Age: 10, Birthday: now.MustParse("2010-1-1")}
|
||||
user3 := User{Name: "MapSearchUser3", Age: 20, Birthday: now.MustParse("2020-1-1")}
|
||||
user4 := User{Name: "MapSearchUser4", Age: 30, Birthday: now.MustParse("2020-1-1"), CompanyID: &companyID}
|
||||
user1 := User{Name: "MapSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
||||
user2 := User{Name: "MapSearchUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
||||
user3 := User{Name: "MapSearchUser3", Age: 20, Birthday: parseTime("2020-1-1")}
|
||||
user4 := User{Name: "MapSearchUser4", Age: 30, Birthday: parseTime("2020-1-1"), CompanyID: &companyID}
|
||||
DB.Save(&user1).Save(&user2).Save(&user3).Save(&user4)
|
||||
|
||||
var user User
|
||||
|
@ -267,9 +266,9 @@ func TestSearchWithMap(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSearchWithEmptyChain(t *testing.T) {
|
||||
user1 := User{Name: "ChainSearchUser1", Age: 1, Birthday: now.MustParse("2000-1-1")}
|
||||
user2 := User{Name: "ChainearchUser2", Age: 10, Birthday: now.MustParse("2010-1-1")}
|
||||
user3 := User{Name: "ChainearchUser3", Age: 20, Birthday: now.MustParse("2020-1-1")}
|
||||
user1 := User{Name: "ChainSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
||||
user2 := User{Name: "ChainearchUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
||||
user3 := User{Name: "ChainearchUser3", Age: 20, Birthday: parseTime("2020-1-1")}
|
||||
DB.Save(&user1).Save(&user2).Save(&user3)
|
||||
|
||||
if DB.Where("").Where("").First(&User{}).Error != nil {
|
||||
|
|
Loading…
Reference in New Issue