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
|
|
|
}},
|
2022-01-06 10:02:53 +03:00
|
|
|
"SELECT * FROM `users` GROUP BY `role` HAVING `role` = ?",
|
|
|
|
[]interface{}{"admin"},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]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
|
|
|
}},
|
2022-01-06 10:02:53 +03:00
|
|
|
"SELECT * FROM `users` GROUP BY `role`,`gender` HAVING `role` = ? AND `gender` <> ?",
|
|
|
|
[]interface{}{"admin", "U"},
|
2020-02-07 18:45:35 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, result := range results {
|
|
|
|
t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) {
|
|
|
|
checkBuildClauses(t, result.Clauses, result.Result, result.Vars)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|