gorm/clause/clause_test.go

55 lines
1.2 KiB
Go

package clause_test
import (
"fmt"
"reflect"
"sync"
"testing"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/clause"
"github.com/jinzhu/gorm/schema"
"github.com/jinzhu/gorm/tests"
)
func TestClause(t *testing.T) {
var (
db, _ = gorm.Open(nil, nil)
results = []struct {
Clauses []clause.Interface
Result string
Vars []interface{}
}{{
[]clause.Interface{clause.Select{}, clause.From{}},
"SELECT * FROM users", []interface{}{},
}}
)
for idx, result := range results {
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
var (
user, _ = schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
stmt = gorm.Statement{
DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{},
}
buildNames []string
)
for _, c := range result.Clauses {
buildNames = append(buildNames, c.Name())
stmt.AddClause(c)
}
stmt.Build(buildNames...)
if stmt.SQL.String() != result.Result {
t.Errorf("SQL expects %v got %v", result.Result, stmt.SQL.String())
}
if reflect.DeepEqual(stmt.Vars, result.Vars) {
t.Errorf("Vars expects %+v got %v", stmt.Vars, result.Vars)
}
})
}
}