gorm/clause/group_by_test.go

41 lines
1.1 KiB
Go
Raw Normal View History

2020-02-07 18:45:35 +03:00
package clause_test
import (
"fmt"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm/clause"
2020-02-07 18:45:35 +03:00
)
func TestGroupBy(t *testing.T) {
results := []struct {
Clauses []clause.Interface
Result string
Vars []interface{}
}{
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.GroupBy{
Columns: []clause.Column{{Name: "role"}},
2020-03-08 13:05:22 +03:00
Having: []clause.Expression{clause.Eq{"role", "admin"}},
2020-02-07 18:45:35 +03:00
}},
"SELECT * FROM `users` GROUP BY `role` HAVING `role` = ?", []interface{}{"admin"},
},
{
[]clause.Interface{clause.Select{}, clause.From{}, clause.GroupBy{
Columns: []clause.Column{{Name: "role"}},
2020-03-08 13:05:22 +03:00
Having: []clause.Expression{clause.Eq{"role", "admin"}},
2020-02-07 18:45:35 +03:00
}, clause.GroupBy{
Columns: []clause.Column{{Name: "gender"}},
2020-03-08 13:05:22 +03:00
Having: []clause.Expression{clause.Neq{"gender", "U"}},
2020-02-07 18:45:35 +03:00
}},
"SELECT * FROM `users` GROUP BY `role`,`gender` HAVING `role` = ? AND `gender` <> ?", []interface{}{"admin", "U"},
},
}
for idx, result := range results {
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
})
}
}