...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/stats.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  	"fmt"
    18  
    19  	"github.com/go-kivik/kivik/v4"
    20  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    21  )
    22  
    23  func init() {
    24  	kt.Register("Stats", stats)
    25  }
    26  
    27  func stats(ctx *kt.Context) {
    28  	ctx.RunAdmin(func(ctx *kt.Context) {
    29  		ctx.Parallel()
    30  		roTests(ctx, ctx.Admin)
    31  	})
    32  	ctx.RunNoAuth(func(ctx *kt.Context) {
    33  		ctx.Parallel()
    34  		roTests(ctx, ctx.NoAuth)
    35  	})
    36  	ctx.RunRW(func(ctx *kt.Context) {
    37  		ctx.Parallel()
    38  		ctx.RunAdmin(func(ctx *kt.Context) {
    39  			ctx.Parallel()
    40  			rwTests(ctx, ctx.Admin)
    41  		})
    42  		ctx.RunNoAuth(func(ctx *kt.Context) {
    43  			ctx.Parallel()
    44  			rwTests(ctx, ctx.NoAuth)
    45  		})
    46  	})
    47  }
    48  
    49  func rwTests(ctx *kt.Context, client *kivik.Client) {
    50  	dbname := ctx.TestDB()
    51  	db := ctx.Admin.DB(dbname, ctx.Options("db"))
    52  	if err := db.Err(); err != nil {
    53  		ctx.Fatalf("Failed to connect to db: %s", err)
    54  	}
    55  	for i := 0; i < 10; i++ {
    56  		id := fmt.Sprintf("%d", i)
    57  		rev, err := db.Put(context.Background(), id, struct{}{})
    58  		if err != nil {
    59  			ctx.Fatalf("Failed to create document ID %s: %s", id, err)
    60  		}
    61  		const deleteThreshold = 5
    62  		if i > deleteThreshold {
    63  			if _, err = db.Delete(context.Background(), id, rev); err != nil {
    64  				ctx.Fatalf("Failed to delete document ID %s: %s", id, err)
    65  			}
    66  		}
    67  	}
    68  	const docCount = 6
    69  	testDBInfo(ctx, client, dbname, docCount)
    70  }
    71  
    72  func roTests(ctx *kt.Context, client *kivik.Client) {
    73  	for _, dbname := range ctx.MustStringSlice("databases") {
    74  		func(dbname string) {
    75  			ctx.Run(dbname, func(ctx *kt.Context) {
    76  				ctx.Parallel()
    77  				testDBInfo(ctx, client, dbname, 0)
    78  			})
    79  		}(dbname)
    80  	}
    81  }
    82  
    83  func testDBInfo(ctx *kt.Context, client *kivik.Client, dbname string, docCount int64) {
    84  	stats, err := client.DB(dbname, ctx.Options("db")).Stats(context.Background())
    85  	if !ctx.IsExpectedSuccess(err) {
    86  		return
    87  	}
    88  	if stats.Name != dbname {
    89  		ctx.Errorf("Name: Expected '%s', actual '%s'", dbname, stats.Name)
    90  	}
    91  	if docCount > 0 {
    92  		if docCount != stats.DocCount {
    93  			ctx.Errorf("DocCount: Expected %d, actual %d", docCount, stats.DocCount)
    94  		}
    95  	}
    96  }
    97  

View as plain text