Skip to content

Commit 53202af

Browse files
committed
add grouping functions tests
1 parent 76f5190 commit 53202af

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

funcs_grouping_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package handy
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"testing"
7+
)
8+
9+
func TestJoin(t *testing.T) {
10+
type student struct {
11+
ID string
12+
Name string
13+
}
14+
type studentMajor struct {
15+
ID string
16+
Major string
17+
}
18+
19+
students := NewIterableFromSlice([]student{
20+
{"1", "Alice"},
21+
{"2", "Bob"},
22+
{"3", "Cindy"},
23+
})
24+
majors := NewIterableFromSlice([]studentMajor{
25+
{"1", "Math"},
26+
{"1", "Physics"},
27+
{"2", "English"},
28+
})
29+
e := Join(students, majors, func(s student) string {
30+
return s.ID
31+
}, func(m studentMajor) string {
32+
return m.ID
33+
})
34+
type res struct {
35+
ID string
36+
Name string
37+
Major string
38+
}
39+
actual := []res{}
40+
for v := range e.Iter() {
41+
actual = append(actual, res{
42+
ID: v.Outer.ID,
43+
Name: v.Outer.Name,
44+
Major: v.Inner.Major,
45+
})
46+
}
47+
expect := []res{
48+
{"1", "Alice", "Math"},
49+
{"1", "Alice", "Physics"},
50+
{"2", "Bob", "English"},
51+
}
52+
if !slices.Equal(actual, expect) {
53+
t.Fatalf("test Join, expect: %v, actual: %v", expect, actual)
54+
}
55+
}
56+
57+
func TestJoinAs(t *testing.T) {
58+
type student struct {
59+
ID string
60+
Name string
61+
}
62+
type studentMajor struct {
63+
ID string
64+
Major string
65+
}
66+
67+
type res struct {
68+
ID string
69+
Name string
70+
Major string
71+
}
72+
students := NewIterableFromSlice([]student{
73+
{"1", "Alice"},
74+
{"2", "Bob"},
75+
{"3", "Cindy"},
76+
})
77+
majors := NewIterableFromSlice([]studentMajor{
78+
{"1", "Math"},
79+
{"1", "Physics"},
80+
{"2", "English"},
81+
})
82+
e := JoinAs(students, majors, func(s student) string {
83+
return s.ID
84+
}, func(m studentMajor) string {
85+
return m.ID
86+
}, func(outer student, inner studentMajor) res {
87+
return res{
88+
ID: outer.ID,
89+
Name: outer.Name,
90+
Major: inner.Major,
91+
}
92+
})
93+
actual := []res{}
94+
for v := range e.Iter() {
95+
actual = append(actual, v)
96+
if v.ID == "2" {
97+
break
98+
}
99+
}
100+
expect := []res{
101+
{"1", "Alice", "Math"},
102+
{"1", "Alice", "Physics"},
103+
{"2", "Bob", "English"},
104+
}
105+
if !slices.Equal(actual, expect) {
106+
t.Fatalf("test JoinAs, expect: %v, actual: %v", expect, actual)
107+
}
108+
}
109+
110+
func TestGroupJoin(t *testing.T) {
111+
type student struct {
112+
ID string
113+
Name string
114+
}
115+
type studentMajor struct {
116+
ID string
117+
Major string
118+
}
119+
120+
students := NewIterableFromSlice([]student{
121+
{"1", "Alice"},
122+
{"2", "Bob"},
123+
{"3", "Cindy"},
124+
})
125+
majors := NewIterableFromSlice([]studentMajor{
126+
{"1", "Math"},
127+
{"1", "Physics"},
128+
{"2", "English"},
129+
})
130+
e := GroupJoin(students, majors, func(s student) string {
131+
return s.ID
132+
}, func(m studentMajor) string {
133+
return m.ID
134+
})
135+
type res struct {
136+
ID string
137+
Name string
138+
Majors string
139+
}
140+
actual := []res{}
141+
for v := range e.Iter() {
142+
r := res{
143+
ID: v.Outer.ID,
144+
Name: v.Outer.Name,
145+
}
146+
for s := range v.Inner.Iter() {
147+
if r.Majors != "" {
148+
r.Majors += fmt.Sprintf(",%s", s.Major)
149+
} else {
150+
r.Majors = s.Major
151+
}
152+
}
153+
actual = append(actual, r)
154+
}
155+
expect := []res{
156+
{"1", "Alice", "Math,Physics"},
157+
{"2", "Bob", "English"},
158+
}
159+
if !slices.Equal(actual, expect) {
160+
t.Fatalf("test Join, expect: %v, actual: %v", expect, actual)
161+
}
162+
}

0 commit comments

Comments
 (0)