Debian Codegraph @

docstore/driver/util_test.go

raw
1 // Copyright 2019 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package driver
16
17 import (
18 "fmt"
19 "testing"
20
21 "github.com/google/go-cmp/cmp"
22 "github.com/google/go-cmp/cmp/cmpopts"
23 )
24
25 func TestSplitActions(t *testing.T) {
26 in := []*Action{
27 {Kind: Get},
28 {Kind: Get},
29 {Kind: Put},
30 {Kind: Update},
31 {Kind: Get},
32 {Kind: Create},
33 }
34
35 for _, test := range []struct {
36 desc string
37 split func(a, b *Action) bool
38 want [][]*Action
39 }{
40 {
41 "always false",
42 func(a, b *Action) bool { return false },
43 [][]*Action{in},
44 },
45 {
46 "always true",
47 func(a, b *Action) bool { return true },
48 [][]*Action{
49 {{Kind: Get}},
50 {{Kind: Get}},
51 {{Kind: Put}},
52 {{Kind: Update}},
53 {{Kind: Get}},
54 {{Kind: Create}},
55 },
56 },
57 {
58 "Get vs. other",
59 func(a, b *Action) bool { return (a.Kind == Get) != (b.Kind == Get) },
60 [][]*Action{
61 {{Kind: Get}, {Kind: Get}},
62 {{Kind: Put}, {Kind: Update}},
63 {{Kind: Get}},
64 {{Kind: Create}},
65 },
66 },
67 } {
68 got := SplitActions(in, test.split)
69 if diff := cmp.Diff(got, test.want, cmpopts.IgnoreFields(Action{}, "Doc")); diff != "" {
70 t.Errorf("%s: %s", test.desc, diff)
71 }
72 }
73 }
74
75 func TestGroupActions(t *testing.T) {
76 for _, test := range []struct {
77 in []*Action
78 want [][]int // in the same order as the return args
79 }{
80 {
81 in: []*Action{{Kind: Get, Key: 1}},
82 want: [][]int{nil, {0}, nil, nil, nil},
83 },
84 {
85 in: []*Action{
86 {Kind: Get, Key: 1},
87 {Kind: Get, Key: 3},
88 {Kind: Put, Key: 1},
89 {Kind: Replace, Key: 2},
90 {Kind: Get, Key: 2},
91 },
92 want: [][]int{{0}, {1}, {2, 3}, nil, {4}},
93 },
94 {
95 in: []*Action{{Kind: Create}, {Kind: Create}, {Kind: Create}},
96 want: [][]int{nil, nil, {0, 1, 2}, nil, nil},
97 },
98 {
99 in: []*Action{
100 {Kind: Get, Key: 1},
101 {Kind: Get, Key: 3},
102 {Kind: Put, Key: 1, InAtomicWrite: true},
103 {Kind: Replace, Key: 2, InAtomicWrite: true},
104 {Kind: Get, Key: 2},
105 },
106 want: [][]int{{0}, {1}, nil, {2, 3}, {4}},
107 },
108 {
109 in: []*Action{
110 {Kind: Get, Key: 1},
111 {Kind: Get, Key: 3},
112 {Kind: Put, Key: 1, InAtomicWrite: true},
113 {Kind: Replace, Key: 2},
114 {Kind: Put, Key: 2, InAtomicWrite: true},
115 {Kind: Get, Key: 2},
116 },
117 want: [][]int{{0}, {1}, {3}, {2, 4}, {5}},
118 },
119 {
120 in: []*Action{
121 {Kind: Get, Key: 1},
122 {Kind: Get, Key: 3},
123 {Kind: Put, Key: 1, InAtomicWrite: true},
124 {Kind: Replace, Key: 2, InAtomicWrite: true},
125 {Kind: Put, Key: 2, InAtomicWrite: true},
126 {Kind: Get, Key: 2},
127 },
128 want: [][]int{{0}, {1}, nil, {2, 4}, {5}},
129 },
130 } {
131 got := make([][]*Action, 5)
132 got[0], got[1], got[2], got[3], got[4] = GroupActions(test.in)
133 want := make([][]*Action, 5)
134 for i, s := range test.want {
135 for _, x := range s {
136 want[i] = append(want[i], test.in[x])
137 }
138 }
139 diff := cmp.Diff(got, want,
140 cmpopts.IgnoreUnexported(Document{}),
141 cmpopts.SortSlices(func(a1, a2 *Action) bool {
142 if a1.Kind != a2.Kind {
143 return a1.Kind < a2.Kind
144 }
145 a1k, _ := a1.Key.(int)
146 a2k, _ := a2.Key.(int)
147 return a1k < a2k
148 }))
149 if diff != "" {
150 t.Errorf("%v: %s", test.in, diff)
151 }
152 }
153 }
154
155 func (a *Action) String() string { // for TestGroupActions
156 return fmt.Sprintf("<%s %v>", a.Kind, a.Key)
157 }
158
159 func TestAsFunc(t *testing.T) {
160 x := 1
161 as := AsFunc(x)
162
163 var y int
164 if !as(&y) || y != 1 {
165 t.Errorf("*int: returned false or wrong value %d", y)
166 }
167
168 var z float64
169 for _, arg := range []any{nil, y, &z} {
170 if as(arg) {
171 t.Errorf("%#v: got true, want false", arg)
172 }
173 }
174 }
175
176 func TestGroupByFieldPath(t *testing.T) {
177 for i, test := range []struct {
178 in []*Action
179 want [][]int // indexes into test.in
180 }{
181 {
182 in: []*Action{{Index: 0}, {Index: 1}, {Index: 2}},
183 want: [][]int{{0, 1, 2}},
184 },
185 {
186 in: []*Action{{Index: 0}, {Index: 1, FieldPaths: [][]string{{"a"}}}, {Index: 2}},
187 want: [][]int{{0, 2}, {1}},
188 },
189 {
190 in: []*Action{
191 {Index: 0, FieldPaths: [][]string{{"a", "b"}}},
192 {Index: 1, FieldPaths: [][]string{{"a"}}},
193 {Index: 2},
194 {Index: 3, FieldPaths: [][]string{{"a"}, {"b"}}},
195 },
196 want: [][]int{{0}, {1}, {2}, {3}},
197 },
198 } {
199 got := GroupByFieldPath(test.in)
200 want := make([][]*Action, len(test.want))
201 for i, s := range test.want {
202 want[i] = make([]*Action, len(s))
203 for j, x := range s {
204 want[i][j] = test.in[x]
205 }
206 }
207 if diff := cmp.Diff(got, want, cmpopts.IgnoreUnexported(Document{})); diff != "" {
208 t.Errorf("#%d: %s", i, diff)
209 }
210 }
211 }