15 "github.com/erikstmartin/go-testdb"
16 "github.com/jinzhu/gorm"
17 _ "github.com/jinzhu/gorm/dialects/mssql"
18 _ "github.com/jinzhu/gorm/dialects/mysql"
19 "github.com/jinzhu/gorm/dialects/postgres"
20 _ "github.com/jinzhu/gorm/dialects/sqlite"
21 "github.com/jinzhu/now"
26 t1, t2, t3, t4, t5 time.Time
32 if DB, err = OpenTestConnection(); err != nil {
33 panic(fmt.Sprintf("No error should happen when connecting to test database, but got err=%+v", err))
39 func OpenTestConnection() (db *gorm.DB, err error) {
40 dbDSN := os.Getenv("GORM_DSN")
41 switch os.Getenv("GORM_DIALECT") {
43 fmt.Println("testing mysql...")
45 dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True"
47 db, err = gorm.Open("mysql", dbDSN)
49 fmt.Println("testing postgres...")
51 dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
53 db, err = gorm.Open("postgres", dbDSN)
55 // CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
56 // CREATE DATABASE gorm;
58 // CREATE USER gorm FROM LOGIN gorm;
59 // sp_changedbowner 'gorm';
60 fmt.Println("testing mssql...")
62 dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
64 db, err = gorm.Open("mssql", dbDSN)
66 fmt.Println("testing sqlite3...")
67 db, err = gorm.Open("sqlite3", filepath.Join(os.TempDir(), "gorm.db"))
70 // db.SetLogger(Logger{log.New(os.Stdout, "\r\n", 0)})
71 // db.SetLogger(log.New(os.Stdout, "\r\n", 0))
72 if debug := os.Getenv("DEBUG"); debug == "true" {
74 } else if debug == "false" {
78 db.DB().SetMaxIdleConns(10)
83 func TestOpen_ReturnsError_WithBadArgs(t *testing.T) {
85 testCases := []interface{}{42, time.Now(), &stringRef}
86 for _, tc := range testCases {
87 t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
88 _, err := gorm.Open("postgresql", tc)
90 t.Error("Should got error with invalid database source")
92 if !strings.HasPrefix(err.Error(), "invalid database source:") {
93 t.Errorf("Should got error starting with \"invalid database source:\", but got %q", err.Error())
99 func TestStringPrimaryKey(t *testing.T) {
100 type UUIDStruct struct {
101 ID string `gorm:"primary_key"`
104 DB.DropTable(&UUIDStruct{})
105 DB.AutoMigrate(&UUIDStruct{})
107 data := UUIDStruct{ID: "uuid", Name: "hello"}
108 if err := DB.Save(&data).Error; err != nil || data.ID != "uuid" || data.Name != "hello" {
109 t.Errorf("string primary key should not be populated")
112 data = UUIDStruct{ID: "uuid", Name: "hello world"}
113 if err := DB.Save(&data).Error; err != nil || data.ID != "uuid" || data.Name != "hello world" {
114 t.Errorf("string primary key should not be populated")
118 func TestExceptionsWithInvalidSql(t *testing.T) {
120 if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
121 t.Errorf("Should got error with invalid SQL")
124 if DB.Model(&User{}).Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {
125 t.Errorf("Should got error with invalid SQL")
128 if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Find(&User{}).Error == nil {
129 t.Errorf("Should got error with invalid SQL")
132 var count1, count2 int64
133 DB.Model(&User{}).Count(&count1)
135 t.Errorf("Should find some users")
138 if DB.Where("name = ?", "jinzhu; delete * from users").First(&User{}).Error == nil {
139 t.Errorf("Should got error with invalid SQL")
142 DB.Model(&User{}).Count(&count2)
143 if count1 != count2 {
144 t.Errorf("No user should not be deleted by invalid SQL")
148 func TestSetTable(t *testing.T) {
149 DB.Create(getPreparedUser("pluck_user1", "pluck_user"))
150 DB.Create(getPreparedUser("pluck_user2", "pluck_user"))
151 DB.Create(getPreparedUser("pluck_user3", "pluck_user"))
153 if err := DB.Table("users").Where("role = ?", "pluck_user").Pluck("age", &[]int{}).Error; err != nil {
154 t.Error("No errors should happen if set table for pluck", err)
158 if DB.Table("users").Find(&[]User{}).Error != nil {
159 t.Errorf("No errors should happen if set table for find")
162 if DB.Table("invalid_table").Find(&users).Error == nil {
163 t.Errorf("Should got error when table is set to an invalid table")
166 DB.Exec("drop table deleted_users;")
167 if DB.Table("deleted_users").CreateTable(&User{}).Error != nil {
168 t.Errorf("Create table with specified table")
171 DB.Table("deleted_users").Save(&User{Name: "DeletedUser"})
173 var deletedUsers []User
174 DB.Table("deleted_users").Find(&deletedUsers)
175 if len(deletedUsers) != 1 {
176 t.Errorf("Query from specified table")
179 DB.Save(getPreparedUser("normal_user", "reset_table"))
180 DB.Table("deleted_users").Save(getPreparedUser("deleted_user", "reset_table"))
181 var user1, user2, user3 User
182 DB.Where("role = ?", "reset_table").First(&user1).Table("deleted_users").First(&user2).Table("").First(&user3)
183 if (user1.Name != "normal_user") || (user2.Name != "deleted_user") || (user3.Name != "normal_user") {
184 t.Errorf("unset specified table with blank string")
194 func (c Cart) TableName() string {
195 return "shopping_cart"
198 func TestHasTable(t *testing.T) {
205 // Table should not exist at this point, HasTable should return false
206 if ok := DB.HasTable("foos"); ok {
207 t.Errorf("Table should not exist, but does")
209 if ok := DB.HasTable(&Foo{}); ok {
210 t.Errorf("Table should not exist, but does")
213 // We create the table
214 if err := DB.CreateTable(&Foo{}).Error; err != nil {
215 t.Errorf("Table should be created")
218 // And now it should exits, and HasTable should return true
219 if ok := DB.HasTable("foos"); !ok {
220 t.Errorf("Table should exist, but HasTable informs it does not")
222 if ok := DB.HasTable(&Foo{}); !ok {
223 t.Errorf("Table should exist, but HasTable informs it does not")
227 func TestTableName(t *testing.T) {
229 if DB.NewScope(Order{}).TableName() != "orders" {
230 t.Errorf("Order's table name should be orders")
233 if DB.NewScope(&Order{}).TableName() != "orders" {
234 t.Errorf("&Order's table name should be orders")
237 if DB.NewScope([]Order{}).TableName() != "orders" {
238 t.Errorf("[]Order's table name should be orders")
241 if DB.NewScope(&[]Order{}).TableName() != "orders" {
242 t.Errorf("&[]Order's table name should be orders")
245 DB.SingularTable(true)
246 if DB.NewScope(Order{}).TableName() != "order" {
247 t.Errorf("Order's singular table name should be order")
250 if DB.NewScope(&Order{}).TableName() != "order" {
251 t.Errorf("&Order's singular table name should be order")
254 if DB.NewScope([]Order{}).TableName() != "order" {
255 t.Errorf("[]Order's singular table name should be order")
258 if DB.NewScope(&[]Order{}).TableName() != "order" {
259 t.Errorf("&[]Order's singular table name should be order")
262 if DB.NewScope(&Cart{}).TableName() != "shopping_cart" {
263 t.Errorf("&Cart's singular table name should be shopping_cart")
266 if DB.NewScope(Cart{}).TableName() != "shopping_cart" {
267 t.Errorf("Cart's singular table name should be shopping_cart")
270 if DB.NewScope(&[]Cart{}).TableName() != "shopping_cart" {
271 t.Errorf("&[]Cart's singular table name should be shopping_cart")
274 if DB.NewScope([]Cart{}).TableName() != "shopping_cart" {
275 t.Errorf("[]Cart's singular table name should be shopping_cart")
277 DB.SingularTable(false)
280 func TestNullValues(t *testing.T) {
281 DB.DropTable(&NullValue{})
282 DB.AutoMigrate(&NullValue{})
284 if err := DB.Save(&NullValue{
285 Name: sql.NullString{String: "hello", Valid: true},
286 Gender: &sql.NullString{String: "M", Valid: true},
287 Age: sql.NullInt64{Int64: 18, Valid: true},
288 Male: sql.NullBool{Bool: true, Valid: true},
289 Height: sql.NullFloat64{Float64: 100.11, Valid: true},
290 AddedAt: NullTime{Time: time.Now(), Valid: true},
291 }).Error; err != nil {
292 t.Errorf("Not error should raise when test null value")
296 DB.First(&nv, "name = ?", "hello")
298 if nv.Name.String != "hello" || nv.Gender.String != "M" || nv.Age.Int64 != 18 || nv.Male.Bool != true || nv.Height.Float64 != 100.11 || nv.AddedAt.Valid != true {
299 t.Errorf("Should be able to fetch null value")
302 if err := DB.Save(&NullValue{
303 Name: sql.NullString{String: "hello-2", Valid: true},
304 Gender: &sql.NullString{String: "F", Valid: true},
305 Age: sql.NullInt64{Int64: 18, Valid: false},
306 Male: sql.NullBool{Bool: true, Valid: true},
307 Height: sql.NullFloat64{Float64: 100.11, Valid: true},
308 AddedAt: NullTime{Time: time.Now(), Valid: false},
309 }).Error; err != nil {
310 t.Errorf("Not error should raise when test null value")
314 DB.First(&nv2, "name = ?", "hello-2")
315 if nv2.Name.String != "hello-2" || nv2.Gender.String != "F" || nv2.Age.Int64 != 0 || nv2.Male.Bool != true || nv2.Height.Float64 != 100.11 || nv2.AddedAt.Valid != false {
316 t.Errorf("Should be able to fetch null value")
319 if err := DB.Save(&NullValue{
320 Name: sql.NullString{String: "hello-3", Valid: false},
321 Gender: &sql.NullString{String: "M", Valid: true},
322 Age: sql.NullInt64{Int64: 18, Valid: false},
323 Male: sql.NullBool{Bool: true, Valid: true},
324 Height: sql.NullFloat64{Float64: 100.11, Valid: true},
325 AddedAt: NullTime{Time: time.Now(), Valid: false},
326 }).Error; err == nil {
327 t.Errorf("Can't save because of name can't be null")
331 func TestNullValuesWithFirstOrCreate(t *testing.T) {
333 Name: sql.NullString{String: "first_or_create", Valid: true},
334 Gender: &sql.NullString{String: "M", Valid: true},
338 result := DB.Where(nv1).FirstOrCreate(&nv2)
340 if result.RowsAffected != 1 {
341 t.Errorf("RowsAffected should be 1 after create some record")
344 if result.Error != nil {
345 t.Errorf("Should not raise any error, but got %v", result.Error)
348 if nv2.Name.String != "first_or_create" || nv2.Gender.String != "M" {
349 t.Errorf("first or create with nullvalues")
352 if err := DB.Where(nv1).Assign(NullValue{Age: sql.NullInt64{Int64: 18, Valid: true}}).FirstOrCreate(&nv2).Error; err != nil {
353 t.Errorf("Should not raise any error, but got %v", err)
356 if nv2.Age.Int64 != 18 {
357 t.Errorf("should update age to 18")
361 func TestTransaction(t *testing.T) {
363 u := User{Name: "transcation"}
364 if err := tx.Save(&u).Error; err != nil {
365 t.Errorf("No error should raise")
368 if err := tx.First(&User{}, "name = ?", "transcation").Error; err != nil {
369 t.Errorf("Should find saved record")
372 if sqlTx, ok := tx.CommonDB().(*sql.Tx); !ok || sqlTx == nil {
373 t.Errorf("Should return the underlying sql.Tx")
378 if err := tx.First(&User{}, "name = ?", "transcation").Error; err == nil {
379 t.Errorf("Should not find record after rollback")
383 u2 := User{Name: "transcation-2"}
384 if err := tx2.Save(&u2).Error; err != nil {
385 t.Errorf("No error should raise")
388 if err := tx2.First(&User{}, "name = ?", "transcation-2").Error; err != nil {
389 t.Errorf("Should find saved record")
394 if err := DB.First(&User{}, "name = ?", "transcation-2").Error; err != nil {
395 t.Errorf("Should be able to find committed record")
399 func TestRow(t *testing.T) {
400 user1 := User{Name: "RowUser1", Age: 1, Birthday: parseTime("2000-1-1")}
401 user2 := User{Name: "RowUser2", Age: 10, Birthday: parseTime("2010-1-1")}
402 user3 := User{Name: "RowUser3", Age: 20, Birthday: parseTime("2020-1-1")}
403 DB.Save(&user1).Save(&user2).Save(&user3)
405 row := DB.Table("users").Where("name = ?", user2.Name).Select("age").Row()
409 t.Errorf("Scan with Row")
413 func TestRows(t *testing.T) {
414 user1 := User{Name: "RowsUser1", Age: 1, Birthday: parseTime("2000-1-1")}
415 user2 := User{Name: "RowsUser2", Age: 10, Birthday: parseTime("2010-1-1")}
416 user3 := User{Name: "RowsUser3", Age: 20, Birthday: parseTime("2020-1-1")}
417 DB.Save(&user1).Save(&user2).Save(&user3)
419 rows, err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("name, age").Rows()
421 t.Errorf("Not error should happen, got %v", err)
428 rows.Scan(&name, &age)
433 t.Errorf("Should found two records")
437 func TestScanRows(t *testing.T) {
438 user1 := User{Name: "ScanRowsUser1", Age: 1, Birthday: parseTime("2000-1-1")}
439 user2 := User{Name: "ScanRowsUser2", Age: 10, Birthday: parseTime("2010-1-1")}
440 user3 := User{Name: "ScanRowsUser3", Age: 20, Birthday: parseTime("2020-1-1")}
441 DB.Save(&user1).Save(&user2).Save(&user3)
443 rows, err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("name, age").Rows()
445 t.Errorf("Not error should happen, got %v", err)
456 if err := DB.ScanRows(rows, &result); err != nil {
457 t.Errorf("should get no error, but got %v", err)
459 results = append(results, result)
462 if !reflect.DeepEqual(results, []Result{{Name: "ScanRowsUser2", Age: 10}, {Name: "ScanRowsUser3", Age: 20}}) {
463 t.Errorf("Should find expected results")
467 func TestScan(t *testing.T) {
468 user1 := User{Name: "ScanUser1", Age: 1, Birthday: parseTime("2000-1-1")}
469 user2 := User{Name: "ScanUser2", Age: 10, Birthday: parseTime("2010-1-1")}
470 user3 := User{Name: "ScanUser3", Age: 20, Birthday: parseTime("2020-1-1")}
471 DB.Save(&user1).Save(&user2).Save(&user3)
479 DB.Table("users").Select("name, age").Where("name = ?", user3.Name).Scan(&res)
480 if res.Name != user3.Name {
481 t.Errorf("Scan into struct should work")
484 var doubleAgeRes = &result{}
485 if err := DB.Table("users").Select("age + age as age").Where("name = ?", user3.Name).Scan(&doubleAgeRes).Error; err != nil {
486 t.Errorf("Scan to pointer of pointer")
488 if doubleAgeRes.Age != res.Age*2 {
489 t.Errorf("Scan double age as age")
493 DB.Table("users").Select("name, age").Where("name in (?)", []string{user2.Name, user3.Name}).Scan(&ress)
494 if len(ress) != 2 || ress[0].Name != user2.Name || ress[1].Name != user3.Name {
495 t.Errorf("Scan into struct map")
499 func TestRaw(t *testing.T) {
500 user1 := User{Name: "ExecRawSqlUser1", Age: 1, Birthday: parseTime("2000-1-1")}
501 user2 := User{Name: "ExecRawSqlUser2", Age: 10, Birthday: parseTime("2010-1-1")}
502 user3 := User{Name: "ExecRawSqlUser3", Age: 20, Birthday: parseTime("2020-1-1")}
503 DB.Save(&user1).Save(&user2).Save(&user3)
511 DB.Raw("SELECT name, age FROM users WHERE name = ? or name = ?", user2.Name, user3.Name).Scan(&ress)
512 if len(ress) != 2 || ress[0].Name != user2.Name || ress[1].Name != user3.Name {
513 t.Errorf("Raw with scan")
516 rows, _ := DB.Raw("select name, age from users where name = ?", user3.Name).Rows()
522 t.Errorf("Raw with Rows should find one record with name 3")
525 DB.Exec("update users set name=? where name in (?)", "jinzhu", []string{user1.Name, user2.Name, user3.Name})
526 if DB.Where("name in (?)", []string{user1.Name, user2.Name, user3.Name}).First(&User{}).Error != gorm.ErrRecordNotFound {
527 t.Error("Raw sql to update records")
531 func TestGroup(t *testing.T) {
532 rows, err := DB.Select("name").Table("users").Group("name").Rows()
541 t.Errorf("Should not raise any error")
545 func TestJoins(t *testing.T) {
548 CreditCard: CreditCard{Number: "411111111111"},
549 Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}},
554 DB.Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins").Find(&users1)
555 if len(users1) != 2 {
556 t.Errorf("should find two users using left join")
560 DB.Joins("left join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Where("name = ?", "joins").First(&users2)
561 if len(users2) != 1 {
562 t.Errorf("should find one users using left join with conditions")
566 DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.number = ?", "411111111111").Where("name = ?", "joins").First(&users3)
567 if len(users3) != 1 {
568 t.Errorf("should find one users using multiple left join conditions")
572 DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.number = ?", "422222222222").Where("name = ?", "joins").First(&users4)
573 if len(users4) != 0 {
574 t.Errorf("should find no user when searching with unexisting credit card")
578 db5 := DB.Joins("join emails on emails.user_id = users.id AND emails.email = ?", "join1@example.com").Joins("join credit_cards on credit_cards.user_id = users.id AND credit_cards.number = ?", "411111111111").Where(User{Id: 1}).Where(Email{Id: 1}).Not(Email{Id: 10}).First(&users5)
579 if db5.Error != nil {
580 t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s", db5.Error.Error())
584 type JoinedIds struct {
585 UserID int64 `gorm:"column:id"`
586 BillingAddressID int64 `gorm:"column:id"`
587 EmailID int64 `gorm:"column:id"`
590 func TestScanIdenticalColumnNames(t *testing.T) {
593 Email: "joinIds@example.com",
594 BillingAddress: Address{
595 Address1: "One Park Place",
597 Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}},
601 var users []JoinedIds
602 DB.Select("users.id, addresses.id, emails.id").Table("users").
603 Joins("left join addresses on users.billing_address_id = addresses.id").
604 Joins("left join emails on emails.user_id = users.id").
605 Where("name = ?", "joinsIds").Scan(&users)
608 t.Fatal("should find two rows using left join")
611 if user.Id != users[0].UserID {
612 t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[0].UserID)
614 if user.Id != users[1].UserID {
615 t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[1].UserID)
618 if user.BillingAddressID.Int64 != users[0].BillingAddressID {
619 t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID)
621 if user.BillingAddressID.Int64 != users[1].BillingAddressID {
622 t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID)
625 if users[0].EmailID == users[1].EmailID {
626 t.Errorf("Email ids should be unique. Got %d and %d", users[0].EmailID, users[1].EmailID)
629 if int64(user.Emails[0].Id) != users[0].EmailID && int64(user.Emails[1].Id) != users[0].EmailID {
630 t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[0].EmailID)
633 if int64(user.Emails[0].Id) != users[1].EmailID && int64(user.Emails[1].Id) != users[1].EmailID {
634 t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[1].EmailID)
638 func TestJoinsWithSelect(t *testing.T) {
645 Name: "joins_with_select",
646 Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}},
651 DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results)
652 if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" {
653 t.Errorf("Should find all two emails with Join select")
657 func TestHaving(t *testing.T) {
658 rows, err := DB.Select("name, count(*) as total").Table("users").Group("name").Having("name IN (?)", []string{"2", "3"}).Rows()
665 rows.Scan(&name, &total)
667 if name == "2" && total != 1 {
668 t.Errorf("Should have one user having name 2")
670 if name == "3" && total != 2 {
671 t.Errorf("Should have two users having name 3")
675 t.Errorf("Should not raise any error")
679 func TestQueryBuilderSubselectInWhere(t *testing.T) {
680 user := User{Name: "query_expr_select_ruser1", Email: "root@user1.com", Age: 32}
682 user = User{Name: "query_expr_select_ruser2", Email: "nobody@user2.com", Age: 16}
684 user = User{Name: "query_expr_select_ruser3", Email: "root@user3.com", Age: 64}
686 user = User{Name: "query_expr_select_ruser4", Email: "somebody@user3.com", Age: 128}
690 DB.Select("*").Where("name IN (?)", DB.
691 Select("name").Table("users").Where("name LIKE ?", "query_expr_select%").QueryExpr()).Find(&users)
694 t.Errorf("Four users should be found, instead found %d", len(users))
697 DB.Select("*").Where("name LIKE ?", "query_expr_select%").Where("age >= (?)", DB.
698 Select("AVG(age)").Table("users").Where("name LIKE ?", "query_expr_select%").QueryExpr()).Find(&users)
701 t.Errorf("Two users should be found, instead found %d", len(users))
705 func TestQueryBuilderRawQueryWithSubquery(t *testing.T) {
706 user := User{Name: "subquery_test_user1", Age: 10}
708 user = User{Name: "subquery_test_user2", Age: 11}
710 user = User{Name: "subquery_test_user3", Age: 12}
714 err := DB.Raw("select count(*) from (?) tmp",
717 Where("age >= ? and name in (?)", 10, []string{"subquery_test_user1", "subquery_test_user2"}).
720 ).Count(&count).Error
723 t.Errorf("Expected to get no errors, but got %v", err)
726 t.Errorf("Row count must be 2, instead got %d", count)
729 err = DB.Raw("select count(*) from (?) tmp",
732 Where("name LIKE ?", "subquery_test%").
733 Not("age <= ?", 10).Not("name in (?)", []string{"subquery_test_user1", "subquery_test_user2"}).
736 ).Count(&count).Error
739 t.Errorf("Expected to get no errors, but got %v", err)
742 t.Errorf("Row count must be 1, instead got %d", count)
746 func TestQueryBuilderSubselectInHaving(t *testing.T) {
747 user := User{Name: "query_expr_having_ruser1", Email: "root@user1.com", Age: 64}
749 user = User{Name: "query_expr_having_ruser2", Email: "root@user2.com", Age: 128}
751 user = User{Name: "query_expr_having_ruser3", Email: "root@user1.com", Age: 64}
753 user = User{Name: "query_expr_having_ruser4", Email: "root@user2.com", Age: 128}
757 DB.Select("AVG(age) as avgage").Where("name LIKE ?", "query_expr_having_%").Group("email").Having("AVG(age) > (?)", DB.
758 Select("AVG(age)").Where("name LIKE ?", "query_expr_having_%").Table("users").QueryExpr()).Find(&users)
761 t.Errorf("Two user group should be found, instead found %d", len(users))
765 func DialectHasTzSupport() bool {
766 // NB: mssql and FoundationDB do not support time zones.
767 if dialect := os.Getenv("GORM_DIALECT"); dialect == "foundation" {
773 func TestTimeWithZone(t *testing.T) {
774 var format = "2006-01-02 15:04:05 -0700"
775 var times []time.Time
776 GMT8, _ := time.LoadLocation("Asia/Shanghai")
777 times = append(times, time.Date(2013, 02, 19, 1, 51, 49, 123456789, GMT8))
778 times = append(times, time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC))
780 for index, vtime := range times {
781 name := "time_with_zone_" + strconv.Itoa(index)
782 user := User{Name: name, Birthday: &vtime}
784 if !DialectHasTzSupport() {
785 // If our driver dialect doesn't support TZ's, just use UTC for everything here.
786 utcBirthday := user.Birthday.UTC()
787 user.Birthday = &utcBirthday
791 expectedBirthday := "2013-02-18 17:51:49 +0000"
792 foundBirthday := user.Birthday.UTC().Format(format)
793 if foundBirthday != expectedBirthday {
794 t.Errorf("User's birthday should not be changed after save for name=%s, expected bday=%+v but actual value=%+v", name, expectedBirthday, foundBirthday)
797 var findUser, findUser2, findUser3 User
798 DB.First(&findUser, "name = ?", name)
799 foundBirthday = findUser.Birthday.UTC().Format(format)
800 if foundBirthday != expectedBirthday {
801 t.Errorf("User's birthday should not be changed after find for name=%s, expected bday=%+v but actual value=%+v", name, expectedBirthday, foundBirthday)
804 if DB.Where("id = ? AND birthday >= ?", findUser.Id, user.Birthday.Add(-time.Minute)).First(&findUser2).RecordNotFound() {
805 t.Errorf("User should be found")
808 if !DB.Where("id = ? AND birthday >= ?", findUser.Id, user.Birthday.Add(time.Minute)).First(&findUser3).RecordNotFound() {
809 t.Errorf("User should not be found")
814 func TestHstore(t *testing.T) {
815 type Details struct {
820 if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" {
824 if err := DB.Exec("CREATE EXTENSION IF NOT EXISTS hstore").Error; err != nil {
825 fmt.Println("\033[31mHINT: Must be superuser to create hstore extension (ALTER USER gorm WITH SUPERUSER;)\033[0m")
826 panic(fmt.Sprintf("No error should happen when create hstore extension, but got %+v", err))
829 DB.Exec("drop table details")
831 if err := DB.CreateTable(&Details{}).Error; err != nil {
832 panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
835 bankAccountId, phoneNumber, opinion := "123456", "14151321232", "sharkbait"
836 bulk := map[string]*string{
837 "bankAccountId": &bankAccountId,
838 "phoneNumber": &phoneNumber,
841 d := Details{Bulk: bulk}
845 if err := DB.First(&d2).Error; err != nil {
846 t.Errorf("Got error when tried to fetch details: %+v", err)
849 for k := range bulk {
850 if r, ok := d2.Bulk[k]; ok {
851 if res, _ := bulk[k]; *res != *r {
852 t.Errorf("Details should be equal")
855 t.Errorf("Details should be existed")
860 func TestSetAndGet(t *testing.T) {
861 if value, ok := DB.Set("hello", "world").Get("hello"); !ok {
862 t.Errorf("Should be able to get setting after set")
864 if value.(string) != "world" {
865 t.Errorf("Setted value should not be changed")
869 if _, ok := DB.Get("non_existing"); ok {
870 t.Errorf("Get non existing key should return error")
874 func TestCompatibilityMode(t *testing.T) {
875 DB, _ := gorm.Open("testdb", "")
876 testdb.SetQueryFunc(func(query string) (driver.Rows, error) {
877 columns := []string{"id", "name", "age"}
883 return testdb.RowsFromCSVString(columns, result), nil
888 if (users[0].Name != "Tim") || len(users) != 3 {
889 t.Errorf("Unexcepted result returned")
893 func TestOpenExistingDB(t *testing.T) {
894 DB.Save(&User{Name: "jnfeinstein"})
895 dialect := os.Getenv("GORM_DIALECT")
897 db, err := gorm.Open(dialect, DB.DB())
899 t.Errorf("Should have wrapped the existing DB connection")
903 if db.Where("name = ?", "jnfeinstein").First(&user).Error == gorm.ErrRecordNotFound {
904 t.Errorf("Should have found existing record")
908 func TestDdlErrors(t *testing.T) {
911 if err = DB.Close(); err != nil {
912 t.Errorf("Closing DDL test db connection err=%s", err)
915 // Reopen DB connection.
916 if DB, err = OpenTestConnection(); err != nil {
917 t.Fatalf("Failed re-opening db connection: %s", err)
921 if err := DB.Find(&User{}).Error; err == nil {
922 t.Errorf("Expected operation on closed db to produce an error, but err was nil")
926 func TestOpenWithOneParameter(t *testing.T) {
927 db, err := gorm.Open("dialect")
929 t.Error("Open with one parameter returned non nil for db")
932 t.Error("Open with one parameter returned err as nil")
936 func TestSaveAssociations(t *testing.T) {
938 deltaAddressCount := 0
939 if err := db.Model(&Address{}).Count(&deltaAddressCount).Error; err != nil {
940 t.Errorf("failed to fetch address count")
944 placeAddress := &Address{
945 Address1: "somewhere on earth",
947 ownerAddress1 := &Address{
948 Address1: "near place address",
950 ownerAddress2 := &Address{
951 Address1: "address2",
953 db.Create(placeAddress)
955 addressCountShouldBe := func(t *testing.T, expectedCount int) {
958 err := db.Model(&Address{}).Count(&countFromDB).Error
960 t.Error("failed to fetch address count")
962 if countFromDB != expectedCount {
963 t.Errorf("address count mismatch: %d", countFromDB)
966 addressCountShouldBe(t, deltaAddressCount+1)
968 // owner address should be created, place address should be reused
970 PlaceAddressID: placeAddress.ID,
971 PlaceAddress: placeAddress,
972 OwnerAddress: ownerAddress1,
974 err := db.Create(place1).Error
976 t.Errorf("failed to store place: %s", err.Error())
978 addressCountShouldBe(t, deltaAddressCount+2)
980 // owner address should be created again, place address should be reused
982 PlaceAddressID: placeAddress.ID,
983 PlaceAddress: &Address{
985 Address1: "address1",
987 OwnerAddress: ownerAddress2,
990 err = db.Create(place2).Error
992 t.Errorf("failed to store place: %s", err.Error())
994 addressCountShouldBe(t, deltaAddressCount+3)
997 db.Model(&Place{}).Where(&Place{
998 PlaceAddressID: placeAddress.ID,
999 OwnerAddressID: ownerAddress1.ID,
1002 t.Errorf("only one instance of (%d, %d) should be available, found: %d",
1003 placeAddress.ID, ownerAddress1.ID, count)
1006 db.Model(&Place{}).Where(&Place{
1007 PlaceAddressID: placeAddress.ID,
1008 OwnerAddressID: ownerAddress2.ID,
1011 t.Errorf("only one instance of (%d, %d) should be available, found: %d",
1012 placeAddress.ID, ownerAddress2.ID, count)
1015 db.Model(&Place{}).Where(&Place{
1016 PlaceAddressID: placeAddress.ID,
1019 t.Errorf("two instances of (%d) should be available, found: %d",
1020 placeAddress.ID, count)
1024 func TestBlockGlobalUpdate(t *testing.T) {
1026 db.Create(&Toy{Name: "Stuffed Animal", OwnerType: "Nobody"})
1028 err := db.Model(&Toy{}).Update("OwnerType", "Human").Error
1030 t.Error("Unexpected error on global update")
1033 err = db.Delete(&Toy{}).Error
1035 t.Error("Unexpected error on global delete")
1038 db.BlockGlobalUpdate(true)
1040 db.Create(&Toy{Name: "Stuffed Animal", OwnerType: "Nobody"})
1042 err = db.Model(&Toy{}).Update("OwnerType", "Human").Error
1044 t.Error("Expected error on global update")
1047 err = db.Model(&Toy{}).Where(&Toy{OwnerType: "Martian"}).Update("OwnerType", "Astronaut").Error
1049 t.Error("Unxpected error on conditional update")
1052 err = db.Delete(&Toy{}).Error
1054 t.Error("Expected error on global delete")
1056 err = db.Where(&Toy{OwnerType: "Martian"}).Delete(&Toy{}).Error
1058 t.Error("Unexpected error on conditional delete")
1062 func BenchmarkGorm(b *testing.B) {
1064 for x := 0; x < b.N; x++ {
1065 e := strconv.Itoa(x) + "benchmark@example.org"
1067 email := EmailWithIdx{Email: e, UserAgent: "pc", RegisteredAt: &now}
1071 DB.First(&EmailWithIdx{}, "email = ?", e)
1073 DB.Model(&email).UpdateColumn("email", "new-"+e)
1079 func BenchmarkRawSql(b *testing.B) {
1080 DB, _ := sql.Open("postgres", "user=gorm DB.ame=gorm sslmode=disable")
1081 DB.SetMaxIdleConns(10)
1082 insertSql := "INSERT INTO emails (user_id,email,user_agent,registered_at,created_at,updated_at) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id"
1083 querySql := "SELECT * FROM emails WHERE email = $1 ORDER BY id LIMIT 1"
1084 updateSql := "UPDATE emails SET email = $1, updated_at = $2 WHERE id = $3"
1085 deleteSql := "DELETE FROM orders WHERE id = $1"
1088 for x := 0; x < b.N; x++ {
1090 e := strconv.Itoa(x) + "benchmark@example.org"
1092 email := EmailWithIdx{Email: e, UserAgent: "pc", RegisteredAt: &now}
1094 DB.QueryRow(insertSql, email.UserId, email.Email, email.UserAgent, email.RegisteredAt, time.Now(), time.Now()).Scan(&id)
1096 rows, _ := DB.Query(querySql, email.Email)
1099 DB.Exec(updateSql, "new-"+e, time.Now(), id)
1101 DB.Exec(deleteSql, id)
1105 func parseTime(str string) *time.Time {
1106 t := now.New(time.Now().UTC()).MustParse(str)