...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/kt/config.go

Documentation: github.com/go-kivik/kivik/v4/kiviktest/kt

     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 kt
    14  
    15  import (
    16  	"strings"
    17  	"testing"
    18  
    19  	kivik "github.com/go-kivik/kivik/v4"
    20  )
    21  
    22  // AllDocsIndex is the default index for _all_docs
    23  var AllDocsIndex = kivik.Index{
    24  	Name: "_all_docs", Type: "special",
    25  	Definition: map[string]interface{}{"fields": []map[string]string{{"_id": "asc"}}},
    26  }
    27  
    28  // SuiteConfig represents the configuration for a test suite.
    29  type SuiteConfig map[string]interface{}
    30  
    31  func name(t *testing.T) string {
    32  	t.Helper()
    33  	name := t.Name()
    34  	return name[strings.Index(name, "/")+1:]
    35  }
    36  
    37  // get looks for the requested key at the current level, and if not found it
    38  // looks at the parent key.
    39  func (c SuiteConfig) get(name, key string) interface{} {
    40  	var k string
    41  	if name == "" {
    42  		k = key
    43  	} else {
    44  		k = name + "." + key
    45  	}
    46  	v, ok := c[k]
    47  	if ok {
    48  		return v
    49  	}
    50  	if name == "" {
    51  		return nil
    52  	}
    53  	if !strings.Contains(name, "/") {
    54  		return c.get("", key)
    55  	}
    56  	// Try the parent
    57  	return c.get(name[0:strings.LastIndex(name, "/")], key)
    58  }
    59  
    60  // Interface returns the configuration value as an interface{}.
    61  func (c SuiteConfig) Interface(t *testing.T, key string) interface{} {
    62  	t.Helper()
    63  	return c.get(name(t), key)
    64  }
    65  
    66  // Bool returns the boolean value of the key.
    67  func (c SuiteConfig) Bool(t *testing.T, key string) bool {
    68  	t.Helper()
    69  	b, _ := c.Interface(t, key).(bool)
    70  	return b
    71  }
    72  
    73  // Skip will skip the currently running test if configuration dictates.
    74  func (c SuiteConfig) Skip(t *testing.T) {
    75  	t.Helper()
    76  	if c.Bool(t, "skip") {
    77  		t.Skip("Test skipped by suite configuration")
    78  	}
    79  }
    80  
    81  // StringSlice returns a string slice.
    82  func (c SuiteConfig) StringSlice(t *testing.T, key string) []string {
    83  	t.Helper()
    84  	v, _ := c.Interface(t, key).([]string)
    85  	return v
    86  }
    87  
    88  // Int returns an int.
    89  func (c SuiteConfig) Int(t *testing.T, key string) int {
    90  	t.Helper()
    91  	v, _ := c.Interface(t, key).(int)
    92  	return v
    93  }
    94  
    95  // String returns a string.
    96  func (c SuiteConfig) String(t *testing.T, key string) string {
    97  	t.Helper()
    98  	v, _ := c.Interface(t, key).(string)
    99  	return v
   100  }
   101  

View as plain text