-
Notifications
You must be signed in to change notification settings - Fork 0
/
linq_test.go
118 lines (110 loc) · 2.98 KB
/
linq_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package linq
import (
"fmt"
"testing"
)
type BMember struct {
Name string
ID int64
Age int
Sex int8
}
type SMember struct {
Name string
ID int64
}
var members = []*BMember{
{ID: 1, Name: "张三", Sex: 1, Age: 28},
{ID: 2, Name: "李四", Sex: 2, Age: 28},
{ID: 3, Name: "王五", Sex: 1, Age: 29},
{ID: 4, Name: "老六", Sex: 2, Age: 29},
}
func TestSum(t *testing.T) {
out := From(members).SumIntBy(func(m *BMember) int64 { return int64(m.Sex) })
fmt.Printf("%+v \n", out)
out2 := MaxBy(From(members), func(m *BMember) int8 { return m.Sex })
fmt.Printf("%+v \n", out2)
out3 := MinBy(From(members), func(m *BMember) int8 { return m.Sex })
fmt.Printf("%+v \n", out3)
out4 := From(members).AvgBy(func(m *BMember) float64 { return float64(m.Sex) })
fmt.Printf("%+v \n", out4)
}
func TestPage(t *testing.T) {
page, pageSize := 1, 3
out1 := From(members).Skip((page - 1) * pageSize).Take(pageSize).ToSlice()
for _, v := range out1 {
fmt.Printf("%d %+v \n", page, v)
}
page = 2
out1 = From(members).Page(page, pageSize).ToSlice()
for _, v := range out1 {
fmt.Printf("%d %+v \n", page, v)
}
}
func TestUnion(t *testing.T) {
out := From(members).Union(From(members)).ToSlice()
for _, v := range out {
fmt.Printf("%+v \n", v)
}
}
func TestOrder(t *testing.T) {
query := From(members)
query = OrderByDescending(query, func(m *BMember) int8 { return m.Sex })
query = ThenBy(query, func(m *BMember) int { return m.Age })
out4 := query.ToSlice()
for _, v := range out4 {
fmt.Printf("%+v \n", v)
}
}
func TestFrom(t *testing.T) {
out := From(members).
Where(func(m *BMember) bool { return m.Age < 29 }).
Where(func(m *BMember) bool { return m.Sex < 29 }).
ToSlice()
for _, v := range out {
fmt.Printf("%+v \n", v)
}
out2 := Select(
From(out),
func(m *BMember) *SMember { return &SMember{ID: m.ID, Name: m.Name} },
).ToSlice()
for _, v := range out2 {
fmt.Printf("%+v \n", v)
}
out3 := GroupBy(
From(members),
func(m *BMember) int8 { return m.Sex },
).ToSlice()
for _, v := range out3 {
fmt.Printf("%+v \n", v)
}
out4 := GroupBySelect(
From(members),
func(m *BMember) int8 { return m.Sex },
func(m *BMember) *BMember { return m },
).ToSlice()
for _, v := range out4 {
fmt.Printf("%+v \n", v)
}
}
func TestFilter(t *testing.T) {
out2 := Filter(
From(members),
func(m *BMember) (*SMember, bool) { return nil, false },
).ToSlice()
for _, v := range out2 {
fmt.Printf("%+v \n", v)
}
}
func TestHasOrder(t *testing.T) {
query := From(members).
Where(func(m *BMember) bool { return m.Age < 29 }).
Where(func(m *BMember) bool { return m.Sex < 29 })
fmt.Printf("%+v \n", query.HasOrder())
query = OrderByDescending(query, func(m *BMember) int8 { return m.Sex })
fmt.Printf("%+v \n", query.HasOrder())
}
func TestFirst(t *testing.T) {
fmt.Println(1, From([]*BMember{}).Where(func(m *BMember) bool { return m.Age < 29 }).DefaultIfEmpty(&BMember{}).First())
fmt.Println(2, From([]*BMember{}).Where(func(m *BMember) bool { return m.Age < 29 }).First())
}