github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/db/getRev.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 package db 14 15 import ( 16 "context" 17 "strings" 18 19 "github.com/go-kivik/kivik/v4" 20 "github.com/go-kivik/kivik/v4/kiviktest/kt" 21 ) 22 23 func init() { 24 kt.Register("GetRev", getRev) 25 } 26 27 func getRev(ctx *kt.Context) { 28 ctx.RunRW(func(ctx *kt.Context) { 29 dbName := ctx.TestDB() 30 db := ctx.Admin.DB(dbName, ctx.Options("db")) 31 if err := db.Err(); err != nil { 32 ctx.Fatalf("Failed to connect to test db: %s", err) 33 } 34 doc := &testDoc{ 35 ID: "bob", 36 } 37 rev, err := db.Put(context.Background(), doc.ID, doc) 38 if err != nil { 39 ctx.Fatalf("Failed to create doc in test db: %s", err) 40 } 41 doc.Rev = rev 42 43 ddoc := &testDoc{ 44 ID: "_design/foo", 45 } 46 rev, err = db.Put(context.Background(), ddoc.ID, ddoc) 47 if err != nil { 48 ctx.Fatalf("Failed to create design doc in test db: %s", err) 49 } 50 ddoc.Rev = rev 51 52 local := &testDoc{ 53 ID: "_local/foo", 54 } 55 rev, err = db.Put(context.Background(), local.ID, local) 56 if err != nil { 57 ctx.Fatalf("Failed to create local doc in test db: %s", err) 58 } 59 local.Rev = rev 60 61 ctx.Run("group", func(ctx *kt.Context) { 62 ctx.RunAdmin(func(ctx *kt.Context) { 63 ctx.Parallel() 64 db := ctx.Admin.DB(dbName, ctx.Options("db")) 65 if err := db.Err(); !ctx.IsExpectedSuccess(err) { 66 return 67 } 68 testGetRev(ctx, db, doc) 69 testGetRev(ctx, db, ddoc) 70 testGetRev(ctx, db, local) 71 testGetRev(ctx, db, &testDoc{ID: "bogus"}) 72 }) 73 ctx.RunNoAuth(func(ctx *kt.Context) { 74 ctx.Parallel() 75 db := ctx.NoAuth.DB(dbName, ctx.Options("db")) 76 if err := db.Err(); !ctx.IsExpectedSuccess(err) { 77 return 78 } 79 testGetRev(ctx, db, doc) 80 testGetRev(ctx, db, ddoc) 81 testGetRev(ctx, db, local) 82 testGetRev(ctx, db, &testDoc{ID: "bogus"}) 83 }) 84 }) 85 }) 86 } 87 88 func testGetRev(ctx *kt.Context, db *kivik.DB, expectedDoc *testDoc) { 89 ctx.Run(expectedDoc.ID, func(ctx *kt.Context) { 90 ctx.Parallel() 91 rev, err := db.GetRev(context.Background(), expectedDoc.ID) 92 if !ctx.IsExpectedSuccess(err) { 93 return 94 } 95 doc := &testDoc{} 96 if err = db.Get(context.Background(), expectedDoc.ID).ScanDoc(&doc); err != nil { 97 ctx.Fatalf("Failed to scan doc: %s\n", err) 98 } 99 if strings.HasPrefix(expectedDoc.ID, "_local/") { 100 // Revisions are meaningless for _local docs 101 return 102 } 103 if rev != doc.Rev { 104 ctx.Errorf("Unexpected rev. Expected: %s, Actual: %s", doc.Rev, rev) 105 } 106 }) 107 }