Debian Codegraph @

execution/execute_search_test.go

raw symbols SCIP
1 package execution_test
2
3 import (
4 "testing"
5
6 "github.com/tomwright/dasel/v3/model"
7 "github.com/tomwright/dasel/v3/model/orderedmap"
8 )
9
10 func TestSearch(t *testing.T) {
11 t.Run("search has key in nested map", testCase{
12 inFn: func() *model.Value {
13 return model.NewValue(
14 orderedmap.NewMap().
15 Set("a", orderedmap.NewMap().
16 Set("x", 1)).
17 Set("b", orderedmap.NewMap().
18 Set("y", 2)),
19 )
20 },
21 s: `search(has("x"))`,
22 outFn: func() *model.Value {
23 s := model.NewSliceValue()
24 _ = s.Append(model.NewValue(orderedmap.NewMap().Set("x", 1)))
25 return s
26 },
27 }.run)
28
29 t.Run("search on array of objects", testCase{
30 inFn: func() *model.Value {
31 return model.NewValue([]any{
32 orderedmap.NewMap().Set("name", "alice"),
33 orderedmap.NewMap().Set("name", "bob"),
34 orderedmap.NewMap().Set("name", "alice"),
35 })
36 },
37 s: `search(name == "alice")`,
38 outFn: func() *model.Value {
39 s := model.NewSliceValue()
40 _ = s.Append(model.NewValue(orderedmap.NewMap().Set("name", "alice")))
41 _ = s.Append(model.NewValue(orderedmap.NewMap().Set("name", "alice")))
42 return s
43 },
44 }.run)
45
46 t.Run("search with $key on map", testCase{
47 inFn: func() *model.Value {
48 m := model.NewMapValue()
49 _ = m.SetMapKey("other", model.NewIntValue(1))
50 _ = m.SetMapKey("target", model.NewIntValue(42))
51 return m
52 },
53 s: `search($key == "target")`,
54 outFn: func() *model.Value {
55 s := model.NewSliceValue()
56 _ = s.Append(model.NewIntValue(42))
57 return s
58 },
59 }.run)
60
61 t.Run("search with $key on slice", testCase{
62 inFn: func() *model.Value {
63 s := model.NewSliceValue()
64 _ = s.Append(model.NewIntValue(10))
65 _ = s.Append(model.NewIntValue(20))
66 _ = s.Append(model.NewIntValue(30))
67 return s
68 },
69 s: `search($key == 1)`,
70 outFn: func() *model.Value {
71 s := model.NewSliceValue()
72 _ = s.Append(model.NewIntValue(20))
73 return s
74 },
75 }.run)
76
77 t.Run("no matches returns empty", testCase{
78 inFn: func() *model.Value {
79 return model.NewValue([]any{
80 orderedmap.NewMap().Set("name", "alice"),
81 orderedmap.NewMap().Set("name", "bob"),
82 })
83 },
84 s: `search(name == "charlie")`,
85 outFn: func() *model.Value {
86 return model.NewSliceValue()
87 },
88 }.run)
89 }