...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/get.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  	"gitlab.com/flimzy/testy"
    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("Get", get)
    26  }
    27  
    28  type testDoc struct {
    29  	ID   string `json:"_id"`
    30  	Rev  string `json:"_rev,omitempty"`
    31  	Name string `json:"name"`
    32  	Age  int    `json:"age"`
    33  }
    34  
    35  func get(ctx *kt.Context) {
    36  	ctx.RunRW(func(ctx *kt.Context) {
    37  		const age = 32
    38  		dbName := ctx.TestDB()
    39  		db := ctx.Admin.DB(dbName, ctx.Options("db"))
    40  		if err := db.Err(); err != nil {
    41  			ctx.Fatalf("Failed to connect to test db: %s", err)
    42  		}
    43  
    44  		doc := &testDoc{
    45  			ID:   "bob",
    46  			Name: "Robert",
    47  			Age:  age,
    48  		}
    49  		rev, err := db.Put(context.Background(), doc.ID, doc)
    50  		if err != nil {
    51  			ctx.Fatalf("Failed to create doc in test db: %s", err)
    52  		}
    53  		doc.Rev = rev
    54  
    55  		ddoc := &testDoc{
    56  			ID:   "_design/foo",
    57  			Name: "Designer",
    58  		}
    59  		rev, err = db.Put(context.Background(), ddoc.ID, ddoc)
    60  		if err != nil {
    61  			ctx.Fatalf("Failed to create design doc in test db: %s", err)
    62  		}
    63  		ddoc.Rev = rev
    64  
    65  		local := &testDoc{
    66  			ID:   "_local/foo",
    67  			Name: "Designer",
    68  		}
    69  		rev, err = db.Put(context.Background(), local.ID, local)
    70  		if err != nil {
    71  			ctx.Fatalf("Failed to create local doc in test db: %s", err)
    72  		}
    73  		local.Rev = rev
    74  
    75  		ctx.Run("group", func(ctx *kt.Context) {
    76  			ctx.RunAdmin(func(ctx *kt.Context) {
    77  				ctx.Parallel()
    78  				db := ctx.Admin.DB(dbName, ctx.Options("db"))
    79  				if err := db.Err(); !ctx.IsExpectedSuccess(err) {
    80  					return
    81  				}
    82  				testGet(ctx, db, doc)
    83  				testGet(ctx, db, ddoc)
    84  				testGet(ctx, db, local)
    85  				testGet(ctx, db, &testDoc{ID: "bogus"})
    86  			})
    87  			ctx.RunNoAuth(func(ctx *kt.Context) {
    88  				ctx.Parallel()
    89  				db := ctx.NoAuth.DB(dbName, ctx.Options("db"))
    90  				if err := db.Err(); !ctx.IsExpectedSuccess(err) {
    91  					return
    92  				}
    93  				testGet(ctx, db, doc)
    94  				testGet(ctx, db, ddoc)
    95  				testGet(ctx, db, local)
    96  				testGet(ctx, db, &testDoc{ID: "bogus"})
    97  			})
    98  		})
    99  	})
   100  }
   101  
   102  func testGet(ctx *kt.Context, db *kivik.DB, expectedDoc *testDoc) {
   103  	ctx.Run(expectedDoc.ID, func(ctx *kt.Context) {
   104  		ctx.Parallel()
   105  		doc := &testDoc{}
   106  		if !ctx.IsExpectedSuccess(db.Get(context.Background(), expectedDoc.ID).ScanDoc(&doc)) {
   107  			return
   108  		}
   109  		if d := testy.DiffAsJSON(expectedDoc, doc); d != nil {
   110  			ctx.Errorf("Fetched document not as expected:\n%s\n", d)
   111  		}
   112  	})
   113  }
   114  

View as plain text