-
Notifications
You must be signed in to change notification settings - Fork 32
/
order_test.go
67 lines (62 loc) · 1.19 KB
/
order_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package underscore
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Order(t *testing.T) {
t.Run("ok", func(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []int
Chain(arr).Order(func(n testModel, _ int) int {
return n.ID
}).Map(func(r testModel, _ int) int {
return r.ID
}).Value(&res)
assert.Len(
t,
res,
len(arr),
)
assert.EqualValues(
t,
res,
[]int{1, 2, 3},
)
})
t.Run("chain", func(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []int
Chain(arr).Where(func(r testModel, _ int) bool {
return r.ID > 5
}).Order(func(r testModel, _ int) int {
return r.ID
}).Map(func(r testModel, _ int) int {
return r.ID
}).Value(&res)
})
}
func Test_OrderBy(t *testing.T) {
arr := []testModel{
{ID: 2, Name: "two"},
{ID: 1, Name: "one"},
{ID: 3, Name: "three"},
}
var res []string
Chain(arr).OrderBy("id").Map(func(r testModel, _ int) string {
return r.Name
}).Value(&res)
assert.Len(t, res, 3)
assert.EqualValues(
t,
res,
[]string{"one", "two", "three"},
)
}