-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
111 lines (102 loc) · 3.25 KB
/
db_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
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package pouchdb
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/gopherjs/gopherjs/js"
"gitlab.com/flimzy/testy"
kivik "github.com/go-kivik/kivik/v4"
"github.com/go-kivik/kivik/v4/kiviktest/kt"
)
func init() {
memPouch := js.Global.Get("PouchDB").Call("defaults", map[string]interface{}{
"db": js.Global.Call("require", "memdown"),
})
js.Global.Set("PouchDB", memPouch)
}
func TestPut(t *testing.T) {
client, err := kivik.New("pouch", "")
if err != nil {
t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
return
}
dbname := kt.TestDBName(t)
ctx := context.Background()
defer client.DestroyDB(ctx, dbname) // nolint: errcheck
if e := client.CreateDB(ctx, dbname); e != nil {
t.Fatalf("Failed to create db: %s", e)
}
_, err = client.DB(dbname).Put(ctx, "foo", map[string]string{"_id": "bar"})
testy.StatusError(t, "id argument must match _id field in document", http.StatusBadRequest, err)
}
func TestPurge(t *testing.T) {
client, err := kivik.New("pouch", "")
if err != nil {
t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
return
}
v, _ := client.Version(context.Background())
pouchVer := v.Version
t.Run("PouchDB 7", func(t *testing.T) {
if !strings.HasPrefix(pouchVer, "7.") {
t.Skipf("Skipping PouchDB 7 test for PouchDB %v", pouchVer)
}
const wantErr = "kivik: purge supported by PouchDB 8 or newer"
client, err := kivik.New("pouch", "")
if err != nil {
t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
return
}
dbname := kt.TestDBName(t)
ctx := context.Background()
t.Cleanup(func() {
_ = client.DestroyDB(ctx, dbname)
})
if e := client.CreateDB(ctx, dbname); e != nil {
t.Fatalf("Failed to create db: %s", e)
}
_, err = client.DB(dbname).Purge(ctx, map[string][]string{"foo": {"1-xxx"}})
if !testy.ErrorMatches(wantErr, err) {
fmt.Fprintf(os.Stderr, "%s\n", err)
t.Errorf("Unexpected error: %s", err)
}
})
t.Run("no IndexedDB", func(t *testing.T) {
if strings.HasPrefix(pouchVer, "7.") {
t.Skipf("Skipping PouchDB 8 test for PouchDB %v", pouchVer)
}
const wantErr = "kivik: purge only supported with indexedDB adapter"
client, err := kivik.New("pouch", "")
if err != nil {
t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err)
return
}
dbname := kt.TestDBName(t)
ctx := context.Background()
t.Cleanup(func() {
_ = client.DestroyDB(ctx, dbname)
})
if e := client.CreateDB(ctx, dbname); e != nil {
t.Fatalf("Failed to create db: %s", e)
}
_, err = client.DB(dbname).Purge(ctx, map[string][]string{"foo": {"1-xxx"}})
if !testy.ErrorMatches(wantErr, err) {
fmt.Fprintf(os.Stderr, "%s\n", err)
t.Errorf("Unexpected error: %s", err)
}
})
}