1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package pouchdb
16
17 import (
18 "context"
19 "fmt"
20 "net/http"
21 "os"
22 "strings"
23 "testing"
24
25 "github.com/gopherjs/gopherjs/js"
26 "gitlab.com/flimzy/testy"
27
28 kivik "github.com/go-kivik/kivik/v4"
29 internal "github.com/go-kivik/kivik/v4/int/errors"
30 "github.com/go-kivik/kivik/v4/kiviktest/kt"
31 )
32
33 func init() {
34 memPouch := js.Global.Get("PouchDB").Call("defaults", map[string]interface{}{
35 "db": js.Global.Call("require", "memdown"),
36 })
37 js.Global.Set("PouchDB", memPouch)
38 }
39
40 func TestPut(t *testing.T) {
41 client, err := kivik.New("pouch", "")
42 if err != nil {
43 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
44 return
45 }
46 dbname := kt.TestDBName(t)
47 ctx := context.Background()
48 t.Cleanup(func() {
49 _ = client.DestroyDB(ctx, dbname)
50 })
51 if e := client.CreateDB(ctx, dbname); e != nil {
52 t.Fatalf("Failed to create db: %s", e)
53 }
54 _, err = client.DB(dbname).Put(ctx, "foo", map[string]string{"_id": "bar"})
55 if d := internal.StatusErrorDiff("id argument must match _id field in document", http.StatusBadRequest, err); d != "" {
56 t.Error(d)
57 }
58 }
59
60 func TestPurge(t *testing.T) {
61 client, err := kivik.New("pouch", "")
62 if err != nil {
63 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
64 return
65 }
66 v, _ := client.Version(context.Background())
67 pouchVer := v.Version
68
69 t.Run("PouchDB 7", func(t *testing.T) {
70 if !strings.HasPrefix(pouchVer, "7.") {
71 t.Skipf("Skipping PouchDB 7 test for PouchDB %v", pouchVer)
72 }
73 const wantErr = "kivik: purge supported by PouchDB 8 or newer"
74 client, err := kivik.New("pouch", "")
75 if err != nil {
76 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
77 return
78 }
79
80 dbname := kt.TestDBName(t)
81 ctx := context.Background()
82 t.Cleanup(func() {
83 _ = client.DestroyDB(ctx, dbname)
84 })
85 if e := client.CreateDB(ctx, dbname); e != nil {
86 t.Fatalf("Failed to create db: %s", e)
87 }
88 _, err = client.DB(dbname).Purge(ctx, map[string][]string{"foo": {"1-xxx"}})
89 if !testy.ErrorMatches(wantErr, err) {
90 fmt.Fprintf(os.Stderr, "%s\n", err)
91 t.Errorf("Unexpected error: %s", err)
92 }
93 })
94 t.Run("no IndexedDB", func(t *testing.T) {
95 if strings.HasPrefix(pouchVer, "7.") {
96 t.Skipf("Skipping PouchDB 8 test for PouchDB %v", pouchVer)
97 }
98 const wantErr = "kivik: purge only supported with indexedDB adapter"
99 client, err := kivik.New("pouch", "")
100 if err != nil {
101 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
102 return
103 }
104 dbname := kt.TestDBName(t)
105 ctx := context.Background()
106 t.Cleanup(func() {
107 _ = client.DestroyDB(ctx, dbname)
108 })
109 if e := client.CreateDB(ctx, dbname); e != nil {
110 t.Fatalf("Failed to create db: %s", e)
111 }
112 _, err = client.DB(dbname).Purge(ctx, map[string][]string{"foo": {"1-xxx"}})
113 if !testy.ErrorMatches(wantErr, err) {
114 fmt.Fprintf(os.Stderr, "%s\n", err)
115 t.Errorf("Unexpected error: %s", err)
116 }
117 })
118 }
119
View as plain text