-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_multi.go
284 lines (268 loc) · 8.35 KB
/
test_multi.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package end2end
import (
"context"
"errors"
"fmt"
"github.com/dal-go/dalgo/dal"
"testing"
)
func deleteAllRecords(ctx context.Context, t *testing.T, db dal.DB, keys []*dal.Key) {
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.DeleteMulti(ctx, keys)
})
if err != nil {
t.Fatalf("failed at DeleteMulti(ctx, keys) for %v records: %v", len(keys), err)
}
}
func testMultiOperations(ctx context.Context, t *testing.T, db dal.DB) {
var k1r1Key = dal.NewKeyWithID(E2ETestKind1, "k1r1")
var k1r2Key = dal.NewKeyWithID(E2ETestKind1, "k1r2")
var k2r1Key = dal.NewKeyWithID(E2ETestKind2, "k2r1")
var allKeys = []*dal.Key{
k1r1Key,
k1r2Key,
k2r1Key,
}
t.Run("1st_initial_delete", func(t *testing.T) {
deleteAllRecords(ctx, t, db, allKeys)
})
t.Run("2nd_initial_delete", func(t *testing.T) {
deleteAllRecords(ctx, t, db, allKeys)
})
t.Run("get_3_non_existing_records", func(t *testing.T) {
get3NonExistingRecords(t, db)
})
t.Run("SetMulti", func(t *testing.T) {
setMulti(t, db, k1r1Key, k1r2Key, k2r1Key)
})
t.Run("GetMulti", func(t *testing.T) {
t.Run("3_existing_records", func(t *testing.T) {
getMulti3existingRecords(t, allKeys, db)
})
t.Run("2_existing_2_missing_records", func(t *testing.T) {
getMulti2existing2missingRecords(t, db, k1r1Key, k1r2Key)
})
})
t.Run("update_2_records", func(t *testing.T) {
update2records(t, db, k1r1Key, k1r2Key, k2r1Key)
})
t.Run("cleanup_delete", func(t *testing.T) {
cleanupDelete(t, db, allKeys)
})
}
func getMulti2existing2missingRecords(t *testing.T, db dal.DB, k1r1Key, k1r2Key *dal.Key) {
keys := []*dal.Key{
k1r1Key,
k1r2Key,
dal.NewKeyWithID(E2ETestKind1, "k1r9"),
dal.NewKeyWithID(E2ETestKind2, "k2r9"),
}
data := make([]TestData, len(keys))
records := make([]dal.Record, len(keys))
for i, key := range keys {
records[i] = dal.NewRecordWithData(key, &data[i])
}
ctx := context.Background()
if err := db.RunReadonlyTransaction(ctx, func(ctx context.Context, tx dal.ReadTransaction) error {
return tx.GetMulti(ctx, records)
}); err != nil {
t.Fatalf("failed to set multiple records at once: %v", err)
}
var hasErrors bool
hasErrors = recordsMustExist(t, records[:2]) > 0 || hasErrors
hasErrors = !recordsMustNotExist(t, records[2:]) || hasErrors
if hasErrors {
return
}
checkPropValue := func(i int, expected string) error {
if data[i].StringProp != expected {
t.Errorf("expected %v got %v, err: %v", expected, data[i].StringProp, records[i].Error())
}
return nil
}
if err := checkPropValue(0, "k1r1str"); err != nil {
t.Error(err)
}
if err := checkPropValue(1, "k1r2str"); err != nil {
t.Error(err)
}
for i := 2; i < 4; i++ {
if records[i].Exists() {
t.Errorf("record unexpectedly showing as existing, key: %v", records[i].Key())
}
}
}
func get3NonExistingRecords(t *testing.T, db dal.DB) {
records := make([]dal.Record, 3)
for i := 0; i < 3; i++ {
records[i] = dal.NewRecordWithData(
dal.NewKeyWithID("NonExistingKind", fmt.Sprintf("non_existing_id_%v", i)),
&TestData{},
)
}
ctx := context.Background()
if err := db.RunReadonlyTransaction(ctx, func(ctx context.Context, tx dal.ReadTransaction) error {
return tx.GetMulti(ctx, records)
}); err != nil {
t.Fatalf("failed to get multiple records at once: %v", err)
}
recordsMustNotExist(t, records)
}
func setMulti(t *testing.T, db dal.DB, k1r1Key, k1r2Key, k2r1Key *dal.Key) {
newRecord := func(key *dal.Key) dal.Record {
return dal.NewRecordWithData(key, &TestData{
StringProp: fmt.Sprintf("%vstr", key.ID),
})
}
records := []dal.Record{
newRecord(k1r1Key),
newRecord(k1r2Key),
newRecord(k2r1Key),
}
ctx := context.Background()
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.SetMulti(ctx, records)
})
if err != nil {
t.Fatalf("failed to set multiple records at once: %v", err)
}
}
func cleanupDelete(t *testing.T, db dal.DB, allKeys []*dal.Key) {
ctx := context.Background()
deleteAllRecords(ctx, t, db, allKeys)
data := make([]struct{}, len(allKeys))
records := make([]dal.Record, len(allKeys))
for i := range records {
records[i] = dal.NewRecordWithData(allKeys[i], &data[i])
}
if err := db.RunReadonlyTransaction(ctx, func(ctx context.Context, tx dal.ReadTransaction) error {
return tx.GetMulti(ctx, records)
}); err != nil {
t.Fatalf("failed to get multiple records at once: %v", err)
}
recordsMustNotExist(t, records)
}
func update2records(t *testing.T, db dal.DB, k1r1Key, k1r2Key, k2r1Key *dal.Key) {
const newValue = "UpdateD"
updates := []dal.Update{
{Field: "StringProp", Value: newValue},
}
newRecords := func() []dal.Record {
data := make([]*TestData, 3)
for i := range data {
data[i] = new(TestData)
}
return []dal.Record{
dal.NewRecordWithData(k1r1Key, data[0]),
dal.NewRecordWithData(k1r2Key, data[1]),
dal.NewRecordWithData(k2r1Key, data[2]),
}
}
ctx := context.Background()
err := db.RunReadwriteTransaction(ctx, func(ctx context.Context, tx dal.ReadwriteTransaction) error {
return tx.UpdateMulti(ctx, []*dal.Key{k1r1Key, k1r2Key}, updates)
})
if err != nil {
if errors.Is(err, dal.ErrNotSupported) {
t.Log(err)
return
}
t.Fatalf("failed to update 2 records at once: %v", err)
}
records := newRecords()
if err := db.RunReadonlyTransaction(ctx, func(ctx context.Context, tx dal.ReadTransaction) error {
return tx.GetMulti(ctx, records)
}); err != nil {
t.Fatalf("failed to get 3 records at once: %v", err)
}
if recordsMustExist(t, records) > 0 {
return
}
asserRecord := func(i int, expected string) {
if actual := records[i].Data().(*TestData).StringProp; actual != expected {
t.Errorf("record #%d expected to have StringProp as '%v' but got '%v', key: %v", i+1, expected, actual, records[i].Key())
}
}
asserRecord(0, newValue)
asserRecord(1, newValue)
asserRecord(2, "k2r1str")
}
func recordsMustExist(t *testing.T, records []dal.Record) (missingCount int) {
for _, record := range records {
if err := record.Error(); err != nil {
if !errors.Is(err, dal.NoError) {
t.Errorf("not able to check record for existence as it has unexpected error: %v", err)
missingCount++
}
}
if !record.Exists() {
t.Errorf("record was expected to exist, key: %v", record.Key())
missingCount++
}
}
return missingCount
}
func recordsMustNotExist(t *testing.T, records []dal.Record) (hasError bool) {
t.Helper()
for i, record := range records {
if err := record.Error(); err != nil {
hasError = true
t.Errorf("expected to get `%v` error for record with key=[%v] but got unexpected error: %v", dal.ErrRecordNotFound, record.Key(), err)
} else if record.Exists() {
hasError = true
t.Errorf("for record #%v of %v Exists() returned true, but expected false; key: %v",
i+1, len(records), record.Key())
}
}
return hasError
}
func getMulti3existingRecords(t *testing.T, allKeys []*dal.Key, db dal.DB) {
var data []TestData
records := make([]dal.Record, len(allKeys))
assetProps := func(t *testing.T) {
//if recordsMustExist(t, records) > 0 {
// return
//}
assertStringProp := func(i int, record dal.Record) {
id := record.Key().ID.(string)
if expected, actual := id+"str", data[i].StringProp; actual != expected {
t.Errorf("field StringProp was expected to have value '%v' got '%v'", expected, actual)
}
}
for i, record := range records {
assertStringProp(i, record)
}
}
t.Run("using_records_with_data", func(t *testing.T) {
data = make([]TestData, len(allKeys))
for i := range records {
records[i] = dal.NewRecordWithData(allKeys[i], &data[i])
}
ctx := context.Background()
if err := db.RunReadonlyTransaction(ctx, func(ctx context.Context, tx dal.ReadTransaction) error {
return tx.GetMulti(ctx, records)
}); err != nil {
t.Fatalf("failed to get multiple records at once: %v", err)
}
if recordsMustExist(t, records) > 0 {
return
}
assetProps(t)
})
//t.Run("using_DataTo", func(t *testing.T) {
// for i := range records {
// records[i] = dal.NewRecord(allKeys[i])
// }
// if err := db.GetMulti(ctx, records); err != nil {
// t.Fatalf("failed to get multiple records at once: %v", err)
// }
// recordsMustExist(t, records)
// data = make([]TestData, len(allKeys))
// for i, record := range records {
// if err := record.DataTo(&data[i]); err != nil {
// t.Fatalf("failed to record #%v", i+1)
// }
// }
// assetProps(t)
//})
}