forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_subdoc_test.go
424 lines (353 loc) · 10.4 KB
/
collection_subdoc_test.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package gocb
import (
"errors"
"strings"
"testing"
)
func TestInsertLookupIn(t *testing.T) {
type beerWithCountable struct {
testBeerDocument
Countable []string `json:"countable"`
}
var doc beerWithCountable
err := loadJSONTestDataset("beer_sample_single", &doc.testBeerDocument)
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
doc.Countable = []string{"one", "two"}
mutRes, err := globalCollection.Insert("lookupDoc", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error was %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
result, err := globalCollection.LookupIn("lookupDoc", []LookupInSpec{
GetSpec("name", nil),
GetSpec("description", nil),
ExistsSpec("doesnt", nil),
ExistsSpec("style", nil),
GetSpec("doesntexist", nil),
CountSpec("countable", nil),
}, nil)
if err != nil {
t.Fatalf("Get failed, error was %v", err)
}
if result.Exists(2) {
t.Fatalf("Expected doesnt field to not exist")
}
if !result.Exists(3) {
t.Fatalf("Expected style field to exist")
}
var name string
err = result.ContentAt(0, &name)
if err != nil {
t.Fatalf("Failed to get name from LookupInResult, %v", err)
}
if name != doc.Name {
t.Fatalf("Expected name to be %s but was %s", doc.Name, name)
}
var desc string
err = result.ContentAt(1, &desc)
if err != nil {
t.Fatalf("Failed to get description from LookupInResult, %v", err)
}
if desc != doc.Description {
t.Fatalf("Expected description to be %s but was %s", doc.Description, desc)
}
var idontexist string
err = result.ContentAt(4, &idontexist)
if err == nil {
t.Fatalf("Expected lookup on a non existent field to return error")
}
if !errors.Is(err, ErrPathNotFound) {
t.Fatalf("Expected error to be path not found but was %+v", err)
}
var count int
err = result.ContentAt(5, &count)
if err != nil {
t.Fatalf("Failed to get count from LookupInResult, %v", err)
}
if count != 2 {
t.Fatalf("LookupIn Result count should have be 2 but was %d", count)
}
}
func TestMutateInBasicCrud(t *testing.T) {
var doc testBeerDocument
err := loadJSONTestDataset("beer_sample_single", &doc)
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
mutRes, err := globalCollection.Insert("mutateIn", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error: %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
fishName := "blobfish"
newName := "fishy beer"
newStyle := "fishy"
subRes, err := globalCollection.MutateIn("mutateIn", []MutateInSpec{
InsertSpec("fish", fishName, nil),
UpsertSpec("name", newName, nil),
UpsertSpec("newName", newName, nil),
ReplaceSpec("style", newStyle, nil),
RemoveSpec("type", nil),
}, nil)
if err != nil {
t.Fatalf("MutateIn failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
getRes, err := globalCollection.Get("mutateIn", nil)
if err != nil {
t.Fatalf("Getting document errored: %v", err)
}
type fishBeerDocument struct {
testBeerDocument
NewName string `json:"newName"`
Fish string `json:"fish"`
}
var actualDoc fishBeerDocument
err = getRes.Content(&actualDoc)
if err != nil {
t.Fatalf("Getting content errored: %v", err)
}
expectedDoc := fishBeerDocument{
testBeerDocument: doc,
NewName: newName,
Fish: fishName,
}
expectedDoc.Name = newName
expectedDoc.Style = newStyle
expectedDoc.Type = ""
if actualDoc != expectedDoc {
t.Fatalf("results did not match, expected %#v but was %#v", expectedDoc, actualDoc)
}
}
func TestMutateInBasicArray(t *testing.T) {
doc := struct {
Fish []string `json:"array"`
}{
[]string{},
}
mutRes, err := globalCollection.Insert("mutateInArray", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error: %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
subRes, err := globalCollection.MutateIn("mutateInArray", []MutateInSpec{
ArrayAppendSpec("array", "clownfish", nil),
ArrayPrependSpec("array", "whaleshark", nil),
ArrayInsertSpec("array[1]", "catfish", nil),
ArrayAppendSpec("array", []string{"manta ray", "stingray"}, &ArrayAppendSpecOptions{HasMultiple: true}),
ArrayPrependSpec("array", []string{"carp", "goldfish"}, &ArrayPrependSpecOptions{HasMultiple: true}),
ArrayInsertSpec("array[1]", []string{"eel", "stonefish"}, &ArrayInsertSpecOptions{HasMultiple: true}),
}, nil)
if err != nil {
t.Fatalf("MutateIn failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
getRes, err := globalCollection.Get("mutateInArray", nil)
if err != nil {
t.Fatalf("Getting document errored: %v", err)
}
type fishBeerDocument struct {
Fish []string `json:"array"`
}
var actualDoc fishBeerDocument
err = getRes.Content(&actualDoc)
if err != nil {
t.Fatalf("Getting content errored: %v", err)
}
expectedDoc := fishBeerDocument{
Fish: []string{"carp", "eel", "stonefish", "goldfish", "whaleshark", "catfish", "clownfish", "manta ray", "stingray"},
}
if len(expectedDoc.Fish) != len(actualDoc.Fish) {
t.Fatalf("results did not match, expected %v but was %v", expectedDoc, actualDoc)
}
for i, fish := range expectedDoc.Fish {
if fish != actualDoc.Fish[i] {
t.Fatalf("results did not match, expected %s at index %d but was %s", fish, i, actualDoc.Fish[i])
}
}
}
func TestMutateInLookupInXattr(t *testing.T) {
if !globalCluster.SupportsFeature(XattrFeature) {
t.Skip("Skipping test as xattrs not supported")
}
var doc testBeerDocument
err := loadJSONTestDataset("beer_sample_single", &doc)
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
mutRes, err := globalCollection.Insert("mutateInFullInsertInsertXattr", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error: %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
fishName := "flounder"
doc.Name = "namename"
subRes, err := globalCollection.MutateIn("mutateInFullInsertInsertXattr", []MutateInSpec{
InsertSpec("fish", fishName, &InsertSpecOptions{IsXattr: true}),
ReplaceSpec("", doc, nil),
}, nil)
if err != nil {
t.Fatalf("MutateIn failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("MutateIn CAS was 0")
}
result, err := globalCollection.LookupIn("mutateInFullInsertInsertXattr", []LookupInSpec{
GetSpec("fish", &GetSpecOptions{IsXattr: true}),
GetSpec("name", nil),
}, nil)
if err != nil {
t.Fatalf("Get failed, error was %v", err)
}
var fish string
err = result.ContentAt(0, &fish)
if err != nil {
t.Fatalf("Failed to get name from LookupInResult, %v", err)
}
if fish != fishName {
t.Fatalf("Expected fish to be %s but was %s", fishName, fish)
}
var name string
err = result.ContentAt(1, &name)
if err != nil {
t.Fatalf("Failed to get name from LookupInResult, %v", err)
}
if name != doc.Name {
t.Fatalf("Expected name to be %s but was %s", doc.Name, name)
}
}
func TestInsertLookupInInsertGetFull(t *testing.T) {
if !globalCluster.SupportsFeature(XattrFeature) {
t.Skip("Skipping test as xattrs not supported")
}
var doc testBeerDocument
err := loadJSONTestDataset("beer_sample_single", &doc)
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
subRes, err := globalCollection.MutateIn("lookupDocGetFull", []MutateInSpec{
InsertSpec("xattrpath", "xattrvalue", &InsertSpecOptions{IsXattr: true}),
ReplaceSpec("", doc, nil),
}, &MutateInOptions{StoreSemantic: StoreSemanticsUpsert, Expiry: 20})
if err != nil {
t.Fatalf("MutateIn failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("MutateIn CAS was 0")
}
result, err := globalCollection.LookupIn("lookupDocGetFull", []LookupInSpec{
GetSpec("$document.exptime", &GetSpecOptions{IsXattr: true}),
GetSpec("", nil),
}, nil)
if err != nil {
t.Fatalf("Get failed, error was %v", err)
}
var exptime int
err = result.ContentAt(0, &exptime)
if err != nil {
t.Fatalf("Failed to get expiry from LookupInResult, %v", err)
}
if exptime == 0 {
t.Fatalf("Expected expiry to be non zero")
}
var actualDoc testBeerDocument
err = result.ContentAt(1, &actualDoc)
if err != nil {
t.Fatalf("Failed to get name from LookupInResult, %v", err)
}
if actualDoc != doc {
t.Fatalf("Expected doc to be %v but was %v", doc, actualDoc)
}
}
func TestMutateInLookupInCounters(t *testing.T) {
doc := struct {
Counter int `json:"counter"`
}{
Counter: 20,
}
mutRes, err := globalCollection.Insert("mutateInLookupInCounters", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error: %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
subRes, err := globalCollection.MutateIn("mutateInLookupInCounters", []MutateInSpec{
IncrementSpec("counter", 10, nil),
DecrementSpec("counter", 5, nil),
}, nil)
if err != nil {
t.Fatalf("Increment failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
result, err := globalCollection.LookupIn("mutateInLookupInCounters", []LookupInSpec{
GetSpec("counter", nil),
}, nil)
if err != nil {
t.Fatalf("Get failed, error was %v", err)
}
var counter int
err = result.ContentAt(0, &counter)
if err != nil {
t.Fatalf("Failed to get counter from LookupInResult, %v", err)
}
if counter != 25 {
t.Fatalf("Expected counter to be 25 but was %d", counter)
}
}
func TestMutateInLookupInMacro(t *testing.T) {
if !globalCluster.SupportsFeature(ExpandMacrosFeature) {
t.Skip("Skipping test as macros not supported")
}
var doc testBeerDocument
err := loadJSONTestDataset("beer_sample_single", &doc)
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
mutRes, err := globalCollection.Insert("mutateInInsertMacro", doc, nil)
if err != nil {
t.Fatalf("Insert failed, error: %v", err)
}
if mutRes.Cas() == 0 {
t.Fatalf("Insert CAS was 0")
}
subRes, err := globalCollection.MutateIn("mutateInInsertMacro", []MutateInSpec{
InsertSpec("caspath", MutationMacroCAS, nil),
}, nil)
if err != nil {
t.Fatalf("MutateIn failed, error was %v", err)
}
if subRes.Cas() == 0 {
t.Fatalf("MutateIn CAS was 0")
}
result, err := globalCollection.LookupIn("mutateInInsertMacro", []LookupInSpec{
GetSpec("caspath", &GetSpecOptions{IsXattr: true}),
}, nil)
if err != nil {
t.Fatalf("Get failed, error was %v", err)
}
var caspath string
err = result.ContentAt(0, &caspath)
if err != nil {
t.Fatalf("Failed to get caspath from LookupInResult, %v", err)
}
if !strings.HasPrefix(caspath, "0x") {
t.Fatalf("Expected caspath to start with 0x but was %s", caspath)
}
}