forked from leesper/couchdb-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_test.go
1480 lines (1335 loc) · 34.7 KB
/
database_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
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package couchdb
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"mime"
"net/url"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
func TestNewDefaultDB(t *testing.T) {
dbDefault, err := NewDatabase("golang-default")
if err != nil {
t.Errorf("new default database error %v", err)
}
if err = dbDefault.Available(); err == nil {
t.Error(`db available`)
}
}
func TestNewDB(t *testing.T) {
newDB := "golang-newdb"
server.Create(newDB)
defer server.Delete(newDB)
dbNew, err := NewDatabase(fmt.Sprintf("%s/%s", DefaultBaseURL, newDB))
if err != nil {
t.Error(`new database error`, err)
}
if err = dbNew.Available(); err != nil {
t.Error(`db not available, error`, err)
}
}
func TestSaveNew(t *testing.T) {
doc := map[string]interface{}{"doc": "bar"}
id, rev, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save error`, err)
}
if id != doc["_id"].(string) {
t.Errorf("invalid id: %q", id)
}
if rev != doc["_rev"].(string) {
t.Errorf("invalid rev: %q", rev)
}
}
func TestSaveNewWithID(t *testing.T) {
doc := map[string]interface{}{"_id": "foo"}
id, rev, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save error`, err)
}
if doc["_id"].(string) != "foo" {
t.Errorf("doc[_id] = %s, not foo", doc["_id"])
}
if id != "foo" {
t.Errorf("id = %s, not foo", id)
}
if rev != doc["_rev"].(string) {
t.Errorf("invalid rev: %q", rev)
}
}
func TestSaveExisting(t *testing.T) {
doc := map[string]interface{}{}
idOld, revOld, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save error`, err)
}
doc["foo"] = true
idNew, revNew, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save foo error`, err)
}
if idOld != idNew {
t.Errorf("ids are not equal old %s new %s", idOld, idNew)
}
if doc["_rev"].(string) != revNew {
t.Errorf("invalid rev %s want %s", doc["_rev"].(string), revNew)
}
if revOld == revNew {
t.Errorf("new rev is equal to old rev %s", revOld)
}
}
func TestSaveNewBatch(t *testing.T) {
doc := map[string]interface{}{"_id": "foo"}
_, rev, err := testsDB.Save(doc, url.Values{"batch": []string{"ok"}})
if err != nil {
t.Error(`db save batch error`, err)
}
if len(rev) > 0 {
t.Error(`rev not empty`, rev)
}
if r, ok := doc["_rev"]; ok {
t.Error(`doc has _rev field`, r.(string))
}
}
func TestSaveExistingBatch(t *testing.T) {
doc := map[string]interface{}{"_id": "bar"}
idOld, revOld, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save error`, err)
}
idNew, revNew, err := testsDB.Save(doc, url.Values{"batch": []string{"ok"}})
if err != nil {
t.Error(`db save batch error`, err)
}
if idOld != idNew {
t.Errorf("old id %s not equal to new id %s", idOld, idNew)
}
if len(revNew) > 0 {
t.Error(`rev not empty`, revNew)
}
if doc["_rev"].(string) != revOld {
t.Errorf("doc[_rev] %s not equal to old rev %s", doc["_rev"].(string), revOld)
}
}
func TestDatabaseExists(t *testing.T) {
if err := testsDB.Available(); err != nil {
t.Error(`golang-tests not available, error`, err)
}
dbMissing, _ := NewDatabase("golang-missing")
if err := dbMissing.Available(); err == nil {
t.Error(`golang-missing available`)
}
}
func TestDatabaseName(t *testing.T) {
name, err := testsDB.Name()
if err != nil {
t.Error(`db name error`, err)
}
if name != "golang-tests" {
t.Errorf("db name %s, want golang-tests", name)
}
}
func TestDatabaseString(t *testing.T) {
if testsDB.String() != "Database http://localhost:5984/golang-tests" {
t.Error(`db string invalid`, testsDB)
}
}
func TestCommit(t *testing.T) {
if err := testsDB.Commit(); err != nil {
t.Error(`db commit error`, err)
}
}
func TestCreateLargeDoc(t *testing.T) {
var buf bytes.Buffer
// 10MB
for i := 0; i < 110*1024; i++ {
buf.WriteString("0123456789")
}
doc := map[string]interface{}{"data": buf.String()}
if err := testsDB.Set("large", doc); err != nil {
t.Error(`db set error`, err)
}
doc, err := testsDB.Get("large", nil)
if err != nil {
t.Error(`db get error`, err)
}
if doc["_id"].(string) != "large" {
t.Errorf("doc[_id] = %s, want large", doc["_id"].(string))
}
err = testsDB.DeleteDoc(doc)
if err != nil {
t.Error(`db delete doc error`, err)
}
}
func TestDocIDQuoting(t *testing.T) {
doc := map[string]interface{}{"foo": "bar"}
err := testsDB.Set("foo/bar", doc)
if err != nil {
t.Error(`db set error`, err)
}
doc, err = testsDB.Get("foo/bar", nil)
if err != nil {
t.Error(`db get error`, err)
}
if doc["foo"].(string) != "bar" {
t.Errorf("doc[foo] = %s want bar", doc["foo"].(string))
}
err = testsDB.Delete("foo/bar")
if err != nil {
t.Error(`db delete error`, err)
}
_, err = testsDB.Get("foo/bar", nil)
if err == nil {
t.Error(`db get foo/bar ok`)
}
}
func TestDisallowNaN(t *testing.T) {
doc := map[string]interface{}{"number": math.NaN()}
err := testsDB.Set("foo", doc)
if err == nil {
t.Error(`db set NaN ok`)
}
}
func TestDisallowNilID(t *testing.T) {
err := testsDB.DeleteDoc(map[string]interface{}{"_id": nil, "_rev": nil})
if err == nil {
t.Error(`db delete doc with id nil ok`)
}
err = testsDB.DeleteDoc(map[string]interface{}{"_id": "foo", "_rev": nil})
if err == nil {
t.Error(`db delete doc with rev nil ok`)
}
}
func TestDocRevs(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{"bar": 42}
err := testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
oldRev := doc["_rev"].(string)
doc["bar"] = 43
err = testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
newRev := doc["_rev"].(string)
newDoc, err := testsDB.Get(uuid, nil)
if err != nil {
t.Error("db get error", err)
}
if newRev != newDoc["_rev"].(string) {
t.Errorf("new doc rev %s want %s", newDoc["_rev"].(string), newRev)
}
newDoc, err = testsDB.Get(uuid, url.Values{"rev": []string{newRev}})
if err != nil {
t.Error("db get error", err)
}
if newRev != newDoc["_rev"].(string) {
t.Errorf("new doc rev %s want %s", newDoc["_rev"].(string), newRev)
}
oldDoc, err := testsDB.Get(uuid, url.Values{"rev": []string{oldRev}})
if err != nil {
t.Error("db get error", err)
}
if oldRev != oldDoc["_rev"].(string) {
t.Errorf("old doc rev %s want %s", oldDoc["_rev"].(string), oldRev)
}
revs, err := testsDB.Revisions(uuid, nil)
if err != nil {
t.Error(`db revisions error`, err)
}
if revs[0]["_rev"].(string) != newRev {
t.Errorf("revs first %s want %s", revs[0]["_rev"].(string), newRev)
}
if revs[1]["_rev"].(string) != oldRev {
t.Errorf("revs second %s not equal to %s", revs[1]["_rev"].(string), oldRev)
}
_, err = testsDB.Revisions("crap", nil)
if err == nil {
t.Error(`db revisions crap ok`)
}
err = testsDB.Compact()
if err != nil {
t.Error("db compact error", err)
}
info, err := testsDB.Info("")
if err != nil {
t.Error(`db info error`, err)
}
for info["compact_running"].(bool) {
info, err = testsDB.Info("")
if err != nil {
t.Error(`db info error`, err)
}
}
_, err = testsDB.Get(uuid, url.Values{"rev": []string{oldRev}})
if err == nil {
t.Errorf("db get compacted doc ok, rev = %s", oldRev)
}
}
func TestAttachmentCRUD(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{"bar": 42}
testsDB.Set(uuid, doc)
oldRev := doc["_rev"].(string)
testsDB.PutAttachment(doc, []byte("Foo bar"), "foo.txt", "text/plain")
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err := testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
attachments := reflect.ValueOf(doc["_attachments"])
foo := reflect.ValueOf(attachments.MapIndex(reflect.ValueOf("foo.txt")).Interface())
length := int(foo.MapIndex(reflect.ValueOf("length")).Interface().(float64))
if length != len("Foo bar") {
t.Errorf("length %d want %d", length, len("Foo bar"))
}
contentType := foo.MapIndex(reflect.ValueOf("content_type")).Interface().(string)
if contentType != "text/plain" {
t.Errorf("content type %s want text/plain", contentType)
}
data, err := testsDB.GetAttachment(doc, "foo.txt")
if err != nil {
t.Error(`get attachment error`, err)
}
if string(data) != "Foo bar" {
t.Errorf("db get attachment %s want Foo bar", string(data))
}
data, err = testsDB.GetAttachmentID(uuid, "foo.txt")
if err != nil {
t.Error(`get attachment id error`, err)
}
if string(data) != "Foo bar" {
t.Errorf("db get attachment id %s want Foo bar", string(data))
}
oldRev = doc["_rev"].(string)
err = testsDB.DeleteAttachment(doc, "foo.txt")
if err != nil {
t.Error(`db delete attachment error`, err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
if _, ok := doc["_attachments"]; ok {
t.Error(`doc attachments still existed`)
}
}
func TestAttachmentWithFiles(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{"bar": 42}
err := testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
oldRev := doc["_rev"].(string)
fileObj := []byte("Foo bar baz")
err = testsDB.PutAttachment(doc, fileObj, "foo.txt", mime.TypeByExtension(".txt"))
if err != nil {
t.Error("db put attachment error", err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
attachments := reflect.ValueOf(doc["_attachments"])
foo := reflect.ValueOf(attachments.MapIndex(reflect.ValueOf("foo.txt")).Interface())
length := int(foo.MapIndex(reflect.ValueOf("length")).Interface().(float64))
if length != len("Foo bar baz") {
t.Errorf("length %d want %d", length, len("Foo bar"))
}
contentType := foo.MapIndex(reflect.ValueOf("content_type")).Interface().(string)
if contentType != "text/plain; charset=utf-8" {
t.Errorf("content type %s want text/plain; charset=utf-8", contentType)
}
data, err := testsDB.GetAttachment(doc, "foo.txt")
if err != nil {
t.Error(`get attachment error`, err)
}
if string(data) != "Foo bar baz" {
t.Errorf("db get attachment %s want Foo bar", string(data))
}
data, err = testsDB.GetAttachmentID(uuid, "foo.txt")
if err != nil {
t.Error(`get attachment id error`, err)
}
if string(data) != "Foo bar baz" {
t.Errorf("db get attachment id %s want Foo bar", string(data))
}
oldRev = doc["_rev"].(string)
err = testsDB.DeleteAttachment(doc, "foo.txt")
if err != nil {
t.Error(`db delete attachment error`, err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
if _, ok := doc["_attachments"]; ok {
t.Error(`doc attachments still existed`)
}
}
func TestAttachmentCRUDFromFS(t *testing.T) {
uuid := GenerateUUID()
content := "Foo bar baz"
tmpFileName := filepath.Join(os.TempDir(), "foo.txt")
tmpFile, err := os.Create(tmpFileName)
if err != nil {
t.Error(`create file error`, err)
}
_, err = tmpFile.Write([]byte(content))
if err != nil {
t.Error(`write file error`, err)
}
tmpFile.Close()
tmpFile, err = os.Open(tmpFileName)
if err != nil {
t.Error(`open file error`, err)
}
defer tmpFile.Close()
data, err := ioutil.ReadAll(tmpFile)
if err != nil {
t.Error(`read tmp file error`, err)
}
doc := map[string]interface{}{"bar": 42}
err = testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
oldRev := doc["_rev"].(string)
err = testsDB.PutAttachment(doc, data, "foo.txt", mime.TypeByExtension(filepath.Ext(tmpFileName)))
if err != nil {
t.Error(`put attachment error`, err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
attachment := reflect.ValueOf(doc["_attachments"])
foo := reflect.ValueOf(attachment.MapIndex(reflect.ValueOf("foo.txt")).Interface())
length := int(foo.MapIndex(reflect.ValueOf("length")).Interface().(float64))
if len(content) != length {
t.Errorf("length %d want %d", length, len(content))
}
contentType := foo.MapIndex(reflect.ValueOf("content_type")).Interface().(string)
if contentType != "text/plain; charset=utf-8" {
t.Errorf("content type %s want text/plain; charset=utf-8", contentType)
}
data, err = testsDB.GetAttachment(doc, "foo.txt")
if err != nil {
t.Error(`get attachment error`, err)
}
if string(data) != content {
t.Error(`get attachment should be `, content)
}
data, err = testsDB.GetAttachmentID(uuid, "foo.txt")
if err != nil {
t.Error(`get attachment id error`, err)
}
if string(data) != content {
t.Error(`get attachment id should be `, content)
}
if err = testsDB.DeleteAttachment(doc, "foo.txt"); err != nil {
t.Error(`delete attachment file error`, err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
if _, ok := doc["_attachments"]; ok {
t.Error(`doc attachments still existed`)
}
}
func TestEmptyAttachment(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{}
err := testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
oldRev := doc["_rev"].(string)
err = testsDB.PutAttachment(doc, []byte(""), "empty.txt", mime.TypeByExtension(".txt"))
if err != nil {
t.Error(`put attachment error`, err)
}
if oldRev == doc["_rev"].(string) {
t.Error(`doc[_rev] == oldRev`)
}
doc, err = testsDB.Get(uuid, nil)
if err != nil {
t.Error(`db get error`, err)
}
attachment := reflect.ValueOf(doc["_attachments"])
empty := reflect.ValueOf(attachment.MapIndex(reflect.ValueOf("empty.txt")).Interface())
length := int(empty.MapIndex(reflect.ValueOf("length")).Interface().(float64))
if length != 0 {
t.Errorf("length %d want %d", length, 0)
}
}
func TestDefaultAttachment(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{}
err := testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
_, err = testsDB.GetAttachment(doc, "missing.txt")
if err == nil {
t.Error(`db get attachment ok`)
}
}
func TestAttachmentNoFilename(t *testing.T) {
uuid := GenerateUUID()
doc := map[string]interface{}{}
err := testsDB.Set(uuid, doc)
if err != nil {
t.Error(`db set doc error`, err)
}
err = testsDB.PutAttachment(doc, []byte(""), "", "")
if err == nil {
t.Error(`db put attachment with no file name ok`)
}
}
func TestJSONAttachment(t *testing.T) {
doc := map[string]interface{}{}
err := testsDB.Set(GenerateUUID(), doc)
if err != nil {
t.Error(`db set doc error`, err)
}
err = testsDB.PutAttachment(doc, []byte("{}"), "test.json", "application/json")
if err != nil {
t.Error(`db put attachment json error`, err)
}
data, err := testsDB.GetAttachment(doc, "test.json")
if err != nil {
t.Error(`db get attachment json error`, err)
}
if string(data) != "{}" {
t.Errorf("data = %s want {}", string(data))
}
}
func TestBulkUpdateConflict(t *testing.T) {
docs := []map[string]interface{}{
{
"type": "Person",
"name": "John Doe",
},
{
"type": "Person",
"name": "Mary Jane",
},
{
"type": "Person",
"name": "Gotham City",
},
}
testsDB.Update(docs, nil)
// update the first doc to provoke a conflict in the next bulk update
doc := map[string]interface{}{}
for k, v := range docs[0] {
doc[k] = v
}
testsDB.Set(doc["_id"].(string), doc)
results, err := testsDB.Update(docs, nil)
if err != nil {
t.Error(`db update error`, err)
}
if results[0].Err != ErrConflict {
t.Errorf("db update conflict err %v want ErrConflict", results[0].Err)
}
}
func TestCopyDocConflict(t *testing.T) {
testsDB.Set("foo1", map[string]interface{}{"status": "idle"})
testsDB.Set("bar1", map[string]interface{}{"status": "testing"})
_, err := testsDB.Copy("foo1", "bar1", "")
if err != ErrConflict {
t.Errorf(`db copy returns %v, want ErrConflict`, err)
}
}
func TestCopyDocOverwrite(t *testing.T) {
foo2 := map[string]interface{}{"status": "testing"}
bar2 := map[string]interface{}{"status": "idle"}
testsDB.Set("foo2", foo2)
testsDB.Set("bar2", bar2)
result, err := testsDB.Copy("foo2", "bar2", bar2["_rev"].(string))
if err != nil {
t.Error(`db copy error`, err)
}
doc, _ := testsDB.Get("bar2", nil)
if result != doc["_rev"].(string) {
t.Errorf("db copy returns %s want %s", result, doc["_rev"].(string))
}
if doc["status"].(string) != "testing" {
t.Errorf("db copy status = %s, want testing", doc["status"].(string))
}
}
func TestChanges(t *testing.T) {
options := url.Values{
"style": []string{"all_docs"},
}
_, err := testsDB.Changes(options)
if err != nil {
t.Error(`db change error`, err)
}
}
func TestPurge(t *testing.T) {
version, err := server.Version()
if err != nil {
t.Error("server version error", err)
}
// TODO: purge not implemented in CouchDB 2.0.0
if !strings.HasPrefix(version, "2") {
doc := map[string]interface{}{"a": "b"}
err := testsDB.Set("purge", doc)
if err != nil {
t.Error(`db set error`, err)
}
result, err := testsDB.Purge([]map[string]interface{}{doc})
if err != nil {
t.Error(`db purge error`, err)
}
purgeSeq := int(result["purge_seq"].(float64))
if purgeSeq != 1 {
t.Errorf("db purge seq=%d want 1", purgeSeq)
}
}
}
func TestSecurity(t *testing.T) {
secDoc, err := testsDB.GetSecurity()
if err != nil {
t.Error(`get security should return true`)
}
if len(secDoc) > 0 {
t.Error(`secDoc should be empty`)
}
if testsDB.SetSecurity(map[string]interface{}{
"names": []string{"test"},
"roles": []string{},
}) != nil {
t.Error(`set security should return true`)
}
}
func TestDBContains(t *testing.T) {
doc := map[string]interface{}{
"type": "Person",
"name": "Jason Statham",
}
id, _, err := testsDB.Save(doc, nil)
if err != nil {
t.Error(`db save error`, err)
}
if err = testsDB.Contains(id); err != nil {
t.Error(`db contains error`, err)
}
}
func TestDBSetGetDelete(t *testing.T) {
doc := map[string]interface{}{
"type": "Person",
"name": "Jason Statham",
}
err := testsDB.Set("Mechanic", doc)
if err != nil {
t.Error(`db set error`, err)
}
_, err = testsDB.Get("Mechanic", nil)
if err != nil {
t.Error(`db get error`, err)
}
err = testsDB.Delete("Mechanic")
if err != nil {
t.Error(`db delete error`, err)
}
}
func TestDBDocIDsAndLen(t *testing.T) {
doc := map[string]interface{}{
"type": "Person",
"name": "Jason Statham",
}
err := testsDB.Set("Mechanic", doc)
if err != nil {
t.Error(`db set error`, err)
}
ids, err := testsDB.DocIDs()
if err != nil {
t.Error(`db doc ids error`, err)
}
length, err := testsDB.Len()
if err != nil {
t.Error(`db len error`, err)
}
if length != len(ids) {
t.Errorf("Len() returns %d want %d", length, len(ids))
}
}
func TestGetSetRevsLimit(t *testing.T) {
err := testsDB.SetRevsLimit(10)
if err != nil {
t.Error(`db set revs limit error`, err)
}
limit, err := testsDB.GetRevsLimit()
if err != nil {
t.Error(`db get revs limit error`, err)
}
if limit != 10 {
t.Error(`limit should be 10`)
}
}
func TestCleanup(t *testing.T) {
err := testsDB.Cleanup()
if err != nil {
t.Error(`db clean up error`, err)
}
}
func TestParseSelectorSyntax(t *testing.T) {
_, err := parseSelectorSyntax(`title == "Spacecataz" && year == 2004 && director == "Dave Willis"`)
if err != nil {
t.Error("parse selector syntax error", err)
}
_, err = parseSelectorSyntax(`year >= 1990 && (director == "George Lucas" || director == "Steven Spielberg")`)
if err != nil {
t.Error("parse selector syntax error", err)
}
_, err = parseSelectorSyntax(`year >= 1900 && year <= 2000 && nor(year == 1990, year == 1989, year == 1997)`)
if err != nil {
t.Error("parse selector syntax error", err)
}
_, err = parseSelectorSyntax(`_id > nil && all(genre, []string{"Comedy", "Short"})`)
if err != nil {
t.Error("parse selector syntax error", err)
}
_, err = parseSelectorSyntax(`_id > nil && any(genre, genre == "Short" || genre == "Horror" || score >= 8)`)
if err != nil {
t.Error("parse selector syntax error", err)
}
_, err = parseSelectorSyntax(`exists(director, true, "wrongParam")`)
if err == nil {
t.Error("parse exists function ok, should be 2 parameters")
}
_, err = parseSelectorSyntax(`typeof(genre, "array", "wrongParam")`)
if err == nil {
t.Error("parse typeof function ok, should be 2 parameters")
}
_, err = parseSelectorSyntax(`in(director, []string{"Mike Portnoy", "Vitali Kanevsky"}, "wrongParam")`)
if err == nil {
t.Error("parse in function ok, should be 2 parameters")
}
_, err = parseSelectorSyntax(`nin(year, []int{1990, 1992, 1998}, "wrongParam")`)
if err == nil {
t.Error("parse nin function ok, should be 2 parameters")
}
_, err = parseSelectorSyntax(`size(genre, 2, "wrongParam")`)
if err == nil {
t.Error("parse size function ok, should be 2 parameters")
}
_, err = parseSelectorSyntax(`mod(year, 2, 1, "wrongParam")`)
if err == nil {
t.Error("parse mod function ok, should be 3 parameters")
}
_, err = parseSelectorSyntax(`regex(title, "^A", "wrongParam")`)
if err == nil {
t.Error("parse regex function ok, should be 2 parameters")
}
}
func TestParseSortSyntax(t *testing.T) {
_, err := parseSortSyntax([]string{"fieldNameA", "fieldNameB"})
if err != nil {
t.Error("parse sort syntax error", err)
}
_, err = parseSortSyntax([]string{"fieldNameA.subFieldA", "fieldNameB.subFieldB"})
if err != nil {
t.Error("parse sort syntax error", err)
}
_, err = parseSortSyntax([]string{"desc(fieldName1)", "asc(fieldName2)"})
if err != nil {
t.Error("parse sort syntax error", err)
}
_, err = parseSortSyntax([]string{"desc(fieldName1.subField1)", "asc(fieldName2.subField2)"})
if err != nil {
t.Error("parse sort syntax error", err)
}
}
func TestQueryYearAndID(t *testing.T) {
version, err := server.Version()
if err != nil {
t.Error("server version error", err)
}
// CouchDB 2.0 feature
if strings.HasPrefix(version, "2") {
docsQuery, err := movieDB.Query(nil, `_id > nil && in(year, []int{2007, 2004})`, nil, nil, nil, nil)
if err != nil {
t.Error("db query error", err)
}
var rawJSON = `
{
"selector": {
"$and": [
{
"_id": { "$gt": null }
},
{
"year": {
"$in": [2007, 2004]
}
}
]
}
}`
docsRaw, err := movieDB.QueryJSON(rawJSON)
if err != nil {
t.Error("db query json error", err)
}
if !reflect.DeepEqual(docsQuery, docsRaw) {
t.Error("db query year and id not equal")
}
}
}
func TestQueryYearOrDirector(t *testing.T) {
version, err := server.Version()
if err != nil {
t.Error("server version error", err)
}
// CouchDB 2.0 feature
if strings.HasPrefix(version, "2") {
docsQuery, err := movieDB.Query(nil, `year == 1989 && (director == "Ademir Kenovic" || director == "Dezs Garas")`, nil, nil, nil, nil)
if err != nil {
t.Error("db query error", err)
}
var rawJSON = `
{
"selector": {
"year": 1989,
"$or": [
{ "director": "Ademir Kenovic" },
{ "director": "Dezs Garas" }
]
}
}`
docsRaw, err := movieDB.QueryJSON(rawJSON)
if err != nil {
t.Error("db query json error", err)
}
if !reflect.DeepEqual(docsQuery, docsRaw) {
t.Error("db query year or director not equal")
}
}
}
func TestQueryYearGteLteNot(t *testing.T) {
version, err := server.Version()
if err != nil {
t.Error("server version error", err)
}
// CouchDB 2.0 feature
if strings.HasPrefix(version, "2") {
docsQuery, err := movieDB.Query(nil, `year >= 1989 && year <= 2006 && year != 2004`, nil, nil, nil, nil)
if err != nil {
t.Error("db query error", err)
}
var rawJSON = `
{
"selector": {
"year": {
"$gte": 1989
},
"year": {
"$lte": 2006
},
"$not": {
"year": 2004
}
}
}`
docsRaw, err := movieDB.QueryJSON(rawJSON)
if err != nil {
t.Error("db query json error", err)
}
if !reflect.DeepEqual(docsQuery, docsRaw) {
t.Error("db query year gte lte not not equal")
}
}
}
func TestQueryIMDBRatingNor(t *testing.T) {
version, err := server.Version()
if err != nil {
t.Error("server version error", err)
}
// CouchDB 2.0 feature
if strings.HasPrefix(version, "2") {
docsQuery, err := movieDB.Query(nil, `imdb.rating >= 6 && imdb.rating <= 9 && nor(imdb.rating == 8.1, imdb.rating == 8.2)`, nil, nil, nil, nil)
if err != nil {
t.Error("db query error", err)
}
var rawJSON = `
{
"selector": {
"imdb.rating": {
"$gte": 6
},
"imdb.rating": {
"$lte": 9
},
"$nor": [
{ "imdb.rating": 8.1 },
{ "imdb.rating": 8.2 }