move dir and add ut for ParseOrder
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
package clauses
|
||||
package order
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"github.com/astaxie/beego/client/orm/structs"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Sort int8
|
||||
|
||||
const (
|
||||
SortNone Sort = 0
|
||||
SortAscending Sort = 1
|
||||
SortDescending Sort = 2
|
||||
None Sort = 0
|
||||
Ascending Sort = 1
|
||||
Descending Sort = 2
|
||||
)
|
||||
|
||||
type OrderOption func(order *Order)
|
||||
type Option func(order *Order)
|
||||
|
||||
type Order struct {
|
||||
column string
|
||||
@@ -18,7 +21,7 @@ type Order struct {
|
||||
isRaw bool
|
||||
}
|
||||
|
||||
func OrderClause(options ...OrderOption) *Order {
|
||||
func Clause(options ...Option) *Order {
|
||||
o := &Order{}
|
||||
for _, option := range options {
|
||||
option(o)
|
||||
@@ -37,9 +40,9 @@ func (o *Order) GetSort() Sort {
|
||||
|
||||
func (o *Order) SortString() string {
|
||||
switch o.GetSort() {
|
||||
case SortAscending:
|
||||
case Ascending:
|
||||
return "ASC"
|
||||
case SortDescending:
|
||||
case Descending:
|
||||
return "DESC"
|
||||
}
|
||||
|
||||
@@ -53,10 +56,10 @@ func (o *Order) IsRaw() bool {
|
||||
func ParseOrder(expressions ...string) []*Order {
|
||||
var orders []*Order
|
||||
for _, expression := range expressions {
|
||||
sort := SortAscending
|
||||
column := strings.ReplaceAll(expression, ExprSep, ExprDot)
|
||||
sort := Ascending
|
||||
column := strings.ReplaceAll(expression, structs.ExprSep, structs.ExprDot)
|
||||
if column[0] == '-' {
|
||||
sort = SortDescending
|
||||
sort = Descending
|
||||
column = column[1:]
|
||||
}
|
||||
|
||||
@@ -69,31 +72,31 @@ func ParseOrder(expressions ...string) []*Order {
|
||||
return orders
|
||||
}
|
||||
|
||||
func OrderColumn(column string) OrderOption {
|
||||
func Column(column string) Option {
|
||||
return func(order *Order) {
|
||||
order.column = strings.ReplaceAll(column, ExprSep, ExprDot)
|
||||
order.column = strings.ReplaceAll(column, structs.ExprSep, structs.ExprDot)
|
||||
}
|
||||
}
|
||||
|
||||
func sort(sort Sort) OrderOption {
|
||||
func sort(sort Sort) Option {
|
||||
return func(order *Order) {
|
||||
order.sort = sort
|
||||
}
|
||||
}
|
||||
|
||||
func OrderSortAscending() OrderOption {
|
||||
return sort(SortAscending)
|
||||
func SortAscending() Option {
|
||||
return sort(Ascending)
|
||||
}
|
||||
|
||||
func OrderSortDescending() OrderOption {
|
||||
return sort(SortDescending)
|
||||
func SortDescending() Option {
|
||||
return sort(Descending)
|
||||
}
|
||||
|
||||
func OrderSortNone() OrderOption {
|
||||
return sort(SortNone)
|
||||
func SortNone() Option {
|
||||
return sort(None)
|
||||
}
|
||||
|
||||
func OrderRaw() OrderOption {
|
||||
func Raw() Option {
|
||||
return func(order *Order) {
|
||||
order.isRaw = true
|
||||
}
|
||||
112
client/orm/structs/clauses/order/order_test.go
Normal file
112
client/orm/structs/clauses/order/order_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOrderClause(t *testing.T) {
|
||||
var (
|
||||
column = `a`
|
||||
)
|
||||
|
||||
o := Clause(
|
||||
Column(column),
|
||||
)
|
||||
|
||||
if o.GetColumn() != column {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortAscending(t *testing.T) {
|
||||
o := Clause(
|
||||
SortAscending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != Ascending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortDescending(t *testing.T) {
|
||||
o := Clause(
|
||||
SortDescending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != Descending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortNone(t *testing.T) {
|
||||
o1 := Clause(
|
||||
SortNone(),
|
||||
)
|
||||
|
||||
if o1.GetSort() != None {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := Clause()
|
||||
|
||||
if o2.GetSort() != None {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderRaw(t *testing.T) {
|
||||
o1 := Clause()
|
||||
|
||||
if o1.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := Clause(
|
||||
Raw(),
|
||||
)
|
||||
|
||||
if !o2.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderColumn(t *testing.T) {
|
||||
o1 := Clause(
|
||||
Column(`aaa`),
|
||||
)
|
||||
|
||||
if o1.GetColumn() != `aaa` {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOrder(t *testing.T) {
|
||||
orders := ParseOrder(
|
||||
`-user__status`,
|
||||
`status`,
|
||||
`user__status`,
|
||||
)
|
||||
|
||||
t.Log(orders)
|
||||
|
||||
if orders[0].GetSort() != Descending {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if orders[0].GetColumn() != `user.status` {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if orders[1].GetColumn() != `status` {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if orders[1].GetSort() != Ascending {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
if orders[2].GetColumn() != `user.status` {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package clauses
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestOrderClause(t *testing.T) {
|
||||
var (
|
||||
column = `a`
|
||||
)
|
||||
|
||||
o := OrderClause(
|
||||
OrderColumn(column),
|
||||
)
|
||||
|
||||
if o.GetColumn() != column {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortAscending(t *testing.T) {
|
||||
o := OrderClause(
|
||||
OrderSortAscending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != SortAscending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortDescending(t *testing.T) {
|
||||
o := OrderClause(
|
||||
OrderSortDescending(),
|
||||
)
|
||||
|
||||
if o.GetSort() != SortDescending {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderSortNone(t *testing.T) {
|
||||
o1 := OrderClause(
|
||||
OrderSortNone(),
|
||||
)
|
||||
|
||||
if o1.GetSort() != SortNone {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := OrderClause()
|
||||
|
||||
if o2.GetSort() != SortNone {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderRaw(t *testing.T) {
|
||||
o1 := OrderClause()
|
||||
|
||||
if o1.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
o2 := OrderClause(
|
||||
OrderRaw(),
|
||||
)
|
||||
|
||||
if !o2.IsRaw() {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderColumn(t *testing.T) {
|
||||
o1 := OrderClause(
|
||||
OrderColumn(`aaa`),
|
||||
)
|
||||
|
||||
if o1.GetColumn() != `aaa` {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package clauses
|
||||
package structs
|
||||
|
||||
const (
|
||||
ExprSep = "__"
|
||||
Reference in New Issue
Block a user