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 TestOrderBy(t *testing.T) {
|
|
|
|
results := []struct {
|
|
|
|
Clauses []clause.Interface
|
|
|
|
Result string
|
|
|
|
Vars []interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]clause.Interface{clause.Select{}, clause.From{}, clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{Column: clause.PrimaryColumn, Desc: true}},
|
|
|
|
}},
|
|
|
|
"SELECT * FROM `users` ORDER BY `users`.`id` DESC", nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]clause.Interface{
|
|
|
|
clause.Select{}, clause.From{}, clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{Column: clause.PrimaryColumn, Desc: true}},
|
|
|
|
}, clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{Column: clause.Column{Name: "name"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"SELECT * FROM `users` ORDER BY `users`.`id` DESC,`name`", nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]clause.Interface{
|
|
|
|
clause.Select{}, clause.From{}, clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{Column: clause.PrimaryColumn, Desc: true}},
|
|
|
|
}, clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{Column: clause.Column{Name: "name"}, Reorder: true}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"SELECT * FROM `users` ORDER BY `name`", nil,
|
|
|
|
},
|
2020-11-03 05:30:05 +03:00
|
|
|
{
|
|
|
|
[]clause.Interface{
|
|
|
|
clause.Select{}, clause.From{}, clause.OrderBy{
|
|
|
|
Expression: clause.Expr{SQL: "FIELD(id, ?)", Vars: []interface{}{[]int{1, 2, 3}}, WithoutParentheses: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"SELECT * FROM `users` ORDER BY FIELD(id, ?,?,?)", []interface{}{1, 2, 3},
|
|
|
|
},
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|