2020-02-07 18:45:35 +03:00
|
|
|
package clause_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-06 12:47:30 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-02-07 18:45:35 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-02-07 18:45:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSet(t *testing.T) {
|
|
|
|
results := []struct {
|
|
|
|
Clauses []clause.Interface
|
|
|
|
Result string
|
|
|
|
Vars []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]clause.Interface{
|
|
|
|
clause.Update{},
|
|
|
|
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
|
|
|
},
|
2022-01-06 10:02:53 +03:00
|
|
|
"UPDATE `users` SET `users`.`id`=?",
|
|
|
|
[]interface{}{1},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]clause.Interface{
|
|
|
|
clause.Update{},
|
|
|
|
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
|
|
|
clause.Set([]clause.Assignment{{clause.Column{Name: "name"}, "jinzhu"}}),
|
|
|
|
},
|
2022-01-06 10:02:53 +03:00
|
|
|
"UPDATE `users` SET `name`=?",
|
|
|
|
[]interface{}{"jinzhu"},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, result := range results {
|
|
|
|
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
|
|
|
|
checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-06-06 12:47:30 +03:00
|
|
|
|
|
|
|
func TestAssignments(t *testing.T) {
|
|
|
|
set := clause.Assignments(map[string]interface{}{
|
|
|
|
"name": "jinzhu",
|
|
|
|
"age": 18,
|
|
|
|
})
|
|
|
|
|
|
|
|
assignments := []clause.Assignment(set)
|
|
|
|
|
|
|
|
sort.Slice(assignments, func(i, j int) bool {
|
|
|
|
return strings.Compare(assignments[i].Column.Name, assignments[j].Column.Name) > 0
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(assignments) != 2 || assignments[0].Column.Name != "name" || assignments[0].Value.(string) != "jinzhu" || assignments[1].Column.Name != "age" || assignments[1].Value.(int) != 18 {
|
|
|
|
t.Errorf("invalid assignments, got %v", assignments)
|
|
|
|
}
|
|
|
|
}
|