Debian Codegraph @

execution/execute_group_by_test.go

raw symbols SCIP
1 package execution_test
2
3 import (
4 "context"
5 "strings"
6 "testing"
7
8 "github.com/tomwright/dasel/v3/execution"
9 "github.com/tomwright/dasel/v3/model"
10 )
11
12 func TestGroupBy(t *testing.T) {
13 t.Run("group by $this strings", testCase{
14 inFn: func() *model.Value {
15 res := model.NewSliceValue()
16 _ = res.Append(model.NewStringValue("a"))
17 _ = res.Append(model.NewStringValue("b"))
18 _ = res.Append(model.NewStringValue("a"))
19 _ = res.Append(model.NewStringValue("c"))
20 _ = res.Append(model.NewStringValue("b"))
21 return res
22 },
23 s: `groupBy($this)`,
24 outFn: func() *model.Value {
25 res := model.NewMapValue()
26
27 groupA := model.NewSliceValue()
28 _ = groupA.Append(model.NewStringValue("a"))
29 _ = groupA.Append(model.NewStringValue("a"))
30 _ = res.SetMapKey("a", groupA)
31
32 groupB := model.NewSliceValue()
33 _ = groupB.Append(model.NewStringValue("b"))
34 _ = groupB.Append(model.NewStringValue("b"))
35 _ = res.SetMapKey("b", groupB)
36
37 groupC := model.NewSliceValue()
38 _ = groupC.Append(model.NewStringValue("c"))
39 _ = res.SetMapKey("c", groupC)
40
41 return res
42 },
43 }.run)
44
45 t.Run("group by $this ints", testCase{
46 inFn: func() *model.Value {
47 res := model.NewSliceValue()
48 _ = res.Append(model.NewIntValue(1))
49 _ = res.Append(model.NewIntValue(2))
50 _ = res.Append(model.NewIntValue(1))
51 _ = res.Append(model.NewIntValue(2))
52 _ = res.Append(model.NewIntValue(3))
53 return res
54 },
55 s: `groupBy($this)`,
56 outFn: func() *model.Value {
57 res := model.NewMapValue()
58
59 group1 := model.NewSliceValue()
60 _ = group1.Append(model.NewIntValue(1))
61 _ = group1.Append(model.NewIntValue(1))
62 _ = res.SetMapKey("1", group1)
63
64 group2 := model.NewSliceValue()
65 _ = group2.Append(model.NewIntValue(2))
66 _ = group2.Append(model.NewIntValue(2))
67 _ = res.SetMapKey("2", group2)
68
69 group3 := model.NewSliceValue()
70 _ = group3.Append(model.NewIntValue(3))
71 _ = res.SetMapKey("3", group3)
72
73 return res
74 },
75 }.run)
76
77 t.Run("group by field name", testCase{
78 inFn: func() *model.Value {
79 res := model.NewSliceValue()
80
81 alice := model.NewMapValue()
82 _ = alice.SetMapKey("name", model.NewStringValue("Alice"))
83 _ = alice.SetMapKey("dept", model.NewStringValue("eng"))
84 _ = res.Append(alice)
85
86 bob := model.NewMapValue()
87 _ = bob.SetMapKey("name", model.NewStringValue("Bob"))
88 _ = bob.SetMapKey("dept", model.NewStringValue("eng"))
89 _ = res.Append(bob)
90
91 carol := model.NewMapValue()
92 _ = carol.SetMapKey("name", model.NewStringValue("Carol"))
93 _ = carol.SetMapKey("dept", model.NewStringValue("sales"))
94 _ = res.Append(carol)
95
96 return res
97 },
98 s: `groupBy(dept)`,
99 outFn: func() *model.Value {
100 res := model.NewMapValue()
101
102 alice := model.NewMapValue()
103 _ = alice.SetMapKey("name", model.NewStringValue("Alice"))
104 _ = alice.SetMapKey("dept", model.NewStringValue("eng"))
105
106 bob := model.NewMapValue()
107 _ = bob.SetMapKey("name", model.NewStringValue("Bob"))
108 _ = bob.SetMapKey("dept", model.NewStringValue("eng"))
109
110 carol := model.NewMapValue()
111 _ = carol.SetMapKey("name", model.NewStringValue("Carol"))
112 _ = carol.SetMapKey("dept", model.NewStringValue("sales"))
113
114 engGroup := model.NewSliceValue()
115 _ = engGroup.Append(alice)
116 _ = engGroup.Append(bob)
117 _ = res.SetMapKey("eng", engGroup)
118
119 salesGroup := model.NewSliceValue()
120 _ = salesGroup.Append(carol)
121 _ = res.SetMapKey("sales", salesGroup)
122
123 return res
124 },
125 }.run)
126
127 t.Run("group by boolean expression", testCase{
128 inFn: func() *model.Value {
129 res := model.NewSliceValue()
130 _ = res.Append(model.NewIntValue(3))
131 _ = res.Append(model.NewIntValue(7))
132 _ = res.Append(model.NewIntValue(1))
133 _ = res.Append(model.NewIntValue(8))
134 _ = res.Append(model.NewIntValue(5))
135 return res
136 },
137 s: `groupBy($this > 5)`,
138 outFn: func() *model.Value {
139 res := model.NewMapValue()
140
141 falseGroup := model.NewSliceValue()
142 _ = falseGroup.Append(model.NewIntValue(3))
143 _ = falseGroup.Append(model.NewIntValue(1))
144 _ = falseGroup.Append(model.NewIntValue(5))
145 _ = res.SetMapKey("false", falseGroup)
146
147 trueGroup := model.NewSliceValue()
148 _ = trueGroup.Append(model.NewIntValue(7))
149 _ = trueGroup.Append(model.NewIntValue(8))
150 _ = res.SetMapKey("true", trueGroup)
151
152 return res
153 },
154 }.run)
155
156 t.Run("empty array", testCase{
157 inFn: func() *model.Value {
158 return model.NewSliceValue()
159 },
160 s: `groupBy($this)`,
161 outFn: func() *model.Value {
162 return model.NewMapValue()
163 },
164 }.run)
165
166 t.Run("group by $this floats", testCase{
167 inFn: func() *model.Value {
168 res := model.NewSliceValue()
169 _ = res.Append(model.NewFloatValue(1.5))
170 _ = res.Append(model.NewFloatValue(2.5))
171 _ = res.Append(model.NewFloatValue(1.5))
172 return res
173 },
174 s: `groupBy($this)`,
175 outFn: func() *model.Value {
176 res := model.NewMapValue()
177
178 group1 := model.NewSliceValue()
179 _ = group1.Append(model.NewFloatValue(1.5))
180 _ = group1.Append(model.NewFloatValue(1.5))
181 _ = res.SetMapKey("1.5", group1)
182
183 group2 := model.NewSliceValue()
184 _ = group2.Append(model.NewFloatValue(2.5))
185 _ = res.SetMapKey("2.5", group2)
186
187 return res
188 },
189 }.run)
190
191 t.Run("single element", testCase{
192 inFn: func() *model.Value {
193 res := model.NewSliceValue()
194 _ = res.Append(model.NewStringValue("only"))
195 return res
196 },
197 s: `groupBy($this)`,
198 outFn: func() *model.Value {
199 res := model.NewMapValue()
200 group := model.NewSliceValue()
201 _ = group.Append(model.NewStringValue("only"))
202 _ = res.SetMapKey("only", group)
203 return res
204 },
205 }.run)
206
207 t.Run("chaining after groupBy", testCase{
208 inFn: func() *model.Value {
209 res := model.NewSliceValue()
210
211 alice := model.NewMapValue()
212 _ = alice.SetMapKey("name", model.NewStringValue("Alice"))
213 _ = alice.SetMapKey("dept", model.NewStringValue("eng"))
214 _ = res.Append(alice)
215
216 bob := model.NewMapValue()
217 _ = bob.SetMapKey("name", model.NewStringValue("Bob"))
218 _ = bob.SetMapKey("dept", model.NewStringValue("eng"))
219 _ = res.Append(bob)
220
221 carol := model.NewMapValue()
222 _ = carol.SetMapKey("name", model.NewStringValue("Carol"))
223 _ = carol.SetMapKey("dept", model.NewStringValue("sales"))
224 _ = res.Append(carol)
225
226 return res
227 },
228 s: `groupBy(dept).eng`,
229 outFn: func() *model.Value {
230 alice := model.NewMapValue()
231 _ = alice.SetMapKey("name", model.NewStringValue("Alice"))
232 _ = alice.SetMapKey("dept", model.NewStringValue("eng"))
233
234 bob := model.NewMapValue()
235 _ = bob.SetMapKey("name", model.NewStringValue("Bob"))
236 _ = bob.SetMapKey("dept", model.NewStringValue("eng"))
237
238 res := model.NewSliceValue()
239 _ = res.Append(alice)
240 _ = res.Append(bob)
241 return res
242 },
243 }.run)
244
245 t.Run("group by $key", testCase{
246 inFn: func() *model.Value {
247 res := model.NewSliceValue()
248 _ = res.Append(model.NewStringValue("a"))
249 _ = res.Append(model.NewStringValue("b"))
250 _ = res.Append(model.NewStringValue("c"))
251 return res
252 },
253 s: `groupBy($key)`,
254 outFn: func() *model.Value {
255 res := model.NewMapValue()
256 s0 := model.NewSliceValue()
257 _ = s0.Append(model.NewStringValue("a"))
258 _ = res.SetMapKey("0", s0)
259 s1 := model.NewSliceValue()
260 _ = s1.Append(model.NewStringValue("b"))
261 _ = res.SetMapKey("1", s1)
262 s2 := model.NewSliceValue()
263 _ = s2.Append(model.NewStringValue("c"))
264 _ = res.SetMapKey("2", s2)
265 return res
266 },
267 }.run)
268
269 t.Run("non-slice input errors", func(t *testing.T) {
270 in := model.NewStringValue("not a slice")
271 _, err := execution.ExecuteSelector(context.Background(), `groupBy($this)`, in, execution.NewOptions())
272 if err == nil {
273 t.Fatal("expected error for non-slice input")
274 }
275 if !strings.Contains(err.Error(), "cannot group by on non-slice data") {
276 t.Fatalf("unexpected error: %s", err)
277 }
278 })
279 }