mirror of https://github.com/go-gorm/gorm.git
Refact tests
This commit is contained in:
parent
b929a082d7
commit
468e54f0ee
|
@ -0,0 +1,177 @@
|
||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Product) BeforeCreate() (err error) {
|
||||||
|
if s.Code == "Invalid" {
|
||||||
|
err = errors.New("invalid product")
|
||||||
|
}
|
||||||
|
s.BeforeCreateCallTimes = s.BeforeCreateCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) BeforeUpdate() (err error) {
|
||||||
|
if s.Code == "dont_update" {
|
||||||
|
err = errors.New("can't update")
|
||||||
|
}
|
||||||
|
s.BeforeUpdateCallTimes = s.BeforeUpdateCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) BeforeSave() (err error) {
|
||||||
|
if s.Code == "dont_save" {
|
||||||
|
err = errors.New("can't save")
|
||||||
|
}
|
||||||
|
s.BeforeSaveCallTimes = s.BeforeSaveCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) AfterFind() {
|
||||||
|
s.AfterFindCallTimes = s.AfterFindCallTimes + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) AfterCreate(db *gorm.DB) {
|
||||||
|
db.Model(s).UpdateColumn(Product{AfterCreateCallTimes: s.AfterCreateCallTimes + 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) AfterUpdate() {
|
||||||
|
s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) AfterSave() (err error) {
|
||||||
|
if s.Code == "after_save_error" {
|
||||||
|
err = errors.New("can't save")
|
||||||
|
}
|
||||||
|
s.AfterSaveCallTimes = s.AfterSaveCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) BeforeDelete() (err error) {
|
||||||
|
if s.Code == "dont_delete" {
|
||||||
|
err = errors.New("can't delete")
|
||||||
|
}
|
||||||
|
s.BeforeDeleteCallTimes = s.BeforeDeleteCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) AfterDelete() (err error) {
|
||||||
|
if s.Code == "after_delete_error" {
|
||||||
|
err = errors.New("can't delete")
|
||||||
|
}
|
||||||
|
s.AfterDeleteCallTimes = s.AfterDeleteCallTimes + 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Product) GetCallTimes() []int64 {
|
||||||
|
return []int64{s.BeforeCreateCallTimes, s.BeforeSaveCallTimes, s.BeforeUpdateCallTimes, s.AfterCreateCallTimes, s.AfterSaveCallTimes, s.AfterUpdateCallTimes, s.BeforeDeleteCallTimes, s.AfterDeleteCallTimes, s.AfterFindCallTimes}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunCallbacks(t *testing.T) {
|
||||||
|
p := Product{Code: "unique_code", Price: 100}
|
||||||
|
db.Save(&p)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 1, 0, 0, 0, 0}) {
|
||||||
|
t.Errorf("Callbacks should be invoked successfully, %v", p.GetCallTimes())
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where("Code = ?", "unique_code").First(&p)
|
||||||
|
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 1, 0, 1, 0, 0, 0, 0, 1}) {
|
||||||
|
t.Errorf("After callbacks values are not saved, %v", p.GetCallTimes())
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Price = 200
|
||||||
|
db.Save(&p)
|
||||||
|
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 1, 1, 0, 0, 1}) {
|
||||||
|
t.Errorf("After update callbacks should be invoked successfully, %v", p.GetCallTimes())
|
||||||
|
}
|
||||||
|
|
||||||
|
var products []Product
|
||||||
|
db.Find(&products, "code = ?", "unique_code")
|
||||||
|
if products[0].AfterFindCallTimes != 2 {
|
||||||
|
t.Errorf("AfterFind callbacks should work with slice")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where("Code = ?", "unique_code").First(&p)
|
||||||
|
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 0, 0, 2}) {
|
||||||
|
t.Errorf("After update callbacks values are not saved, %v", p.GetCallTimes())
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Delete(&p)
|
||||||
|
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 1, 1, 0, 0, 1, 1, 2}) {
|
||||||
|
t.Errorf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes())
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("Code = ?", "unique_code").First(&p).Error == nil {
|
||||||
|
t.Errorf("Can't find a deleted record")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCallbacksWithErrors(t *testing.T) {
|
||||||
|
p := Product{Code: "Invalid", Price: 100}
|
||||||
|
if db.Save(&p).Error == nil {
|
||||||
|
t.Errorf("An error from before create callbacks happened when create with invalid value")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("code = ?", "Invalid").First(&Product{}).Error == nil {
|
||||||
|
t.Errorf("Should not save record that have errors")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Save(&Product{Code: "dont_save", Price: 100}).Error == nil {
|
||||||
|
t.Errorf("An error from after create callbacks happened when create with invalid value")
|
||||||
|
}
|
||||||
|
|
||||||
|
p2 := Product{Code: "update_callback", Price: 100}
|
||||||
|
db.Save(&p2)
|
||||||
|
|
||||||
|
p2.Code = "dont_update"
|
||||||
|
if db.Save(&p2).Error == nil {
|
||||||
|
t.Errorf("An error from before update callbacks happened when update with invalid value")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("code = ?", "update_callback").First(&Product{}).Error != nil {
|
||||||
|
t.Errorf("Record Should not be updated due to errors happened in before update callback")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("code = ?", "dont_update").First(&Product{}).Error == nil {
|
||||||
|
t.Errorf("Record Should not be updated due to errors happened in before update callback")
|
||||||
|
}
|
||||||
|
|
||||||
|
p2.Code = "dont_save"
|
||||||
|
if db.Save(&p2).Error == nil {
|
||||||
|
t.Errorf("An error from before save callbacks happened when update with invalid value")
|
||||||
|
}
|
||||||
|
|
||||||
|
p3 := Product{Code: "dont_delete", Price: 100}
|
||||||
|
db.Save(&p3)
|
||||||
|
if db.Delete(&p3).Error == nil {
|
||||||
|
t.Errorf("An error from before delete callbacks happened when delete")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("Code = ?", "dont_delete").First(&p3).Error != nil {
|
||||||
|
t.Errorf("An error from before delete callbacks happened")
|
||||||
|
}
|
||||||
|
|
||||||
|
p4 := Product{Code: "after_save_error", Price: 100}
|
||||||
|
db.Save(&p4)
|
||||||
|
if err := db.First(&Product{}, "code = ?", "after_save_error").Error; err == nil {
|
||||||
|
t.Errorf("Record should be reverted if get an error in after save callback")
|
||||||
|
}
|
||||||
|
|
||||||
|
p5 := Product{Code: "after_delete_error", Price: 100}
|
||||||
|
db.Save(&p5)
|
||||||
|
if err := db.First(&Product{}, "code = ?", "after_delete_error").Error; err != nil {
|
||||||
|
t.Errorf("Record should be found")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Delete(&p5)
|
||||||
|
if err := db.First(&Product{}, "code = ?", "after_delete_error").Error; err != nil {
|
||||||
|
t.Errorf("Record shouldn't be deleted because of an error happened in after delete callback")
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreate(t *testing.T) {
|
func TestCreate(t *testing.T) {
|
||||||
user := User{Name: "1", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}}
|
float := 35.03554004971999
|
||||||
|
user := User{Name: "CreateUser", Age: 18, Birthday: time.Now(), UserNum: Num(111), PasswordHash: []byte{'f', 'a', 'k', '4'}, Latitude: float}
|
||||||
|
|
||||||
if !db.NewRecord(user) || !db.NewRecord(&user) {
|
if !db.NewRecord(user) || !db.NewRecord(&user) {
|
||||||
t.Error("User should be new record before create")
|
t.Error("User should be new record before create")
|
||||||
|
@ -35,4 +36,49 @@ func TestCreate(t *testing.T) {
|
||||||
if newUser.UserNum != Num(111) {
|
if newUser.UserNum != Num(111) {
|
||||||
t.Errorf("User's UserNum should be saved (custom type)")
|
t.Errorf("User's UserNum should be saved (custom type)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if newUser.Latitude != float {
|
||||||
|
t.Errorf("Float64 should not be changed after save")
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.CreatedAt.IsZero() {
|
||||||
|
t.Errorf("Should have created_at after create")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newUser.CreatedAt.IsZero() {
|
||||||
|
t.Errorf("Should have created_at after create")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Model(user).Update("name", "create_user_new_name")
|
||||||
|
db.First(&user, user.Id)
|
||||||
|
if user.CreatedAt != newUser.CreatedAt {
|
||||||
|
t.Errorf("CreatedAt should not be changed after update")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnonymousScanner(t *testing.T) {
|
||||||
|
user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
|
||||||
|
db.Save(&user)
|
||||||
|
|
||||||
|
var user2 User
|
||||||
|
db.First(&user2, "name = ?", "anonymous_scanner")
|
||||||
|
if user2.Role.Name != "admin" {
|
||||||
|
t.Errorf("Should be able to get anonymous scanner")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !user2.IsAdmin() {
|
||||||
|
t.Errorf("Should be able to get anonymous scanner")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnonymousField(t *testing.T) {
|
||||||
|
user := User{Name: "anonymous_field", Company: Company{Name: "company"}}
|
||||||
|
db.Save(&user)
|
||||||
|
|
||||||
|
var user2 User
|
||||||
|
db.First(&user2, "name = ?", "anonymous_field")
|
||||||
|
db.Model(&user2).Related(&user2.Company)
|
||||||
|
if user2.Company.Name != "company" {
|
||||||
|
t.Errorf("Should be able to get anonymous field")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
1895
main_test.go
1895
main_test.go
File diff suppressed because it is too large
Load Diff
|
@ -1,12 +0,0 @@
|
||||||
package gorm_test
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestQueryManyToManyWithRelated(t *testing.T) {
|
|
||||||
db.Model(&User{}).Related(&[]Language{}, "Languages")
|
|
||||||
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
|
||||||
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
|
||||||
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
|
||||||
// db.Model(&User{}).Many2Many("Languages").Remove(&Language{})
|
|
||||||
// db.Model(&User{}).Many2Many("Languages").Replace(&[]Language{})
|
|
||||||
}
|
|
|
@ -3,6 +3,7 @@ package gorm_test
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runMigration() {
|
func runMigration() {
|
||||||
|
@ -89,3 +90,32 @@ func TestIndexes(t *testing.T) {
|
||||||
t.Errorf("Should be able to create duplicated emails after remove unique index")
|
t.Errorf("Should be able to create duplicated emails after remove unique index")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BigEmail struct {
|
||||||
|
Id int64
|
||||||
|
UserId int64
|
||||||
|
Email string
|
||||||
|
UserAgent string
|
||||||
|
RegisteredAt time.Time
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b BigEmail) TableName() string {
|
||||||
|
return "emails"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoMigration(t *testing.T) {
|
||||||
|
db.AutoMigrate(Address{})
|
||||||
|
if err := db.Table("emails").AutoMigrate(BigEmail{}).Error; err != nil {
|
||||||
|
t.Errorf("Auto Migrate should not raise any error")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()})
|
||||||
|
|
||||||
|
var bigemail BigEmail
|
||||||
|
db.First(&bigemail, "user_agent = ?", "pc")
|
||||||
|
if bigemail.Email != "jinzhu@example.org" || bigemail.UserAgent != "pc" || bigemail.RegisteredAt.IsZero() {
|
||||||
|
t.Error("Big Emails should be saved and fetched correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
467
query_test.go
467
query_test.go
|
@ -1,6 +1,13 @@
|
||||||
package gorm_test
|
package gorm_test
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jinzhu/now"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func TestFirstAndLast(t *testing.T) {
|
func TestFirstAndLast(t *testing.T) {
|
||||||
db.Save(&User{Name: "user1", Emails: []Email{{Email: "user1@example.com"}}})
|
db.Save(&User{Name: "user1", Emails: []Email{{Email: "user1@example.com"}}})
|
||||||
|
@ -55,3 +62,461 @@ func TestFindAsSliceOfPointers(t *testing.T) {
|
||||||
t.Errorf("Find slice of pointers")
|
t.Errorf("Find slice of pointers")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
scopedb := db.Where("name LIKE ?", "%PlainSqlUser%")
|
||||||
|
|
||||||
|
if db.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
|
||||||
|
t.Errorf("Search with plain SQL")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("name LIKE ?", "%"+user1.Name+"%").First(&User{}).RecordNotFound() {
|
||||||
|
t.Errorf("Search with plan SQL (regexp)")
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []User
|
||||||
|
db.Find(&users, "name LIKE ? and age > ?", "%PlainSqlUser%", 1)
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Errorf("Should found 2 users that age > 1, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Where("name LIKE ?", "%PlainSqlUser%").Where("age >= ?", 1).Find(&users)
|
||||||
|
if len(users) != 3 {
|
||||||
|
t.Errorf("Should found 3 users that age >= 1, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
scopedb.Where("age <> ?", 20).Find(&users)
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Errorf("Should found 2 users age != 20, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
scopedb.Where("birthday > ?", now.MustParse("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))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
scopedb.Where("birthday > ?", "2002-10-10").Find(&users)
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Errorf("Should found 2 users's birthday >= 2002-10-10, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
scopedb.Where("birthday >= ?", "2010-1-1").Where("birthday < ?", "2020-1-1").Find(&users)
|
||||||
|
if len(users) != 1 {
|
||||||
|
t.Errorf("Should found 1 users's birthday < 2020-1-1 and >= 2010-1-1, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Where("name in (?)", []string{user1.Name, user2.Name}).Find(&users)
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Errorf("Should found 2 users, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Where("id in (?)", []int64{user1.Id, user2.Id, user3.Id}).Find(&users)
|
||||||
|
if len(users) != 3 {
|
||||||
|
t.Errorf("Should found 3 users, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Where("id in (?)", user1.Id).Find(&users)
|
||||||
|
if len(users) != 1 {
|
||||||
|
t.Errorf("Should found 1 users, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !db.Where("name = ?", "none existing").Find(&[]User{}).RecordNotFound() {
|
||||||
|
t.Errorf("Should get RecordNotFound error when looking for none existing records")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
|
||||||
|
if db.Where(user1.Id).First(&User{}).RecordNotFound() {
|
||||||
|
t.Errorf("Search with primary key")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.First(&User{}, user1.Id).RecordNotFound() {
|
||||||
|
t.Errorf("Search with primary key as inline condition")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.First(&User{}, fmt.Sprintf("%v", user1.Id)).RecordNotFound() {
|
||||||
|
t.Errorf("Search with primary key as inline condition")
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []User
|
||||||
|
db.Where([]int64{user1.Id, user2.Id, user3.Id}).Find(&users)
|
||||||
|
if len(users) != 3 {
|
||||||
|
t.Errorf("Should found 3 users when search with primary keys, but got %v", len(users))
|
||||||
|
}
|
||||||
|
|
||||||
|
var user User
|
||||||
|
db.First(&user, &User{Name: user1.Name})
|
||||||
|
if user.Id == 0 || user.Name != user1.Name {
|
||||||
|
t.Errorf("Search first record with inline pointer of struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.First(&user, User{Name: user1.Name})
|
||||||
|
if user.Id == 0 || user.Name != user.Name {
|
||||||
|
t.Errorf("Search first record with inline struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: user1.Name}).First(&user)
|
||||||
|
if user.Id == 0 || user.Name != user1.Name {
|
||||||
|
t.Errorf("Search first record with where struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Find(&users, &User{Name: user2.Name})
|
||||||
|
if len(users) != 1 {
|
||||||
|
t.Errorf("Search all records with inline struct")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchWithMap(t *testing.T) {
|
||||||
|
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")}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
|
||||||
|
var user User
|
||||||
|
db.First(&user, map[string]interface{}{"name": user1.Name})
|
||||||
|
if user.Id == 0 || user.Name != user1.Name {
|
||||||
|
t.Errorf("Search first record with inline map")
|
||||||
|
}
|
||||||
|
|
||||||
|
user = User{}
|
||||||
|
db.Where(map[string]interface{}{"name": user2.Name}).First(&user)
|
||||||
|
if user.Id == 0 || user.Name != user2.Name {
|
||||||
|
t.Errorf("Search first record with where map")
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []User
|
||||||
|
db.Where(map[string]interface{}{"name": user3.Name}).Find(&users)
|
||||||
|
if len(users) != 1 {
|
||||||
|
t.Errorf("Search all records with inline map")
|
||||||
|
}
|
||||||
|
|
||||||
|
users = []User{}
|
||||||
|
db.Find(&users, map[string]interface{}{"name": user3.Name})
|
||||||
|
if len(users) != 1 {
|
||||||
|
t.Errorf("Search all records with inline map")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelect(t *testing.T) {
|
||||||
|
user1 := User{Name: "SelectUser1"}
|
||||||
|
db.Save(&user1)
|
||||||
|
|
||||||
|
var user User
|
||||||
|
db.Where("name = ?", user1.Name).Select("name").Find(&user)
|
||||||
|
if user.Id != 0 {
|
||||||
|
t.Errorf("Should not have ID because only selected name, %+v", user.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.Name != user1.Name {
|
||||||
|
t.Errorf("Should have user Name when selected it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOrderAndPluck(t *testing.T) {
|
||||||
|
user1 := User{Name: "OrderPluckUser1", Age: 1}
|
||||||
|
user2 := User{Name: "OrderPluckUser2", Age: 10}
|
||||||
|
user3 := User{Name: "OrderPluckUser3", Age: 20}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
scopedb := db.Model(&User{}).Where("name like ?", "%OrderPluckUser%")
|
||||||
|
|
||||||
|
var ages []int64
|
||||||
|
scopedb.Order("age desc").Pluck("age", &ages)
|
||||||
|
if ages[0] != 20 {
|
||||||
|
t.Errorf("The first age should be 20 when order with age desc")
|
||||||
|
}
|
||||||
|
|
||||||
|
var ages1, ages2 []int64
|
||||||
|
scopedb.Order("age desc").Pluck("age", &ages1).Order("age").Pluck("age", &ages2)
|
||||||
|
if !reflect.DeepEqual(ages1, ages2) {
|
||||||
|
t.Errorf("The first order is the primary order")
|
||||||
|
}
|
||||||
|
|
||||||
|
var ages3, ages4 []int64
|
||||||
|
scopedb.Model(&User{}).Order("age desc").Pluck("age", &ages3).Order("age", true).Pluck("age", &ages4)
|
||||||
|
if reflect.DeepEqual(ages3, ages4) {
|
||||||
|
t.Errorf("Reorder should work")
|
||||||
|
}
|
||||||
|
|
||||||
|
var names []string
|
||||||
|
var ages5 []int64
|
||||||
|
scopedb.Model(User{}).Order("name").Order("age desc").Pluck("age", &ages5).Pluck("name", &names)
|
||||||
|
if !(names[0] == user1.Name && names[1] == user2.Name && names[2] == user3.Name && ages5[2] == 20) {
|
||||||
|
t.Errorf("Order with multiple orders")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Model(User{}).Select("name, age").Find(&[]User{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLimit(t *testing.T) {
|
||||||
|
user1 := User{Name: "LimitUser1", Age: 1}
|
||||||
|
user2 := User{Name: "LimitUser2", Age: 10}
|
||||||
|
user3 := User{Name: "LimitUser3", Age: 20}
|
||||||
|
user4 := User{Name: "LimitUser4", Age: 10}
|
||||||
|
user5 := User{Name: "LimitUser5", Age: 20}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3).Save(&user4).Save(&user5)
|
||||||
|
|
||||||
|
var users1, users2, users3 []User
|
||||||
|
db.Order("age desc").Limit(3).Find(&users1).Limit(5).Find(&users2).Limit(-1).Find(&users3)
|
||||||
|
|
||||||
|
if len(users1) != 3 || len(users2) != 5 || len(users3) <= 5 {
|
||||||
|
t.Errorf("Limit should works")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOffset(t *testing.T) {
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
db.Save(&User{Name: fmt.Sprintf("OffsetUser%v", i)})
|
||||||
|
}
|
||||||
|
var users1, users2, users3, users4 []User
|
||||||
|
db.Limit(100).Order("age desc").Find(&users1).Offset(3).Find(&users2).Offset(5).Find(&users3).Offset(-1).Find(&users4)
|
||||||
|
|
||||||
|
if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {
|
||||||
|
t.Errorf("Offset should work")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOr(t *testing.T) {
|
||||||
|
user1 := User{Name: "OrUser1", Age: 1}
|
||||||
|
user2 := User{Name: "OrUser2", Age: 10}
|
||||||
|
user3 := User{Name: "OrUser3", Age: 20}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
|
||||||
|
var users []User
|
||||||
|
db.Where("name = ?", user1.Name).Or("name = ?", user2.Name).Find(&users)
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Errorf("Find users with or")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCount(t *testing.T) {
|
||||||
|
user1 := User{Name: "CountUser1", Age: 1}
|
||||||
|
user2 := User{Name: "CountUser2", Age: 10}
|
||||||
|
user3 := User{Name: "CountUser3", Age: 20}
|
||||||
|
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
var count, count1, count2 int64
|
||||||
|
var users []User
|
||||||
|
|
||||||
|
if err := db.Where("name = ?", user1.Name).Or("name = ?", user3.Name).Find(&users).Count(&count).Error; err != nil {
|
||||||
|
t.Errorf(fmt.Sprintf("Count should work, but got err %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if count != int64(len(users)) {
|
||||||
|
t.Errorf("Count() method should get correct value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Model(&User{}).Where("name = ?", user1.Name).Count(&count1).Or("name in (?)", []string{user2.Name, user3.Name}).Count(&count2)
|
||||||
|
if count1 != 1 || count2 != 3 {
|
||||||
|
t.Errorf("Multiple count in chain")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNot(t *testing.T) {
|
||||||
|
var users1, users2, users3, users4, users5, users6, users7, users8 []User
|
||||||
|
db.Find(&users1)
|
||||||
|
|
||||||
|
db.Not(users1[0].Id).Find(&users2)
|
||||||
|
|
||||||
|
if len(users1)-len(users2) != 1 {
|
||||||
|
t.Errorf("Should ignore the first users with Not")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Not([]int{}).Find(&users3)
|
||||||
|
if len(users1)-len(users3) != 0 {
|
||||||
|
t.Errorf("Should find all users with a blank condition")
|
||||||
|
}
|
||||||
|
|
||||||
|
var name3Count int64
|
||||||
|
db.Table("users").Where("name = ?", "3").Count(&name3Count)
|
||||||
|
db.Not("name", "3").Find(&users4)
|
||||||
|
if len(users1)-len(users4) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
users4 = []User{}
|
||||||
|
db.Not("name = ?", "3").Find(&users4)
|
||||||
|
if len(users1)-len(users4) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
users4 = []User{}
|
||||||
|
db.Not("name <> ?", "3").Find(&users4)
|
||||||
|
if len(users4) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Not(User{Name: "3"}).Find(&users5)
|
||||||
|
|
||||||
|
if len(users1)-len(users5) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Not(map[string]interface{}{"name": "3"}).Find(&users6)
|
||||||
|
if len(users1)-len(users6) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Not("name", []string{"3"}).Find(&users7)
|
||||||
|
if len(users1)-len(users7) != int(name3Count) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
|
||||||
|
var name2Count int64
|
||||||
|
db.Table("users").Where("name = ?", "2").Count(&name2Count)
|
||||||
|
db.Not("name", []string{"3", "2"}).Find(&users8)
|
||||||
|
if len(users1)-len(users8) != (int(name3Count) + int(name2Count)) {
|
||||||
|
t.Errorf("Should find all users's name not equal 3")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFillSmallerStruct(t *testing.T) {
|
||||||
|
user1 := User{Name: "SmallerUser", Age: 100}
|
||||||
|
db.Save(&user1)
|
||||||
|
type SimpleUser struct {
|
||||||
|
Name string
|
||||||
|
Id int64
|
||||||
|
UpdatedAt time.Time
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var simpleUser SimpleUser
|
||||||
|
db.Table("users").Where("name = ?", user1.Name).First(&simpleUser)
|
||||||
|
|
||||||
|
if simpleUser.Id == 0 || simpleUser.Name == "" {
|
||||||
|
t.Errorf("Should fill data correctly into smaller struct")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindOrInitialize(t *testing.T) {
|
||||||
|
var user1, user2, user3, user4, user5, user6 User
|
||||||
|
db.Where(&User{Name: "find or init", Age: 33}).FirstOrInit(&user1)
|
||||||
|
if user1.Name != "find or init" || user1.Id != 0 || user1.Age != 33 {
|
||||||
|
t.Errorf("user should be initialized with search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(User{Name: "find or init", Age: 33}).FirstOrInit(&user2)
|
||||||
|
if user2.Name != "find or init" || user2.Id != 0 || user2.Age != 33 {
|
||||||
|
t.Errorf("user should be initialized with search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.FirstOrInit(&user3, map[string]interface{}{"name": "find or init 2"})
|
||||||
|
if user3.Name != "find or init 2" || user3.Id != 0 {
|
||||||
|
t.Errorf("user should be initialized with inline search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or init"}).Attrs(User{Age: 44}).FirstOrInit(&user4)
|
||||||
|
if user4.Name != "find or init" || user4.Id != 0 || user4.Age != 44 {
|
||||||
|
t.Errorf("user should be initialized with search value and attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or init"}).Assign("age", 44).FirstOrInit(&user4)
|
||||||
|
if user4.Name != "find or init" || user4.Id != 0 || user4.Age != 44 {
|
||||||
|
t.Errorf("user should be initialized with search value and assign attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Save(&User{Name: "find or init", Age: 33})
|
||||||
|
db.Where(&User{Name: "find or init"}).Attrs("age", 44).FirstOrInit(&user5)
|
||||||
|
if user5.Name != "find or init" || user5.Id == 0 || user5.Age != 33 {
|
||||||
|
t.Errorf("user should be found and not initialized by Attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or init", Age: 33}).FirstOrInit(&user6)
|
||||||
|
if user6.Name != "find or init" || user6.Id == 0 || user6.Age != 33 {
|
||||||
|
t.Errorf("user should be found with FirstOrInit")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or init"}).Assign(User{Age: 44}).FirstOrInit(&user6)
|
||||||
|
if user6.Name != "find or init" || user6.Id == 0 || user6.Age != 44 {
|
||||||
|
t.Errorf("user should be found and updated with assigned attrs")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindOrCreate(t *testing.T) {
|
||||||
|
var user1, user2, user3, user4, user5, user6, user7, user8 User
|
||||||
|
db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1)
|
||||||
|
if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 {
|
||||||
|
t.Errorf("user should be created with search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user2)
|
||||||
|
if user1.Id != user2.Id || user2.Name != "find or create" || user2.Id == 0 || user2.Age != 33 {
|
||||||
|
t.Errorf("user should be created with search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.FirstOrCreate(&user3, map[string]interface{}{"name": "find or create 2"})
|
||||||
|
if user3.Name != "find or create 2" || user3.Id == 0 {
|
||||||
|
t.Errorf("user should be created with inline search value")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create 3"}).Attrs("age", 44).FirstOrCreate(&user4)
|
||||||
|
if user4.Name != "find or create 3" || user4.Id == 0 || user4.Age != 44 {
|
||||||
|
t.Errorf("user should be created with search value and attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedAt1 := user4.UpdatedAt
|
||||||
|
db.Where(&User{Name: "find or create 3"}).Assign("age", 55).FirstOrCreate(&user4)
|
||||||
|
if updatedAt1.Format(time.RFC3339Nano) == user4.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
|
t.Errorf("UpdateAt should be changed when update values with assign")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create 4"}).Assign(User{Age: 44}).FirstOrCreate(&user4)
|
||||||
|
if user4.Name != "find or create 4" || user4.Id == 0 || user4.Age != 44 {
|
||||||
|
t.Errorf("user should be created with search value and assigned attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create"}).Attrs("age", 44).FirstOrInit(&user5)
|
||||||
|
if user5.Name != "find or create" || user5.Id == 0 || user5.Age != 33 {
|
||||||
|
t.Errorf("user should be found and not initialized by Attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create"}).Assign(User{Age: 44}).FirstOrCreate(&user6)
|
||||||
|
if user6.Name != "find or create" || user6.Id == 0 || user6.Age != 44 {
|
||||||
|
t.Errorf("user should be found and updated with assigned attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create"}).Find(&user7)
|
||||||
|
if user7.Name != "find or create" || user7.Id == 0 || user7.Age != 44 {
|
||||||
|
t.Errorf("user should be found and updated with assigned attrs")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Where(&User{Name: "find or create embedded struct"}).Assign(User{Age: 44, CreditCard: CreditCard{Number: "1231231231"}, Emails: []Email{{Email: "jinzhu@assign_embedded_struct.com"}, {Email: "jinzhu-2@assign_embedded_struct.com"}}}).FirstOrCreate(&user8)
|
||||||
|
if db.Where("email = ?", "jinzhu-2@assign_embedded_struct.com").First(&Email{}).RecordNotFound() {
|
||||||
|
t.Errorf("embedded struct email should be saved")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.Where("email = ?", "1231231231").First(&CreditCard{}).RecordNotFound() {
|
||||||
|
t.Errorf("embedded struct credit card should be saved")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSelectWithEscapedFieldName(t *testing.T) {
|
||||||
|
user1 := User{Name: "EscapedFieldNameUser", Age: 1}
|
||||||
|
user2 := User{Name: "EscapedFieldNameUser", Age: 10}
|
||||||
|
user3 := User{Name: "EscapedFieldNameUser", Age: 20}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
|
||||||
|
var names []string
|
||||||
|
db.Model(User{}).Where(&User{Name: "EscapedFieldNameUser"}).Pluck("\"name\"", &names)
|
||||||
|
|
||||||
|
if len(names) != 3 {
|
||||||
|
t.Errorf("Expected 3 name, but got: %d", len(names))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,136 @@
|
||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSubStruct(t *testing.T) {
|
||||||
|
db.DropTable(Category{})
|
||||||
|
db.DropTable(Post{})
|
||||||
|
db.DropTable(Comment{})
|
||||||
|
|
||||||
|
db.CreateTable(Category{})
|
||||||
|
db.CreateTable(Post{})
|
||||||
|
db.CreateTable(Comment{})
|
||||||
|
|
||||||
|
post := Post{
|
||||||
|
Title: "post 1",
|
||||||
|
Body: "body 1",
|
||||||
|
Comments: []Comment{{Content: "Comment 1"}, {Content: "Comment 2"}},
|
||||||
|
Category: Category{Name: "Category 1"},
|
||||||
|
MainCategory: Category{Name: "Main Category 1"},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Save(&post).Error; err != nil {
|
||||||
|
t.Errorf("Got errors when save post")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.First(&Category{}, "name = ?", "Category 1").Error != nil {
|
||||||
|
t.Errorf("Category should be saved")
|
||||||
|
}
|
||||||
|
|
||||||
|
var p Post
|
||||||
|
db.First(&p, post.Id)
|
||||||
|
|
||||||
|
if post.CategoryId.Int64 == 0 || p.CategoryId.Int64 == 0 || post.MainCategoryId == 0 || p.MainCategoryId == 0 {
|
||||||
|
t.Errorf("Category Id should exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.First(&Comment{}, "content = ?", "Comment 1").Error != nil {
|
||||||
|
t.Errorf("Comment 1 should be saved")
|
||||||
|
}
|
||||||
|
if post.Comments[0].PostId == 0 {
|
||||||
|
t.Errorf("Comment Should have post id")
|
||||||
|
}
|
||||||
|
|
||||||
|
var comment Comment
|
||||||
|
if db.First(&comment, "content = ?", "Comment 2").Error != nil {
|
||||||
|
t.Errorf("Comment 2 should be saved")
|
||||||
|
}
|
||||||
|
|
||||||
|
if comment.PostId == 0 {
|
||||||
|
t.Errorf("Comment 2 Should have post id")
|
||||||
|
}
|
||||||
|
|
||||||
|
comment3 := Comment{Content: "Comment 3", Post: Post{Title: "Title 3", Body: "Body 3"}}
|
||||||
|
db.Save(&comment3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRelated(t *testing.T) {
|
||||||
|
user := User{
|
||||||
|
Name: "jinzhu",
|
||||||
|
BillingAddress: Address{Address1: "Billing Address - Address 1"},
|
||||||
|
ShippingAddress: Address{Address1: "Shipping Address - Address 1"},
|
||||||
|
Emails: []Email{{Email: "jinzhu@example.com"}, {Email: "jinzhu-2@example@example.com"}},
|
||||||
|
CreditCard: CreditCard{Number: "1234567890"},
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Save(&user)
|
||||||
|
|
||||||
|
if user.CreditCard.Id == 0 {
|
||||||
|
t.Errorf("After user save, credit card should have id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.BillingAddress.Id == 0 {
|
||||||
|
t.Errorf("After user save, billing address should have id")
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.Emails[0].Id == 0 {
|
||||||
|
t.Errorf("After user save, billing address should have id")
|
||||||
|
}
|
||||||
|
|
||||||
|
var emails []Email
|
||||||
|
db.Model(&user).Related(&emails)
|
||||||
|
if len(emails) != 2 {
|
||||||
|
t.Errorf("Should have two emails")
|
||||||
|
}
|
||||||
|
|
||||||
|
var emails2 []Email
|
||||||
|
db.Model(&user).Where("email = ?", "jinzhu@example.com").Related(&emails2)
|
||||||
|
if len(emails2) != 1 {
|
||||||
|
t.Errorf("Should have two emails")
|
||||||
|
}
|
||||||
|
|
||||||
|
var user1 User
|
||||||
|
db.Model(&user).Related(&user1.Emails)
|
||||||
|
if len(user1.Emails) != 2 {
|
||||||
|
t.Errorf("Should have only one email match related condition")
|
||||||
|
}
|
||||||
|
|
||||||
|
var address1 Address
|
||||||
|
db.Model(&user).Related(&address1, "BillingAddressId")
|
||||||
|
if address1.Address1 != "Billing Address - Address 1" {
|
||||||
|
t.Errorf("Should get billing address from user correctly")
|
||||||
|
}
|
||||||
|
|
||||||
|
user1 = User{}
|
||||||
|
db.Model(&address1).Related(&user1, "BillingAddressId")
|
||||||
|
if db.NewRecord(user1) {
|
||||||
|
t.Errorf("Should get user from address correctly")
|
||||||
|
}
|
||||||
|
|
||||||
|
var user2 User
|
||||||
|
db.Model(&emails[0]).Related(&user2)
|
||||||
|
if user2.Id != user.Id || user2.Name != user.Name {
|
||||||
|
t.Errorf("Should get user from email correctly")
|
||||||
|
}
|
||||||
|
|
||||||
|
var creditcard CreditCard
|
||||||
|
var user3 User
|
||||||
|
db.First(&creditcard, "number = ?", "1234567890")
|
||||||
|
db.Model(&creditcard).Related(&user3)
|
||||||
|
if user3.Id != user.Id || user3.Name != user.Name {
|
||||||
|
t.Errorf("Should get user from credit card correctly")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !db.Model(&CreditCard{}).Related(&User{}).RecordNotFound() {
|
||||||
|
t.Errorf("RecordNotFound for Related")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryManyToManyWithRelated(t *testing.T) {
|
||||||
|
db.Model(&User{}).Related(&[]Language{}, "Languages")
|
||||||
|
// SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111
|
||||||
|
// db.Model(&User{}).Many2Many("Languages").Find(&[]Language{})
|
||||||
|
// db.Model(&User{}).Many2Many("Languages").Add(&Language{})
|
||||||
|
// db.Model(&User{}).Many2Many("Languages").Remove(&Language{})
|
||||||
|
// db.Model(&User{}).Many2Many("Languages").Replace(&[]Language{})
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NameIn1And2(d *gorm.DB) *gorm.DB {
|
||||||
|
return d.Where("name in (?)", []string{"ScopeUser1", "ScopeUser2"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NameIn2And3(d *gorm.DB) *gorm.DB {
|
||||||
|
return d.Where("name in (?)", []string{"ScopeUser2", "ScopeUser3"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NameIn(names []string) func(d *gorm.DB) *gorm.DB {
|
||||||
|
return func(d *gorm.DB) *gorm.DB {
|
||||||
|
return d.Where("name in (?)", names)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScopes(t *testing.T) {
|
||||||
|
user1 := User{Name: "ScopeUser1", Age: 1}
|
||||||
|
user2 := User{Name: "ScopeUser2", Age: 1}
|
||||||
|
user3 := User{Name: "ScopeUser3", Age: 2}
|
||||||
|
db.Save(&user1).Save(&user2).Save(&user3)
|
||||||
|
|
||||||
|
var users1, users2, users3 []User
|
||||||
|
db.Scopes(NameIn1And2).Find(&users1)
|
||||||
|
if len(users1) != 2 {
|
||||||
|
t.Errorf("Should found two users's name in 1, 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Scopes(NameIn1And2, NameIn2And3).Find(&users2)
|
||||||
|
if len(users2) != 1 {
|
||||||
|
t.Errorf("Should found one user's name is 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Scopes(NameIn([]string{user1.Name, user3.Name})).Find(&users3)
|
||||||
|
if len(users3) != 2 {
|
||||||
|
t.Errorf("Should found two users's name in 1, 3")
|
||||||
|
}
|
||||||
|
}
|
|
@ -179,20 +179,6 @@ func (c Cart) TableName() string {
|
||||||
return "shopping_cart"
|
return "shopping_cart"
|
||||||
}
|
}
|
||||||
|
|
||||||
type BigEmail struct {
|
|
||||||
Id int64
|
|
||||||
UserId int64
|
|
||||||
Email string
|
|
||||||
UserAgent string
|
|
||||||
RegisteredAt time.Time
|
|
||||||
CreatedAt time.Time
|
|
||||||
UpdatedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BigEmail) TableName() string {
|
|
||||||
return "emails"
|
|
||||||
}
|
|
||||||
|
|
||||||
type NullTime struct {
|
type NullTime struct {
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Valid bool
|
Valid bool
|
||||||
|
|
195
update_test.go
195
update_test.go
|
@ -6,88 +6,52 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdate(t *testing.T) {
|
func TestUpdate(t *testing.T) {
|
||||||
product1 := Product{Code: "123"}
|
product1 := Product{Code: "product1code"}
|
||||||
product2 := Product{Code: "234"}
|
product2 := Product{Code: "product2code"}
|
||||||
animal1 := Animal{Name: "Ferdinand"}
|
|
||||||
animal2 := Animal{Name: "nerdz"}
|
|
||||||
|
|
||||||
db.Save(&product1).Save(&product2).Update("code", "456")
|
db.Save(&product1).Save(&product2).Update("code", "product2newcode")
|
||||||
|
|
||||||
if product2.Code != "456" {
|
if product2.Code != "product2newcode" {
|
||||||
t.Errorf("Record should be updated with update attributes")
|
t.Errorf("Record should be updated")
|
||||||
}
|
|
||||||
|
|
||||||
db.Save(&animal1).Save(&animal2).Update("name", "Francis")
|
|
||||||
|
|
||||||
if animal2.Name != "Francis" {
|
|
||||||
t.Errorf("Record should be updated with update attributes")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
db.First(&product1, product1.Id)
|
db.First(&product1, product1.Id)
|
||||||
db.First(&product2, product2.Id)
|
db.First(&product2, product2.Id)
|
||||||
updated_at1 := product1.UpdatedAt
|
updatedAt1 := product1.UpdatedAt
|
||||||
updated_at2 := product2.UpdatedAt
|
updatedAt2 := product2.UpdatedAt
|
||||||
|
|
||||||
db.First(&animal1, animal1.Counter)
|
|
||||||
db.First(&animal2, animal2.Counter)
|
|
||||||
animalUpdated_at1 := animal1.UpdatedAt
|
|
||||||
animalUpdated_at2 := animal2.UpdatedAt
|
|
||||||
|
|
||||||
var product3 Product
|
var product3 Product
|
||||||
db.First(&product3, product2.Id).Update("code", "456")
|
db.First(&product3, product2.Id).Update("code", "product2newcode")
|
||||||
if updated_at2.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
|
if updatedAt2.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
t.Errorf("updated_at should not be updated if nothing changed")
|
t.Errorf("updatedAt should not be updated if nothing changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '123'").Error != nil {
|
if db.First(&Product{}, "code = ?", product1.Code).RecordNotFound() {
|
||||||
t.Errorf("Product 123 should not be updated")
|
t.Errorf("Product1 should not be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '234'").Error == nil {
|
if !db.First(&Product{}, "code = ?", "product2code").RecordNotFound() {
|
||||||
t.Errorf("Product 234 should be changed to 456")
|
t.Errorf("Product2's code should be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '456'").Error != nil {
|
if db.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
|
||||||
t.Errorf("Product 234 should be changed to 456")
|
t.Errorf("Product2's code should be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
var animal3 Animal
|
db.Table("products").Where("code in (?)", []string{"product1code"}).Update("code", "product1newcode")
|
||||||
db.First(&animal3, animal2.Counter).Update("Name", "Robert")
|
|
||||||
|
|
||||||
if animalUpdated_at2.Format(time.RFC3339Nano) != animal2.UpdatedAt.Format(time.RFC3339Nano) {
|
|
||||||
t.Errorf("updated_at should not be updated if nothing changed")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'Ferdinand'").Error != nil {
|
|
||||||
t.Errorf("Animal 'Ferdinand' should not be updated")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'nerdz'").Error == nil {
|
|
||||||
t.Errorf("Animal 'nerdz' should be changed to 'Francis'")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'Robert'").Error != nil {
|
|
||||||
t.Errorf("Animal 'nerdz' should be changed to 'Robert'")
|
|
||||||
}
|
|
||||||
|
|
||||||
db.Table("products").Where("code in (?)", []string{"123"}).Update("code", "789")
|
|
||||||
|
|
||||||
var product4 Product
|
var product4 Product
|
||||||
db.First(&product4, product1.Id)
|
db.First(&product4, product1.Id)
|
||||||
if updated_at1.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
|
if updatedAt1.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
t.Errorf("updated_at should be updated if something changed")
|
t.Errorf("updatedAt should be updated if something changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '123'").Error == nil {
|
if !db.First(&Product{}, "code = 'product1code'").RecordNotFound() {
|
||||||
t.Errorf("Product 123 should be changed to 789")
|
t.Errorf("Product1's code should be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '456'").Error != nil {
|
if db.First(&Product{}, "code = 'product1newcode'").RecordNotFound() {
|
||||||
t.Errorf("Product 456 should not be changed to 789")
|
t.Errorf("Product should not be changed to 789")
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Product{}, "code = '789'").Error != nil {
|
|
||||||
t.Errorf("Product 123 should be changed to 789")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.Model(product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
|
if db.Model(product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
|
||||||
|
@ -98,32 +62,22 @@ func TestUpdate(t *testing.T) {
|
||||||
t.Error("No error should raise when update_column with CamelCase")
|
t.Error("No error should raise when update_column with CamelCase")
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Table("animals").Where("name in (?)", []string{"Ferdinand"}).Update("name", "Franz")
|
var products []Product
|
||||||
|
db.Find(&products)
|
||||||
var animal4 Animal
|
if count := db.Model(Product{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(products)) {
|
||||||
db.First(&animal4, animal1.Counter)
|
t.Error("RowsAffected should be correct when do batch update")
|
||||||
if animalUpdated_at1.Format(time.RFC3339Nano) != animal4.UpdatedAt.Format(time.RFC3339Nano) {
|
}
|
||||||
t.Errorf("animalUpdated_at should be updated if something changed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'Ferdinand'").Error == nil {
|
func TestUpdateWithNoStdPrimaryKey(t *testing.T) {
|
||||||
t.Errorf("Animal 'Ferdinand' should be changed to 'Franz'")
|
animal := Animal{Name: "Ferdinand"}
|
||||||
}
|
db.Save(&animal)
|
||||||
|
updatedAt1 := animal.UpdatedAt
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'Robert'").Error != nil {
|
db.Save(&animal).Update("name", "Francis")
|
||||||
t.Errorf("Animal 'Robert' should not be changed to 'Francis'")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Animal{}, "name = 'Franz'").Error != nil {
|
if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
t.Errorf("Animal 'nerdz' should be changed to 'Franz'")
|
t.Errorf("updatedAt should not be updated if nothing changed")
|
||||||
}
|
|
||||||
|
|
||||||
if db.Model(animal2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
|
|
||||||
t.Error("No error should raise when update with CamelCase")
|
|
||||||
}
|
|
||||||
|
|
||||||
if db.Model(&animal2).UpdateColumn("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
|
|
||||||
t.Error("No error should raise when update_column with CamelCase")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var animals []Animal
|
var animals []Animal
|
||||||
|
@ -134,82 +88,73 @@ func TestUpdate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdates(t *testing.T) {
|
func TestUpdates(t *testing.T) {
|
||||||
product1 := Product{Code: "abc", Price: 10}
|
product1 := Product{Code: "product1code", Price: 10}
|
||||||
product2 := Product{Code: "cde", Price: 20}
|
product2 := Product{Code: "product2code", Price: 10}
|
||||||
db.Save(&product1).Save(&product2)
|
db.Save(&product1).Save(&product2)
|
||||||
db.Model(&product2).Updates(map[string]interface{}{"code": "edf", "price": 100})
|
db.Model(&product1).Updates(map[string]interface{}{"code": "product1newcode", "price": 100})
|
||||||
if product2.Code != "edf" || product2.Price != 100 {
|
if product1.Code != "product1newcode" || product1.Price != 100 {
|
||||||
t.Errorf("Record should be updated also with update attributes")
|
t.Errorf("Record should be updated also with map")
|
||||||
}
|
}
|
||||||
|
|
||||||
db.First(&product1, product1.Id)
|
db.First(&product1, product1.Id)
|
||||||
db.First(&product2, product2.Id)
|
db.First(&product2, product2.Id)
|
||||||
updated_at1 := product1.UpdatedAt
|
updatedAt1 := product1.UpdatedAt
|
||||||
updated_at2 := product2.UpdatedAt
|
updatedAt2 := product2.UpdatedAt
|
||||||
|
|
||||||
var product3 Product
|
var product3 Product
|
||||||
db.First(&product3, product2.Id).Updates(Product{Code: "edf", Price: 100})
|
db.First(&product3, product1.Id).Updates(Product{Code: "product1newcode", Price: 100})
|
||||||
if product3.Code != "edf" || product3.Price != 100 {
|
if product3.Code != "product1newcode" || product3.Price != 100 {
|
||||||
t.Errorf("Record should be updated with update attributes")
|
t.Errorf("Record should be updated with struct")
|
||||||
}
|
}
|
||||||
|
|
||||||
if updated_at2.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
|
if updatedAt1.Format(time.RFC3339Nano) != product3.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
t.Errorf("updated_at should not be updated if nothing changed")
|
t.Errorf("updatedAt should not be updated if nothing changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = 'abc' and price = 10").Error != nil {
|
if db.First(&Product{}, "code = ? and price = ?", product2.Code, product2.Price).RecordNotFound() {
|
||||||
t.Errorf("Product abc should not be updated")
|
t.Errorf("Product2 should not be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = 'cde'").Error == nil {
|
if db.First(&Product{}, "code = ?", "product1newcode").RecordNotFound() {
|
||||||
t.Errorf("Product cde should be renamed to edf")
|
t.Errorf("Product1 should be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = 'edf' and price = 100").Error != nil {
|
db.Table("products").Where("code in (?)", []string{"product2code"}).Updates(Product{Code: "product2newcode"})
|
||||||
t.Errorf("Product cde should be renamed to edf")
|
if !db.First(&Product{}, "code = 'product2code'").RecordNotFound() {
|
||||||
}
|
t.Errorf("Product2's code should be updated")
|
||||||
|
|
||||||
db.Table("products").Where("code in (?)", []string{"abc"}).Updates(map[string]string{"code": "fgh", "price": "200"})
|
|
||||||
if db.First(&Product{}, "code = 'abc'").Error == nil {
|
|
||||||
t.Errorf("Product abc's code should be changed to fgh")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var product4 Product
|
var product4 Product
|
||||||
db.First(&product4, product1.Id)
|
db.First(&product4, product2.Id)
|
||||||
if updated_at1.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
|
if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
t.Errorf("updated_at should be updated if something changed")
|
t.Errorf("updatedAt should be updated if something changed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.First(&Product{}, "code = 'edf' and price = ?", 100).Error != nil {
|
if db.First(&Product{}, "code = ?", "product2newcode").RecordNotFound() {
|
||||||
t.Errorf("Product cde's code should not be changed to fgh")
|
t.Errorf("product2's code should be updated")
|
||||||
}
|
|
||||||
|
|
||||||
if db.First(&Product{}, "code = 'fgh' and price = 200").Error != nil {
|
|
||||||
t.Errorf("We should have Product fgh")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateColumn(t *testing.T) {
|
func TestUpdateColumn(t *testing.T) {
|
||||||
product1 := Product{Code: "update_column 1", Price: 10}
|
product1 := Product{Code: "product1code", Price: 10}
|
||||||
product2 := Product{Code: "update_column 2", Price: 20}
|
product2 := Product{Code: "product2code", Price: 20}
|
||||||
db.Save(&product1).Save(&product2).UpdateColumn(map[string]interface{}{"code": "update_column 3", "price": 100})
|
db.Save(&product1).Save(&product2).UpdateColumn(map[string]interface{}{"code": "product2newcode", "price": 100})
|
||||||
if product2.Code != "update_column 3" || product2.Price != 100 {
|
if product2.Code != "product2newcode" || product2.Price != 100 {
|
||||||
t.Errorf("product 2 should be updated with update column")
|
t.Errorf("product 2 should be updated with update column")
|
||||||
}
|
}
|
||||||
|
|
||||||
var product3 Product
|
var product3 Product
|
||||||
db.First(&product3, product1.Id)
|
db.First(&product3, product1.Id)
|
||||||
if product3.Code != "update_column 1" || product3.Price != 10 {
|
if product3.Code != "product1code" || product3.Price != 10 {
|
||||||
t.Errorf("product 1 should not be updated")
|
t.Errorf("product 1 should not be updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
var product4, product5 Product
|
db.First(&product2, product2.Id)
|
||||||
|
updatedAt2 := product2.UpdatedAt
|
||||||
|
db.Model(product2).UpdateColumn("code", "update_column_new")
|
||||||
|
var product4 Product
|
||||||
db.First(&product4, product2.Id)
|
db.First(&product4, product2.Id)
|
||||||
updated_at1 := product4.UpdatedAt
|
if updatedAt2.Format(time.RFC3339Nano) != product4.UpdatedAt.Format(time.RFC3339Nano) {
|
||||||
|
t.Errorf("updatedAt should not be updated with update column")
|
||||||
db.Model(Product{}).Where(product2.Id).UpdateColumn("code", "update_column_new")
|
|
||||||
db.First(&product5, product2.Id)
|
|
||||||
if updated_at1.Format(time.RFC3339Nano) != product5.UpdatedAt.Format(time.RFC3339Nano) {
|
|
||||||
t.Errorf("updated_at should not be updated with update column")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue