forked from taosdata/tdengine_gorm
/
clause.go
61 lines (55 loc) · 1.32 KB
/
clause.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tests
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
"reflect"
"strings"
"sync"
"testing"
)
var db, _ = gorm.Open(DummyDialector{}, nil)
func CheckBuildClauses(t *testing.T, clauses []clause.Interface, results []string, vars [][][]interface{}) {
var (
buildNames []string
buildNamesMap = map[string]bool{}
user, _ = schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
stmt = gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
)
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...)
sql := strings.TrimSpace(stmt.SQL.String())
matched := false
for i, result := range results {
if sql == result {
matched = true
matchVars := false
if len(stmt.Vars) > 0 {
if len(vars) > i {
for _, varItem := range vars[i] {
if reflect.DeepEqual(stmt.Vars, varItem) {
matchVars = true
break
}
}
}
} else {
matchVars = true
}
if !matchVars {
t.Errorf("Vars expects %+v got %v", stmt.Vars, vars[i])
}
break
}
}
if !matched {
t.Errorf("SQL expects in %v got %v", results, sql)
}
}