clause_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package clause_test
  2. import (
  3. "reflect"
  4. "strings"
  5. "sync"
  6. "testing"
  7. "github.com/jinzhu/gorm"
  8. "github.com/jinzhu/gorm/clause"
  9. "github.com/jinzhu/gorm/schema"
  10. "github.com/jinzhu/gorm/tests"
  11. )
  12. var db, _ = gorm.Open(tests.DummyDialector{}, nil)
  13. func checkBuildClauses(t *testing.T, clauses []clause.Interface, result string, vars []interface{}) {
  14. var (
  15. buildNames []string
  16. buildNamesMap = map[string]bool{}
  17. user, _, _ = schema.Parse(&tests.User{}, &sync.Map{}, db.NamingStrategy)
  18. stmt = gorm.Statement{DB: db, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}}
  19. )
  20. for _, c := range clauses {
  21. if _, ok := buildNamesMap[c.Name()]; !ok {
  22. buildNames = append(buildNames, c.Name())
  23. buildNamesMap[c.Name()] = true
  24. }
  25. stmt.AddClause(c)
  26. }
  27. stmt.Build(buildNames...)
  28. if strings.TrimSpace(stmt.SQL.String()) != result {
  29. t.Errorf("SQL expects %v got %v", result, stmt.SQL.String())
  30. }
  31. if !reflect.DeepEqual(stmt.Vars, vars) {
  32. t.Errorf("Vars expects %+v got %v", stmt.Vars, vars)
  33. }
  34. }