Fixed implementation & tests
This commit is contained in:
parent
eb5cd0654f
commit
4a301464df
@ -129,7 +129,7 @@ func saveAfterAssociationsCallback(scope *Scope) {
|
|||||||
scope.Err(newDB.Save(elem).Error)
|
scope.Err(newDB.Save(elem).Error)
|
||||||
}
|
}
|
||||||
} else if autoUpdate {
|
} else if autoUpdate {
|
||||||
scope.Err(newDB.Save(elem).Error)
|
scope.Err(newScope.DB().Updates(elem).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !scope.New(newScope.Value).PrimaryKeyZero() && saveReference {
|
if !scope.New(newScope.Value).PrimaryKeyZero() && saveReference {
|
||||||
|
@ -75,7 +75,7 @@ func updateCallback(scope *Scope) {
|
|||||||
} else {
|
} else {
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if scope.changeableField(field) {
|
if scope.changeableField(field) {
|
||||||
if !field.IsPrimaryKey && field.IsNormal && (!field.IsBlank || field.HasDefaultValue || field.IsForeignKey) {
|
if !field.IsPrimaryKey && field.IsNormal && (field.Name != "CreatedAt" || !field.IsBlank) {
|
||||||
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
|
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
|
||||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
||||||
}
|
}
|
||||||
|
@ -98,12 +98,12 @@ func TestRunCallbacks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DB.Where("Code = ?", "unique_code").First(&p)
|
DB.Where("Code = ?", "unique_code").First(&p)
|
||||||
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 0, 1, 0, 0, 0, 0, 2}) {
|
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())
|
t.Errorf("After update callbacks values are not saved, %v", p.GetCallTimes())
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.Delete(&p)
|
DB.Delete(&p)
|
||||||
if !reflect.DeepEqual(p.GetCallTimes(), []int64{1, 2, 0, 1, 0, 0, 1, 1, 2}) {
|
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())
|
t.Errorf("After delete callbacks should be invoked successfully, %v", p.GetCallTimes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
main_test.go
20
main_test.go
@ -841,9 +841,27 @@ func TestJoinsWithSelect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
DB.Save(&user)
|
DB.Save(&user)
|
||||||
|
|
||||||
|
validateEmails := func(results []result, emails []string) bool {
|
||||||
|
if len(results) != len(emails) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range results {
|
||||||
|
containsEmail := false
|
||||||
|
for _, email := range emails {
|
||||||
|
if email == r.Email {
|
||||||
|
containsEmail = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !containsEmail {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
var results []result
|
var results []result
|
||||||
DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results)
|
DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results)
|
||||||
if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" {
|
if len(results) != 2 || !validateEmails(results, []string{"join1@example.com", "join2@example.com"}) {
|
||||||
t.Errorf("Should find all two emails with Join select")
|
t.Errorf("Should find all two emails with Join select")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user