github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/db/createdoc.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 18 "github.com/go-kivik/kivik/v4" 19 "github.com/go-kivik/kivik/v4/kiviktest/kt" 20 ) 21 22 func init() { 23 kt.Register("CreateDoc", createDoc) 24 } 25 26 func createDoc(ctx *kt.Context) { 27 ctx.RunRW(func(ctx *kt.Context) { 28 dbname := ctx.TestDB() 29 ctx.Run("group", func(ctx *kt.Context) { 30 ctx.RunAdmin(func(ctx *kt.Context) { 31 ctx.Parallel() 32 testCreate(ctx, ctx.Admin, dbname) 33 }) 34 ctx.RunNoAuth(func(ctx *kt.Context) { 35 ctx.Parallel() 36 testCreate(ctx, ctx.NoAuth, dbname) 37 }) 38 }) 39 }) 40 } 41 42 func testCreate(ctx *kt.Context, client *kivik.Client, dbname string) { 43 db := client.DB(dbname, ctx.Options("db")) 44 if err := db.Err(); err != nil { 45 ctx.Fatalf("Failed to connect to database: %s", err) 46 } 47 ctx.Run("WithoutID", func(ctx *kt.Context) { 48 ctx.Parallel() 49 err := kt.Retry(func() error { 50 _, _, err := db.CreateDoc(context.Background(), map[string]string{"foo": "bar"}) 51 return err 52 }) 53 ctx.CheckError(err) 54 }) 55 ctx.Run("WithID", func(ctx *kt.Context) { 56 ctx.Parallel() 57 id := ctx.TestDBName() 58 var docID string 59 err := kt.Retry(func() error { 60 var err error 61 docID, _, err = db.CreateDoc(context.Background(), map[string]string{"foo": "bar", "_id": id}) 62 return err 63 }) 64 if !ctx.IsExpectedSuccess(err) { 65 return 66 } 67 if id != docID { 68 ctx.Errorf("CreateDoc didn't honor provided ID. Expected '%s', Got '%s'", id, docID) 69 } 70 }) 71 }