gorm/clause/clause_test.go

44 lines
1012 B
Go
Raw Normal View History

2020-02-04 04:51:19 +03:00
package clause_test
import (
"reflect"
2020-02-07 18:45:35 +03:00
"strings"
2020-02-04 04:51:19 +03:00
"sync"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
2020-06-02 05:34:50 +03:00
"gorm.io/gorm/utils/tests"
2020-02-04 04:51:19 +03:00
)
2020-02-07 18:45:35 +03:00
var db, _ = gorm.Open(tests.DummyDialector{}, nil)
func checkBuildClauses(t *testing.T, clauses []clause.Interface, result string, vars []interface{}) {
2020-02-04 04:51:19 +03:00
var (
2020-02-07 18:45:35 +03:00
buildNames []string
buildNamesMap = map[string]bool{}
2020-02-26 14:06:42 +03:00
user, _ = schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
2020-02-07 18:45:35 +03:00
stmt = gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
2020-02-04 04:51:19 +03:00
)
2020-02-07 18:45:35 +03:00
for _, c := range clauses {
if _, ok := buildNamesMap[c.Name()]; !ok {
buildNames = append(buildNames, c.Name())
buildNamesMap[c.Name()] = true
}
stmt.AddClause(c)
}
stmt.Build(buildNames...)
if strings.TrimSpace(stmt.SQL.String()) != result {
t.Errorf("SQL expects %v got %v", result, stmt.SQL.String())
}
if !reflect.DeepEqual(stmt.Vars, vars) {
t.Errorf("Vars expects %+v got %v", stmt.Vars, vars)
2020-02-04 04:51:19 +03:00
}
}