added tests for support sql.NamedArg

This commit is contained in:
Nikita Koryabkin 2019-11-29 08:13:09 +03:00
parent 922b2b665f
commit ed05d4b381
2 changed files with 72 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package gorm_test package gorm_test
import ( import (
"database/sql"
"fmt" "fmt"
"reflect" "reflect"
@ -345,6 +346,53 @@ func TestSearchWithMap(t *testing.T) {
} }
} }
func TestSearchWithSqlNameArgs(t *testing.T) {
companyID := 1
user1 := User{Name: "NamedArgSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")}
user2 := User{Name: "NamedArgSearchUser2", Age: 10, Birthday: parseTime("2010-1-1")}
user3 := User{Name: "NamedArgSearchUser3", Age: 20, Birthday: parseTime("2020-1-1")}
user4 := User{Name: "NamedArgSearchUser4", Age: 30, Birthday: parseTime("2020-1-1"), CompanyID: &companyID}
DB.Save(&user1).Save(&user2).Save(&user3).Save(&user4)
var user User
DB.First(&user, []sql.NamedArg{{Name: "name", Value: user1.Name}})
if user.Id == 0 || user.Name != user1.Name {
t.Errorf("Search first record with sql.NamedArg")
}
user = User{}
DB.Where([]sql.NamedArg{{Name: "name", Value: user2.Name}}).First(&user)
if user.Id == 0 || user.Name != user2.Name {
t.Errorf("Search first record with where sql.NamedArg")
}
var users []User
DB.Where([]sql.NamedArg{{Name: "name", Value: user3.Name}}).Find(&users)
if len(users) != 1 {
t.Errorf("Search all records with sql.NamedArg")
}
DB.Find(&users, []sql.NamedArg{{Name: "name", Value: user3.Name}})
if len(users) != 1 {
t.Errorf("Search all records with sql.NamedArg")
}
DB.Find(&users, []sql.NamedArg{{Name: "name", Value: user4.Name}, {Name: "company_id", Value: nil}})
if len(users) != 0 {
t.Errorf("Search all records with sql.NamedArg containing null value finding 0 records")
}
DB.Find(&users, []sql.NamedArg{{Name: "name", Value: user1.Name}, {Name: "company_id", Value: nil}})
if len(users) != 1 {
t.Errorf("Search all records with []sql.NamedArg containing null value finding 1 record")
}
DB.Find(&users, []sql.NamedArg{{Name: "name", Value: user4.Name}, {Name: "company_id", Value: companyID}})
if len(users) != 1 {
t.Errorf("Search all records with []sql.NamedArg")
}
}
func TestSearchWithEmptyChain(t *testing.T) { func TestSearchWithEmptyChain(t *testing.T) {
user1 := User{Name: "ChainSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")} user1 := User{Name: "ChainSearchUser1", Age: 1, Birthday: parseTime("2000-1-1")}
user2 := User{Name: "ChainearchUser2", Age: 10, Birthday: parseTime("2010-1-1")} user2 := User{Name: "ChainearchUser2", Age: 10, Birthday: parseTime("2010-1-1")}
@ -561,6 +609,11 @@ func TestNot(t *testing.T) {
t.Errorf("Should find all user's name not equal to 3 who do not have company id") t.Errorf("Should find all user's name not equal to 3 who do not have company id")
} }
DB.Not([]sql.NamedArg{{Name: "name", Value: "user3"}, {Name: "company_id"}}).Find(&users7)
if len(users1)-len(users7) != 2 { // not user3 or user4
t.Errorf("Should find all user's name not equal to 3 who do not have company id")
}
DB.Not("name", []string{"user3"}).Find(&users8) DB.Not("name", []string{"user3"}).Find(&users8)
if len(users1)-len(users8) != int(name3Count) { if len(users1)-len(users8) != int(name3Count) {
t.Errorf("Should find all users' name not equal 3") t.Errorf("Should find all users' name not equal 3")
@ -637,7 +690,7 @@ func TestFindOrInitialize(t *testing.T) {
} }
func TestFindOrCreate(t *testing.T) { func TestFindOrCreate(t *testing.T) {
var user1, user2, user3, user4, user5, user6, user7, user8 User var user1, user2, user3, user4, user5, user6, user7, user8, user9 User
DB.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1) DB.Where(&User{Name: "find or create", Age: 33}).FirstOrCreate(&user1)
if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 { if user1.Name != "find or create" || user1.Id == 0 || user1.Age != 33 {
t.Errorf("user should be created with search value") t.Errorf("user should be created with search value")
@ -689,6 +742,17 @@ func TestFindOrCreate(t *testing.T) {
t.Errorf("embedded struct email should be saved") t.Errorf("embedded struct email should be saved")
} }
DB.Where([]sql.NamedArg{{Name: "name", Value: "find or create embedded struct"}}).
Assign(
[]sql.NamedArg{
{Name: "age", Value: 26},
{Name: "credit_card", Value: CreditCard{Number: "4344387492301351"}},
{Name: "emails", Value: []Email{{Email: "Nikita@Koryabk.in"}}},
}).FirstOrCreate(&user9)
if DB.Where("email = ?", "Nikita@Koryabk.in").First(&Email{}).RecordNotFound() {
t.Errorf("embedded struct email should be saved")
}
if DB.Where("email = ?", "1231231231").First(&CreditCard{}).RecordNotFound() { if DB.Where("email = ?", "1231231231").First(&CreditCard{}).RecordNotFound() {
t.Errorf("embedded struct credit card should be saved") t.Errorf("embedded struct credit card should be saved")
} }

View File

@ -587,8 +587,14 @@ func (scope *Scope) buildCondition(clause map[string]interface{}, include bool)
case []sql.NamedArg: case []sql.NamedArg:
var sqls []string var sqls []string
for _, val := range value { for _, val := range value {
if len(val.Name) != 0 { if val.Value != nil {
sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", quotedTableName, scope.Quote(val.Name), equalSQL, scope.AddToVars(val))) sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", quotedTableName, scope.Quote(val.Name), equalSQL, scope.AddToVars(val)))
} else {
if !include {
sqls = append(sqls, fmt.Sprintf("(%v.%v IS NOT NULL)", quotedTableName, scope.Quote(val.Name)))
} else {
sqls = append(sqls, fmt.Sprintf("(%v.%v IS NULL)", quotedTableName, scope.Quote(val.Name)))
}
} }
} }
return strings.Join(sqls, " AND ") return strings.Join(sqls, " AND ")