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"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/jinzhu/gorm/clause"
|
|
|
|
"github.com/jinzhu/gorm/schema"
|
|
|
|
"github.com/jinzhu/gorm/tests"
|
|
|
|
)
|
|
|
|
|
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-23 16:22:35 +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
|
|
|
}
|
|
|
|
}
|