github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/db/delattachment.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("DeleteAttachment", delAttachment) 24 } 25 26 func delAttachment(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 testDeleteAttachments(ctx, ctx.Admin, dbname, "foo.txt") 33 testDeleteAttachments(ctx, ctx.Admin, dbname, "NotFound") 34 testDeleteAttachmentsDDoc(ctx, ctx.Admin, dbname, "foo.txt") 35 testDeleteAttachmentNoDoc(ctx, ctx.Admin, dbname) 36 }) 37 ctx.RunNoAuth(func(ctx *kt.Context) { 38 ctx.Parallel() 39 testDeleteAttachments(ctx, ctx.NoAuth, dbname, "foo.txt") 40 testDeleteAttachments(ctx, ctx.NoAuth, dbname, "NotFound") 41 testDeleteAttachmentsDDoc(ctx, ctx.NoAuth, dbname, "foo.txt") 42 testDeleteAttachmentNoDoc(ctx, ctx.NoAuth, dbname) 43 }) 44 }) 45 }) 46 } 47 48 func testDeleteAttachmentNoDoc(ctx *kt.Context, client *kivik.Client, dbname string) { 49 db := client.DB(dbname, ctx.Options("db")) 50 if err := db.Err(); err != nil { 51 ctx.Fatalf("Failed to connect to db") 52 } 53 ctx.Run("NoDoc", func(ctx *kt.Context) { 54 ctx.Parallel() 55 _, err := db.DeleteAttachment(context.Background(), "nonexistantdoc", "2-4259cd84694a6345d6c534ed65f1b30b", "foo.txt") 56 ctx.CheckError(err) 57 }) 58 } 59 60 func testDeleteAttachments(ctx *kt.Context, client *kivik.Client, dbname, filename string) { 61 ctx.Run(filename, func(ctx *kt.Context) { 62 doDeleteAttachmentTest(ctx, client, dbname, ctx.TestDBName(), filename) 63 }) 64 } 65 66 func testDeleteAttachmentsDDoc(ctx *kt.Context, client *kivik.Client, dbname, filename string) { 67 ctx.Run("DesignDoc/"+filename, func(ctx *kt.Context) { 68 doDeleteAttachmentTest(ctx, client, dbname, "_design/"+ctx.TestDBName(), filename) 69 }) 70 } 71 72 func doDeleteAttachmentTest(ctx *kt.Context, client *kivik.Client, dbname, docID, filename string) { 73 db := client.DB(dbname, ctx.Options("db")) 74 if err := db.Err(); err != nil { 75 ctx.Fatalf("Failed to connect to db") 76 } 77 ctx.Parallel() 78 adb := ctx.Admin.DB(dbname, ctx.Options("db")) 79 if err := adb.Err(); err != nil { 80 ctx.Fatalf("Failed to open db: %s", err) 81 } 82 doc := map[string]interface{}{ 83 "_id": docID, 84 "_attachments": map[string]interface{}{ 85 "foo.txt": map[string]interface{}{ 86 "content_type": "text/plain", 87 "data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ=", 88 }, 89 }, 90 } 91 rev, err := adb.Put(context.Background(), docID, doc) 92 if err != nil { 93 ctx.Fatalf("Failed to create doc: %s", err) 94 } 95 rev, err = db.DeleteAttachment(context.Background(), docID, rev, filename) 96 if !ctx.IsExpectedSuccess(err) { 97 return 98 } 99 var i interface{} 100 if err = db.Get(context.Background(), docID, kivik.Rev(rev)).ScanDoc(&i); err != nil { 101 ctx.Fatalf("Failed to get deleted doc: %s", err) 102 } 103 }