github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/config/prefix_config_test.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"sort"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  var ConfigVals = map[string]string{
    25  	"scopeA.k1": "v1",
    26  	"scopeA.k2": "v2",
    27  	"scopeB.k3": "v3",
    28  	"k1":        "v1",
    29  }
    30  
    31  func newConfigVals() map[string]string {
    32  	newConfig := make(map[string]string)
    33  	for k, v := range ConfigVals {
    34  		newConfig[k] = v
    35  	}
    36  	return newConfig
    37  }
    38  
    39  func newPrefixConfig(prefix string) PrefixConfig {
    40  
    41  	mc := NewMapConfig(newConfigVals())
    42  	return NewPrefixConfig(mc, prefix)
    43  
    44  }
    45  
    46  func TestPrefixConfigSet(t *testing.T) {
    47  	conf := newPrefixConfig("test")
    48  	conf.SetStrings(newConfigVals())
    49  	v1, _ := conf.c.GetString("test.k1")
    50  	assert.Equal(t, v1, "v1")
    51  }
    52  
    53  func TestPrefixConfigGet(t *testing.T) {
    54  	t.Run("test GetString", func(t *testing.T) {
    55  		conf := newPrefixConfig("scopeA")
    56  		v1, _ := conf.GetString("k1")
    57  		assert.Equal(t, "v1", v1)
    58  	})
    59  
    60  	t.Run("test GetString fails out of scope", func(t *testing.T) {
    61  		conf := newPrefixConfig("scopeA")
    62  		_, err := conf.GetString("k3")
    63  		assert.Equal(t, err, ErrConfigParamNotFound)
    64  	})
    65  
    66  	t.Run("test GetStringofDefault", func(t *testing.T) {
    67  		conf := newPrefixConfig("scopeA")
    68  		v1, _ := conf.GetString("k1")
    69  		assert.Equal(t, "v1", v1)
    70  	})
    71  
    72  	t.Run("test GetStringOrDefault fails out of scope", func(t *testing.T) {
    73  		conf := newPrefixConfig("scopeA")
    74  		res := conf.GetStringOrDefault("k3", "default")
    75  		assert.Equal(t, "default", res)
    76  	})
    77  }
    78  
    79  func TestPrefixConfigUnset(t *testing.T) {
    80  	t.Run("test Unset", func(t *testing.T) {
    81  		conf := newPrefixConfig("scopeA")
    82  		err := conf.Unset([]string{"k1"})
    83  		assert.NoError(t, err)
    84  		res := conf.GetStringOrDefault("k3", "default")
    85  		assert.Equal(t, "default", res)
    86  	})
    87  
    88  	t.Run("test Unset doesn't affect other scope", func(t *testing.T) {
    89  		conf := newPrefixConfig("scopeA")
    90  		err := conf.Unset([]string{"k1"})
    91  		assert.NoError(t, err)
    92  		res := conf.c.GetStringOrDefault("k1", "")
    93  		assert.Equal(t, "v1", res)
    94  	})
    95  }
    96  
    97  func TestPrefixConfigSize(t *testing.T) {
    98  	conf := newPrefixConfig("scopeA")
    99  	size := conf.Size()
   100  	assert.Equal(t, size, 2)
   101  }
   102  
   103  func TestPrefixConfigIter(t *testing.T) {
   104  	conf := newPrefixConfig("scopeA")
   105  	keys := make([]string, 0, 6)
   106  	conf.Iter(func(k, v string) bool {
   107  		keys = append(keys, k)
   108  		return false
   109  	})
   110  	sort.Strings(keys)
   111  	assert.Equal(t, []string{"k1", "k2"}, keys)
   112  }