...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/copy.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  
    18  	"github.com/go-kivik/kivik/v4"
    19  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    20  )
    21  
    22  func init() {
    23  	kt.Register("Copy", _copy)
    24  }
    25  
    26  func _copy(ctx *kt.Context) {
    27  	ctx.RunRW(func(ctx *kt.Context) {
    28  		dbname := ctx.TestDB()
    29  		db := ctx.Admin.DB(dbname, ctx.Options("db"))
    30  		if err := db.Err(); err != nil {
    31  			ctx.Fatalf("Failed to open db: %s", err)
    32  		}
    33  
    34  		doc := map[string]string{
    35  			"_id":  "foo",
    36  			"name": "Robert",
    37  		}
    38  		rev, err := db.Put(context.Background(), doc["_id"], doc)
    39  		if err != nil {
    40  			ctx.Fatalf("Failed to create source doc: %s", err)
    41  		}
    42  		doc["_rev"] = rev
    43  
    44  		ddoc := map[string]string{
    45  			"_id":  "_design/foo",
    46  			"name": "Robert",
    47  		}
    48  		rev, err = db.Put(context.Background(), ddoc["_id"], ddoc)
    49  		if err != nil {
    50  			ctx.Fatalf("Failed to create source design doc: %s", err)
    51  		}
    52  		ddoc["_rev"] = rev
    53  
    54  		local := map[string]string{
    55  			"_id":  "_local/foo",
    56  			"name": "Robert",
    57  		}
    58  		rev, err = db.Put(context.Background(), local["_id"], local)
    59  		if err != nil {
    60  			ctx.Fatalf("Failed to create source design doc: %s", err)
    61  		}
    62  		local["_rev"] = rev
    63  
    64  		ctx.Run("group", func(ctx *kt.Context) {
    65  			ctx.RunAdmin(func(ctx *kt.Context) {
    66  				copyTest(ctx, ctx.Admin, dbname, doc)
    67  				copyTest(ctx, ctx.Admin, dbname, ddoc)
    68  				copyTest(ctx, ctx.Admin, dbname, local)
    69  			})
    70  			ctx.RunNoAuth(func(ctx *kt.Context) {
    71  				copyTest(ctx, ctx.NoAuth, dbname, doc)
    72  				copyTest(ctx, ctx.NoAuth, dbname, ddoc)
    73  				copyTest(ctx, ctx.NoAuth, dbname, local)
    74  			})
    75  		})
    76  	})
    77  }
    78  
    79  func copyTest(ctx *kt.Context, client *kivik.Client, dbname string, source map[string]string) {
    80  	ctx.Run(source["_id"], func(ctx *kt.Context) {
    81  		ctx.Parallel()
    82  		db := client.DB(dbname, ctx.Options("db"))
    83  		if err := db.Err(); err != nil {
    84  			ctx.Fatalf("Failed to open db: %s", err)
    85  		}
    86  		targetID := ctx.TestDBName()
    87  		rev, err := db.Copy(context.Background(), targetID, source["_id"])
    88  		if !ctx.IsExpectedSuccess(err) {
    89  			return
    90  		}
    91  		ctx.Run("RevCopy", func(ctx *kt.Context) {
    92  			cp := map[string]string{
    93  				"_id":  targetID,
    94  				"name": "Bob",
    95  				"_rev": rev,
    96  			}
    97  			if _, e := db.Put(context.Background(), targetID, cp); e != nil {
    98  				ctx.Fatalf("Failed to update copy: %s", e)
    99  			}
   100  			targetID2 := ctx.TestDBName()
   101  			if _, e := db.Copy(context.Background(), targetID2, targetID, kivik.Rev(rev)); e != nil {
   102  				ctx.Fatalf("Failed to copy doc with rev option: %s", e)
   103  			}
   104  			var readCopy map[string]string
   105  			if err = db.Get(context.Background(), targetID2).ScanDoc(&readCopy); err != nil {
   106  				ctx.Fatalf("Failed to scan copy: %s", err)
   107  			}
   108  			if readCopy["name"] != "Robert" {
   109  				ctx.Errorf("Copy-with-rev failed. Name = %s, expected %s", readCopy["name"], "Robert")
   110  			}
   111  		})
   112  	})
   113  }
   114  

View as plain text