...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/putattachment.go

Documentation: github.com/go-kivik/kivik/v4/kiviktest/db

     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  	"io"
    18  	"strings"
    19  
    20  	"github.com/go-kivik/kivik/v4"
    21  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    22  )
    23  
    24  func init() {
    25  	kt.Register("PutAttachment", putAttachment)
    26  }
    27  
    28  func putAttachment(ctx *kt.Context) {
    29  	ctx.RunRW(func(ctx *kt.Context) {
    30  		dbname := ctx.TestDB()
    31  		ctx.Run("group", func(ctx *kt.Context) {
    32  			ctx.RunAdmin(func(ctx *kt.Context) {
    33  				ctx.Parallel()
    34  				testPutAttachment(ctx, ctx.Admin, dbname)
    35  			})
    36  			ctx.RunNoAuth(func(ctx *kt.Context) {
    37  				ctx.Parallel()
    38  				testPutAttachment(ctx, ctx.NoAuth, dbname)
    39  			})
    40  		})
    41  	})
    42  }
    43  
    44  func testPutAttachment(ctx *kt.Context, client *kivik.Client, dbname string) {
    45  	db := client.DB(dbname, ctx.Options("db"))
    46  	if err := db.Err(); err != nil {
    47  		ctx.Fatalf("Failed to open db: %s", err)
    48  	}
    49  	adb := ctx.Admin.DB(dbname, ctx.Options("db"))
    50  	if err := adb.Err(); err != nil {
    51  		ctx.Fatalf("Failed to open admin db: %s", err)
    52  	}
    53  	ctx.Run("Update", func(ctx *kt.Context) {
    54  		ctx.Parallel()
    55  		var docID, rev string
    56  		err := kt.Retry(func() error {
    57  			var e error
    58  			docID, rev, e = adb.CreateDoc(context.Background(), map[string]string{"name": "Robert"})
    59  			return e
    60  		})
    61  		if err != nil {
    62  			ctx.Fatalf("Failed to create doc: %s", err)
    63  		}
    64  		err = kt.Retry(func() error {
    65  			att := &kivik.Attachment{
    66  				Filename:    "test.txt",
    67  				ContentType: "text/plain",
    68  				Content:     stringReadCloser(),
    69  			}
    70  			_, err = db.PutAttachment(context.Background(), docID, att, kivik.Rev(rev))
    71  			return err
    72  		})
    73  		ctx.CheckError(err)
    74  	})
    75  	ctx.Run("Create", func(ctx *kt.Context) {
    76  		ctx.Parallel()
    77  		docID := ctx.TestDBName()
    78  		err := kt.Retry(func() error {
    79  			att := &kivik.Attachment{
    80  				Filename:    "test.txt",
    81  				ContentType: "text/plain",
    82  				Content:     stringReadCloser(),
    83  			}
    84  			_, err := db.PutAttachment(context.Background(), docID, att)
    85  			return err
    86  		})
    87  		ctx.CheckError(err)
    88  	})
    89  	ctx.Run("Conflict", func(ctx *kt.Context) {
    90  		ctx.Parallel()
    91  		var docID string
    92  		err2 := kt.Retry(func() error {
    93  			var e error
    94  			docID, _, e = adb.CreateDoc(context.Background(), map[string]string{"name": "Robert"})
    95  			return e
    96  		})
    97  		if err2 != nil {
    98  			ctx.Fatalf("Failed to create doc: %s", err2)
    99  		}
   100  		err := kt.Retry(func() error {
   101  			att := &kivik.Attachment{
   102  				Filename:    "test.txt",
   103  				ContentType: "text/plain",
   104  				Content:     stringReadCloser(),
   105  			}
   106  			_, err := db.PutAttachment(context.Background(), docID, att, kivik.Rev("5-20bd3c7d7d6b81390c6679d8bae8795b"))
   107  			return err
   108  		})
   109  		ctx.CheckError(err)
   110  	})
   111  	ctx.Run("UpdateDesignDoc", func(ctx *kt.Context) {
   112  		ctx.Parallel()
   113  		docID := "_design/" + ctx.TestDBName()
   114  		doc := map[string]string{
   115  			"_id": docID,
   116  		}
   117  		var rev string
   118  		err := kt.Retry(func() error {
   119  			var err error
   120  			rev, err = adb.Put(context.Background(), docID, doc)
   121  			return err
   122  		})
   123  		if err != nil {
   124  			ctx.Fatalf("Failed to create design doc: %s", err)
   125  		}
   126  		err = kt.Retry(func() error {
   127  			att := &kivik.Attachment{
   128  				Filename:    "test.txt",
   129  				ContentType: "text/plain",
   130  				Content:     stringReadCloser(),
   131  			}
   132  			_, err = db.PutAttachment(context.Background(), docID, att, kivik.Rev(rev))
   133  			return err
   134  		})
   135  		ctx.CheckError(err)
   136  	})
   137  	ctx.Run("CreateDesignDoc", func(ctx *kt.Context) {
   138  		ctx.Parallel()
   139  		docID := "_design/" + ctx.TestDBName()
   140  		err := kt.Retry(func() error {
   141  			att := &kivik.Attachment{
   142  				Filename:    "test.txt",
   143  				ContentType: "text/plain",
   144  				Content:     stringReadCloser(),
   145  			}
   146  			_, err := db.PutAttachment(context.Background(), docID, att)
   147  			return err
   148  		})
   149  		ctx.CheckError(err)
   150  	})
   151  }
   152  
   153  func stringReadCloser() io.ReadCloser {
   154  	return io.NopCloser(strings.NewReader("test content"))
   155  }
   156  

View as plain text