github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/db_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build js 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 }