Skip to content

Commit

Permalink
Fix null time not allowed in mysql5.7 test error
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Oct 19, 2016
1 parent 39165d4 commit 5d853fc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 44 deletions.
3 changes: 2 additions & 1 deletion create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 5 additions & 4 deletions customize_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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"`
ID int64 `gorm:"column:mapped_id; primary_key:yes"`
Name string `gorm:"column:mapped_name"`
Date *time.Time `gorm:"column:mapped_time"`
}

// Make sure an ignored field does not interfere with another field's custom
Expand All @@ -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")
Expand Down
46 changes: 27 additions & 19 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
11 changes: 6 additions & 5 deletions migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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") {
Expand Down
29 changes: 14 additions & 15 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"

"github.com/jinzhu/gorm"
"github.com/jinzhu/now"

"testing"
"time"
Expand Down Expand Up @@ -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%")

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5d853fc

Please sign in to comment.