github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/indexeddb/purge_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 indexeddb 16 17 import ( 18 "context" 19 "strings" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/gopherjs/gopherjs/js" 24 "gitlab.com/flimzy/testy" 25 26 kivik "github.com/go-kivik/kivik/v4" 27 "github.com/go-kivik/kivik/v4/kiviktest/kt" 28 _ "github.com/go-kivik/kivik/v4/pouchdb" // PouchDB driver we're testing 29 "github.com/go-kivik/kivik/v4/pouchdb/internal" 30 ) 31 32 func TestPurge(t *testing.T) { 33 pouchVer := internal.PouchDBVersion(t) 34 if strings.HasPrefix(pouchVer, "7.") { 35 t.Skipf("Skipping PouchDB 8+ test for PouchDB %v", pouchVer) 36 } 37 38 js.Global.Call("require", "fake-indexeddb/auto") 39 indexedDBPlugin := js.Global.Call("require", "pouchdb-adapter-indexeddb") 40 pouchDB := js.Global.Get("PouchDB") 41 pouchDB.Call("plugin", indexedDBPlugin) 42 idbPouch := pouchDB.Call("defaults", map[string]interface{}{ 43 "adapter": "indexeddb", 44 }) 45 js.Global.Set("PouchDB", idbPouch) 46 47 t.Run("not found", func(t *testing.T) { 48 const wantErr = "not_found: missing" 49 client, err := kivik.New("pouch", "") 50 if err != nil { 51 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err) 52 return 53 } 54 dbname := kt.TestDBName(t) 55 ctx := context.Background() 56 t.Cleanup(func() { 57 _ = client.DestroyDB(ctx, dbname) 58 }) 59 if e := client.CreateDB(ctx, dbname); e != nil { 60 t.Fatalf("Failed to create db: %s", e) 61 } 62 _, err = client.DB(dbname).Purge(ctx, map[string][]string{"foo": {"1-xxx"}}) 63 if !testy.ErrorMatches(wantErr, err) { 64 t.Errorf("Unexpected error: %s", err) 65 } 66 }) 67 t.Run("success", func(t *testing.T) { 68 client, err := kivik.New("pouch", "") 69 if err != nil { 70 t.Errorf("Failed to connect to PouchDB/memdown driver: %s", err) 71 return 72 } 73 const docID = "test" 74 dbname := kt.TestDBName(t) 75 ctx := context.Background() 76 t.Cleanup(func() { 77 _ = client.DestroyDB(ctx, dbname) 78 }) 79 if e := client.CreateDB(ctx, dbname); e != nil { 80 t.Fatalf("Failed to create db: %s", e) 81 } 82 db := client.DB(dbname) 83 rev, err := db.Put(ctx, docID, map[string]string{"foo": "bar"}) 84 if err != nil { 85 t.Fatal(err) 86 } 87 result, err := db.Purge(ctx, map[string][]string{docID: {rev}}) 88 if err != nil { 89 t.Fatal(err) 90 } 91 want := &kivik.PurgeResult{ 92 Seq: 0, 93 Purged: map[string][]string{ 94 docID: {rev}, 95 }, 96 } 97 if d := cmp.Diff(want, result); d != "" { 98 t.Error(d) 99 } 100 }) 101 }