github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/db/createIndex.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("CreateIndex", createIndex)
    24  }
    25  
    26  func createIndex(ctx *kt.Context) {
    27  	ctx.RunRW(func(ctx *kt.Context) {
    28  		ctx.RunAdmin(func(ctx *kt.Context) {
    29  			ctx.Parallel()
    30  			testCreateIndex(ctx, ctx.Admin)
    31  		})
    32  		ctx.RunNoAuth(func(ctx *kt.Context) {
    33  			ctx.Parallel()
    34  			testCreateIndex(ctx, ctx.NoAuth)
    35  		})
    36  	})
    37  }
    38  
    39  func testCreateIndex(ctx *kt.Context, client *kivik.Client) {
    40  	dbname := ctx.TestDB()
    41  	db := client.DB(dbname, ctx.Options("db"))
    42  	if err := db.Err(); err != nil {
    43  		ctx.Fatalf("Failed to open db: %s", err)
    44  	}
    45  	ctx.Run("group", func(ctx *kt.Context) {
    46  		ctx.Run("Valid", func(ctx *kt.Context) {
    47  			doCreateIndex(ctx, db, `{"fields":["foo"]}`)
    48  		})
    49  		ctx.Run("NilIndex", func(ctx *kt.Context) {
    50  			doCreateIndex(ctx, db, nil)
    51  		})
    52  		ctx.Run("BlankIndex", func(ctx *kt.Context) {
    53  			doCreateIndex(ctx, db, "")
    54  		})
    55  		ctx.Run("EmptyIndex", func(ctx *kt.Context) {
    56  			doCreateIndex(ctx, db, "{}")
    57  		})
    58  		ctx.Run("InvalidIndex", func(ctx *kt.Context) {
    59  			doCreateIndex(ctx, db, `{"oink":true}`)
    60  		})
    61  		ctx.Run("InvalidJSON", func(ctx *kt.Context) {
    62  			doCreateIndex(ctx, db, `chicken`)
    63  		})
    64  	})
    65  }
    66  
    67  func doCreateIndex(ctx *kt.Context, db *kivik.DB, index interface{}) {
    68  	ctx.Parallel()
    69  	err := kt.Retry(func() error {
    70  		return db.CreateIndex(context.Background(), "", "", index)
    71  	})
    72  	ctx.CheckError(err)
    73  }