...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/db/getindexes.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("GetIndexes", getIndexes)
    26  }
    27  
    28  func getIndexes(ctx *kt.Context) {
    29  	ctx.RunAdmin(func(ctx *kt.Context) {
    30  		ctx.Parallel()
    31  		roGetIndexesTests(ctx, ctx.Admin)
    32  	})
    33  	ctx.RunNoAuth(func(ctx *kt.Context) {
    34  		ctx.Parallel()
    35  		roGetIndexesTests(ctx, ctx.NoAuth)
    36  	})
    37  	ctx.RunRW(func(ctx *kt.Context) {
    38  		ctx.RunAdmin(func(ctx *kt.Context) {
    39  			ctx.Parallel()
    40  			rwGetIndexesTests(ctx, ctx.Admin)
    41  		})
    42  		ctx.RunNoAuth(func(ctx *kt.Context) {
    43  			ctx.Parallel()
    44  			rwGetIndexesTests(ctx, ctx.NoAuth)
    45  		})
    46  	})
    47  }
    48  
    49  func roGetIndexesTests(ctx *kt.Context, client *kivik.Client) {
    50  	databases := ctx.MustStringSlice("databases")
    51  	for _, dbname := range databases {
    52  		func(dbname string) {
    53  			ctx.Run(dbname, func(ctx *kt.Context) {
    54  				ctx.Parallel()
    55  				testGetIndexes(ctx, client, dbname, ctx.Interface("indexes"))
    56  			})
    57  		}(dbname)
    58  	}
    59  }
    60  
    61  func rwGetIndexesTests(ctx *kt.Context, client *kivik.Client) {
    62  	dbname := ctx.TestDB()
    63  	dba := ctx.Admin.DB(dbname, ctx.Options("db"))
    64  	if err := dba.Err(); err != nil {
    65  		ctx.Fatalf("Failed to open db as admin: %s", err)
    66  	}
    67  	if err := dba.CreateIndex(context.Background(), "foo", "bar", `{"fields":["foo"]}`); err != nil {
    68  		ctx.Fatalf("Failed to create index: %s", err)
    69  	}
    70  	indexes := ctx.Interface("indexes")
    71  	if indexes == nil {
    72  		indexes = []kivik.Index{
    73  			kt.AllDocsIndex,
    74  			{
    75  				DesignDoc: "_design/foo",
    76  				Name:      "bar",
    77  				Type:      "json",
    78  				Definition: map[string]interface{}{
    79  					"fields": []map[string]string{
    80  						{"foo": "asc"},
    81  					},
    82  				},
    83  			},
    84  		}
    85  		testGetIndexes(ctx, client, dbname, indexes)
    86  	}
    87  }
    88  
    89  func testGetIndexes(ctx *kt.Context, client *kivik.Client, dbname string, expected interface{}) {
    90  	db := client.DB(dbname, ctx.Options("db"))
    91  	if err := db.Err(); err != nil {
    92  		ctx.Fatalf("Failed to open db: %s", err)
    93  	}
    94  	indexes, err := db.GetIndexes(context.Background())
    95  	if !ctx.IsExpectedSuccess(err) {
    96  		return
    97  	}
    98  	if d := testy.DiffAsJSON(expected, indexes); d != nil {
    99  		ctx.Errorf("Indexes differ from expectation:\n%s\n", d)
   100  	}
   101  }
   102  

View as plain text