-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
acceptance.go
894 lines (725 loc) · 26.7 KB
/
acceptance.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
package service
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/fulldump/apitest"
"github.com/fulldump/biff"
)
type JSON = map[string]interface{}
func Acceptance(a *biff.A, apiRequest func(method, path string) *apitest.Request) {
a.Alternative("Create collection", func(a *biff.A) {
resp := apiRequest("POST", "/collections").
WithBodyJson(JSON{
"name": "my-collection",
}).Do()
Save(resp, "Create collection", ``)
biff.AssertEqual(resp.StatusCode, http.StatusCreated)
expectedBody := JSON{
"name": "my-collection",
"total": 0,
"indexes": 0,
"defaults": map[string]any{"id": "uuid()"},
}
zzz := biff.AssertEqualJson(resp.BodyJson(), expectedBody)
if !zzz {
fmt.Println("JODERRRRRRR")
}
a.Alternative("Retrieve collection", func(a *biff.A) {
resp := apiRequest("GET", "/collections/my-collection").Do()
Save(resp, "Retrieve collection", ``)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
expectedBody := JSON{
"name": "my-collection",
"total": 0,
"indexes": 0,
"defaults": map[string]any{"id": "uuid()"},
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("List collections", func(a *biff.A) {
resp := apiRequest("GET", "/collections").Do()
Save(resp, "List collections", ``)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
expectedBody := []JSON{
{
"name": "my-collection",
"total": 0,
"indexes": 0,
"defaults": map[string]any{"id": "uuid()"},
},
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("Drop collection", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:dropCollection").
Do()
Save(resp, "Drop collection", ``)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
a.Alternative("Get dropped collection", func(a *biff.A) {
resp := apiRequest("GET", "/collections/my-collection").
Do()
Save(resp, "Get collection - not found", ``)
biff.AssertEqual(resp.StatusCode, http.StatusNotFound)
})
})
a.Alternative("Insert one operation", func(a *biff.A) {
myDocument := JSON{
"id": "my-id",
"name": "Fulanez",
"address": "Elm Street 11",
}
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
Save(resp, "Insert one", ``)
biff.AssertEqual(resp.StatusCode, http.StatusCreated)
expectedBody := map[string]any{
"address": "Elm Street 11",
"id": "my-id",
"name": "Fulanez",
}
biff.AssertEqual(resp.BodyJson(), expectedBody)
a.Alternative("Find with fullscan", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"mode": "fullscan",
"skip": 0,
"limit": 1,
"filter": JSON{
"name": "Fulanez",
},
}).Do()
Save(resp, "Find - fullscan", ``)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
biff.AssertEqual(resp.BodyJson(), myDocument)
})
})
a.Alternative("Insert many", func(a *biff.A) {
myDocuments := []JSON{
{"id": "1", "name": "Alfonso"},
{"id": "2", "name": "Gerardo"},
{"id": "3", "name": "Alfonso"},
}
body := ""
for _, myDocument := range myDocuments {
myDocument, _ := json.Marshal(myDocument)
body += string(myDocument) + "\n"
}
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyString(body).Do()
Save(resp, "Insert many", ``)
a.Alternative("Create index", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:createIndex").
WithBodyJson(JSON{"name": "my-index", "type": "map", "field": "id"}).Do()
Save(resp, "Create index", ``)
a.Alternative("Delete by index", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:remove").
WithBodyJson(JSON{
"index": "my-index",
"value": "2",
}).Do()
Save(resp, "Delete - by index", ``)
biff.AssertEqualJson(resp.BodyJson(), myDocuments[1])
biff.AssertEqual(resp.StatusCode, http.StatusOK)
})
a.Alternative("Patch by index", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:patch").
WithBodyJson(JSON{
"index": "my-index",
"value": "3",
"patch": JSON{
"name": "Pedro",
},
}).Do()
Save(resp, "Patch - by index", ``)
expectedBody := JSON{
"id": "3",
"name": "Pedro",
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
{
resp = apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{"limit": 10}).Do()
Save(resp, "Find - fullscan with limit 10", ``)
dec := json.NewDecoder(strings.NewReader(resp.BodyString()))
expectedDocuments := []JSON{
myDocuments[0],
myDocuments[1],
{"id": "3", "name": "Pedro"},
}
for _, expectedDocument := range expectedDocuments {
var bodyRow interface{}
dec.Decode(&bodyRow)
biff.AssertEqualJson(bodyRow, expectedDocument)
}
biff.AssertEqual(resp.StatusCode, http.StatusOK)
}
})
a.Alternative("Size", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:size").Do()
Save(resp, "Size - experimental", `
EXPERIMENTAL!!!
This will probably be removed, it is extremely inefficient.
`)
})
})
a.Alternative("Delete by fullscan", func(a *biff.A) {
{
resp := apiRequest("POST", "/collections/my-collection:remove").
WithBodyJson(JSON{
"limit": 10,
"filter": JSON{
"name": "Alfonso",
},
}).Do()
Save(resp, "Delete - fullscan", ``)
dec := json.NewDecoder(strings.NewReader(resp.BodyString()))
expectedDocuments := []JSON{
myDocuments[0],
myDocuments[2],
}
for _, expectedDocument := range expectedDocuments {
var bodyRow interface{}
dec.Decode(&bodyRow)
biff.AssertEqualJson(bodyRow, expectedDocument)
}
biff.AssertEqual(resp.StatusCode, http.StatusOK)
}
{
resp = apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{}).Do()
dec := json.NewDecoder(strings.NewReader(resp.BodyString()))
expectedDocuments := []JSON{
myDocuments[1],
}
for _, expectedDocument := range expectedDocuments {
var bodyRow interface{}
dec.Decode(&bodyRow)
biff.AssertEqualJson(bodyRow, expectedDocument)
}
biff.AssertEqual(resp.StatusCode, http.StatusOK)
}
})
a.Alternative("Patch by fullscan", func(a *biff.A) {
{
resp := apiRequest("POST", "/collections/my-collection:patch").
WithBodyJson(JSON{
"limit": 10,
"filter": JSON{
"name": "Alfonso",
},
"patch": JSON{
"country": "es",
},
}).Do()
Save(resp, "Patch - by fullscan", ``)
// biff.AssertEqual(resp.BodyString(), "") // todo: assert body
biff.AssertEqual(resp.StatusCode, http.StatusOK)
}
{
resp = apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{"limit": 10}).Do()
dec := json.NewDecoder(strings.NewReader(resp.BodyString()))
expectedDocuments := []JSON{
{"id": "1", "name": "Alfonso", "country": "es"},
myDocuments[1],
{"id": "3", "name": "Alfonso", "country": "es"},
}
for _, expectedDocument := range expectedDocuments {
var bodyRow interface{}
dec.Decode(&bodyRow)
biff.AssertEqualJson(bodyRow, expectedDocument)
}
biff.AssertEqual(resp.StatusCode, http.StatusOK)
}
})
})
a.Alternative("Create index - map", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:createIndex").
WithBodyJson(JSON{"name": "my-index", "type": "map", "field": "id", "sparse": true}).Do()
expectedBody := JSON{"type": "map", "name": "my-index", "field": "id", "sparse": true}
biff.AssertEqual(resp.StatusCode, http.StatusCreated)
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
a.Alternative("Drop index", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:dropIndex").
WithBodyJson(JSON{"name": "my-index"}).Do()
Save(resp, "Drop index", ``)
biff.AssertEqual(resp.StatusCode, http.StatusNoContent)
a.Alternative("Insert twice", func(a *biff.A) {
{
resp := apiRequest("GET", "/collections/my-collection").Do()
biff.AssertEqualJson(resp.BodyJson().(JSON)["total"], 0)
}
myDocument := JSON{"id": "duplicated-id"}
apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
{
resp := apiRequest("GET", "/collections/my-collection").Do()
biff.AssertEqualJson(resp.BodyJson().(JSON)["total"], 2)
}
})
})
a.Alternative("Get index", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:getIndex").
WithBodyJson(JSON{
"name": "my-index",
}).Do()
Save(resp, "Retrieve index", ``)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("List indexes", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:listIndexes").Do()
Save(resp, "List indexes", ``)
expectedBody := []JSON{{"type": "map", "name": "my-index", "field": "id", "sparse": true}}
biff.AssertEqual(resp.StatusCode, http.StatusOK)
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("Insert twice", func(a *biff.A) {
myDocument := JSON{
"id": "my-id",
"name": "Fulanez",
"address": "Elm Street 11",
}
apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
Save(resp, "Insert - unique index conflict", ``)
expectedBody := JSON{
"error": JSON{
"description": "Unexpected error",
"message": "index add 'my-index': index conflict: field 'id' with value 'my-id'",
},
}
biff.AssertEqual(resp.StatusCode, http.StatusConflict)
biff.AssertEqual(resp.BodyJson(), expectedBody)
})
a.Alternative("Find with unique index", func(a *biff.A) {
myDocument := JSON{
"id": "my-id",
"name": "Fulanez",
"address": "Elm Street 11",
}
apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"value": "my-id",
}).Do()
Save(resp, "Find - by unique index", ``)
biff.AssertEqual(resp.BodyJson(), myDocument)
biff.AssertEqual(resp.StatusCode, http.StatusOK)
})
a.Alternative("Find - index not found", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "invented",
"value": "my-id",
}).Do()
Save(resp, "Find - index not found", ``)
expectedBody := JSON{
"error": JSON{
"description": "Unexpected error",
"message": "index 'invented' not found, available indexes [my-index]",
},
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
biff.AssertEqual(resp.StatusCode, 500) // todo: should be 400
})
})
a.Alternative("Create index - btree compound", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:createIndex").
WithBodyJson(JSON{"name": "my-index", "type": "btree", "fields": []string{"category", "-product"}}).Do()
Save(resp, "Create index - btree compound", ``)
a.Alternative("Insert some documents", func(a *biff.A) {
documents := []JSON{
{"id": "1", "category": "fruit", "product": "orange"},
{"id": "2", "category": "drink", "product": "water"},
{"id": "3", "category": "drink", "product": "milk"},
{"id": "4", "category": "fruit", "product": "apple"},
}
for _, document := range documents {
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(document).Do()
fmt.Println(resp.StatusCode, resp.BodyString())
}
a.Alternative("Find with BTree", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
}).Do()
Save(resp, "Find - by BTree", ``)
expectedOrderIDs := []string{"2", "3", "1", "4"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
biff.AssertEqual(i, len(expectedOrderIDs))
})
})
})
a.Alternative("Create index - btree", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:createIndex").
WithBodyJson(JSON{"name": "my-index", "type": "btree", "fields": []string{"category", "product"}}).Do()
Save(resp, "Create index - btree", ``)
expectedBody := JSON{"name": "my-index", "type": "btree", "fields": []interface{}{"category", "product"}, "sparse": false, "unique": false}
biff.AssertEqual(resp.StatusCode, http.StatusCreated)
biff.AssertEqual(resp.BodyJson(), expectedBody)
a.Alternative("Insert some documents", func(a *biff.A) {
documents := []JSON{
{"id": "1", "category": "fruit", "product": "orange"},
{"id": "2", "category": "drink", "product": "water"},
{"id": "3", "category": "drink", "product": "milk"},
{"id": "4", "category": "fruit", "product": "apple"},
}
for _, document := range documents {
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(document).Do()
fmt.Println(resp.StatusCode, resp.BodyString())
}
a.Alternative("Find with BTree", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
}).Do()
Save(resp, "Find - by BTree", ``)
expectedOrderIDs := []string{"3", "2", "4", "1"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
biff.AssertEqual(i, len(expectedOrderIDs))
})
a.Alternative("Find with BTree with filter", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
"filter": JSON{
"category": "fruit",
},
}).Do()
Save(resp, "Find - by BTree with filter", ``)
expectedOrderIDs := []string{"4", "1"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
biff.AssertEqual(i, len(expectedOrderIDs))
})
a.Alternative("Remove - BTree ", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
}).Do()
Save(resp, "Remove - by BTree with filter", ``)
expectedOrderIDs := []string{"3", "2", "4", "1"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
biff.AssertEqual(i, len(expectedOrderIDs))
})
a.Alternative("Remove - BTree with filter", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
"filter": JSON{
"category": "fruit",
},
}).Do()
Save(resp, "Remove - by BTree with filter", ``)
expectedOrderIDs := []string{"4", "1"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
biff.AssertEqual(i, len(expectedOrderIDs))
})
a.Alternative("Find with BTree - reverse order", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{
"index": "my-index",
"skip": 0,
"limit": 10,
"reverse": true,
}).Do()
Save(resp, "Find - by BTree reverse order", ``)
expectedOrderIDs := []string{"1", "4", "2", "3"}
d := json.NewDecoder(bytes.NewReader(resp.BodyBytes()))
i := 0
for {
item := JSON{}
err := d.Decode(&item)
if err == io.EOF {
break
}
biff.AssertEqual(item["id"], expectedOrderIDs[i])
i++
}
})
})
})
a.Alternative("Find with collection not found", func(a *biff.A) {
resp := apiRequest("POST", "/collections/your-collection:find").
WithBodyJson(JSON{}).Do()
Save(resp, "Find - collection not found", ``)
errorMessage := resp.BodyJson().(JSON)["error"].(JSON)["message"].(string)
biff.AssertEqual(errorMessage, "collection not found")
biff.AssertEqual(resp.StatusCode, http.StatusInternalServerError) // todo: it should return 404
})
a.Alternative("Set defaults", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:setDefaults").
WithBodyJson(JSON{
"c": "auto()",
"n": "auto()",
"name": "",
"street": "",
"verified": false,
}).Do()
expectedBody := JSON{
"id": "uuid()",
"c": "auto()",
"n": "auto()",
"name": "",
"street": "",
"verified": false,
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
a.Alternative("Insert with defaults - overwrite field", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(JSON{
"name": "fulanez",
}).Do()
expectedBody := JSON{
"id": resp.BodyJson().(JSON)["id"],
"c": 1,
"n": 1,
"name": "fulanez",
"street": "",
"verified": false,
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("Insert with defaults - new field", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(JSON{
"new": "field",
}).Do()
expectedBody := JSON{
"id": resp.BodyJson().(JSON)["id"],
"c": 1,
"n": 1,
"name": "",
"street": "",
"verified": false,
"new": "field",
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
})
a.Alternative("Set defaults - example", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:setDefaults").
WithBodyJson(JSON{
"created_on": "unixnano()",
"name": "",
"street": "",
"verified": false,
}).Do()
Save(resp, "Set defaults", `
The ´SetDefaults´ function is designed to automatically assign predefined default values to specific
fields in a document when a new entry is added to a database collection. This ensures consistency and
completeness of data, especially for fields that require a default state or value.
## Overview
When you insert a new document into the collection, ´SetDefaults´ intervenes by checking for any fields
that have not been explicitly provided in the input document. For such fields, if default values have
been predefined using SetDefaults, those values are automatically added to the document before it is
inserted into the collection. This process is seamless and ensures that every new document adheres to
a defined structure and contains all necessary information.
## Example usage
Consider a scenario where you are adding a new user record to a collection but only provide the user's
name. If ´SetDefaults´ has been configured for the collection, it will automatically fill in any missing
fields that have default values defined.
### Input Document
When you attempt to insert a document with just the user's name:
´´´json
{
"name": "Fulanez"
}
´´´
### Predefined Defaults
Assume the following default values have been set for the collection:
´´´json
{
"id": "uuid()", // A function generating a unique identifier
"verified": false // A boolean flag set to false by default
}
´´´
### Resulting Document
With ´SetDefaults´ applied, the document that gets inserted into the collection will include the missing
fields with their default values:
´´´json
{
"id": "3bb5afae-c7b7-11ee-86b0-4f000ceb9a36", // Generated unique ID
"name": "Fulanez", // Provided by the user
"verified": false // Default value
}
´´´
## Benefits
* **Consistency**: Ensures that all documents in the collection follow a consistent structure, even when
some data points are not provided during insertion.
* **Completeness**: Guarantees that essential fields are always populated, either by the user or through
default values, ensuring data integrity.
* **Efficiency**: Saves time and effort by automating the assignment of common default values, reducing
the need for manual data entry or post-insertion updates.
## Configuration
To utilize ´SetDefaults´, you must first define the default values for the desired fields in your
collection's configuration. This typically involves specifying a field name and its corresponding
default value or function (e.g., uuid() for generating unique identifiers).
It's important to note that ´SetDefaults´ only applies to new documents being inserted into the
collection. It does not affect documents that are already present in the collection or those being
updated.
## Generative Functions in ´SetDefaults´
´SetDefaults´ supports a variety of generative functions to automatically assign dynamic values to
fields in new documents. These functions are executed at the time of document insertion, ensuring that
each entry receives a unique or contextually appropriate value based on the specified function. Below is
a list of supported generative functions:
### 1. ´uuid()´
**Description**: Generates a Universally Unique Identifier (UUID) for the document. This is particularly
useful for assigning a unique identifier to each entry, ensuring that each document can be distinctly
identified within the collection.
**Example Usage**: Ideal for fields requiring a unique ID, such as user identifiers, transaction IDs, etc.
**Output Example**: ´"id": "3bb5afae-c7b7-11ee-86b0-4f000ceb9a36"´
### 2. ´unixnano()´
**Description**: Produces a numerical value representing the current time in Unix nanoseconds. This
function is handy for timestamping documents at the exact time of their creation, providing
high-resolution time tracking.
**Example Usage**: Suitable for fields that need to record the precise time of document insertion,
like creation timestamps, log entries, etc.
**Output Example**: ´"created_at": 16180339887467395´ (represents the number of nanoseconds since
January 1, 1970, 00:00:00 UTC)
### 3. ´auto()´
**Description**: Implements an automatic row counter that increments with each insert, starting from
the first insertion. This function is beneficial for maintaining a sequential order or count of the
documents added to the collection.
**Example Usage**: Useful for auto-increment fields, such as a serial number, order number, or any
scenario where a simple, incrementing counter is needed.
**Output Example**: ´"serial_number": 1023´ (where 1023 is the current count of documents inserted
since the first one)
### Implementation Considerations
When integrating generative functions with ´SetDefaults´, consider the following:
**Uniqueness**: Functions like uuid() guarantee uniqueness, making them ideal for identifiers.
**Temporal Precision**: unixnano() provides high-precision timestamps, useful for time-sensitive data.
**Sequential Integrity**: auto() ensures a consistent, incremental sequence, beneficial for ordering or
numbering entries.
Ensure that the chosen generative function aligns with the field's purpose and the overall data model's
requirements. Proper configuration of ´SetDefaults´ with these functions enhances data integrity,
consistency, and utility within your application.
`)
expectedBody := JSON{
"id": "uuid()",
"created_on": "unixnano()",
"name": "",
"street": "",
"verified": false,
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
})
a.Alternative("Set defaults - auto", func(a *biff.A) {
apiRequest("POST", "/collections/my-collection:setDefaults").
WithBodyJson(JSON{
"id": nil,
"n": "auto()",
}).Do()
a.Alternative("Insert multiple", func(a *biff.A) {
for i := 1; i <= 4; i++ {
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(JSON{}).Do()
expectedBody := JSON{
"n": i,
}
biff.AssertEqualJson(resp.BodyJson(), expectedBody)
}
})
})
})
a.Alternative("Insert on not existing collection", func(a *biff.A) {
myDocument := JSON{
"id": "my-id",
}
resp := apiRequest("POST", "/collections/my-collection:insert").
WithBodyJson(myDocument).Do()
expectedBody := map[string]any{
"id": "my-id",
}
biff.AssertEqual(resp.BodyJson(), expectedBody)
biff.AssertEqual(resp.StatusCode, http.StatusCreated)
a.Alternative("List collection", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:find").
WithBodyJson(JSON{}).Do()
biff.AssertEqual(resp.BodyString(), "{\"id\":\"my-id\"}\n")
biff.AssertEqual(resp.StatusCode, http.StatusOK)
})
})
// todo review this alternative
a.Alternative("Create index on not existing collection", func(a *biff.A) {
resp := apiRequest("POST", "/collections/my-collection:createIndex").
WithBodyJson(JSON{
"kind": "map",
"field": "id",
}).Do()
biff.AssertEqual(resp.StatusCode, http.StatusInternalServerError)
})
}