github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/config/config_hierarchy_test.go (about)

     1  // Copyright 2019 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 "testing"
    18  
    19  func TestCHGetAndSet(t *testing.T) {
    20  	ch := NewConfigHierarchy()
    21  	ch.AddConfig("priority", NewMapConfig(map[string]string{}))
    22  	ch.AddConfig("fallback", NewMapConfig(map[string]string{}))
    23  
    24  	priority, ok := ch.GetConfig("priority")
    25  
    26  	if !ok {
    27  		t.Fatal("Could not get priority config from hierarchy.")
    28  	}
    29  
    30  	fallback, ok := ch.GetConfig("fallback")
    31  
    32  	if !ok {
    33  		t.Fatal("Could not get fallback config from hierarchy.")
    34  	}
    35  
    36  	err := priority.SetStrings(map[string]string{
    37  		"both":          "priority",
    38  		"only_priority": "only_priority_value",
    39  	})
    40  
    41  	if err != nil {
    42  		t.Fatal("Unable to set strings in priority config")
    43  	}
    44  
    45  	err = fallback.SetStrings(map[string]string{
    46  		"both":          "whatever",
    47  		"only_fallback": "only_fallback_value",
    48  	})
    49  
    50  	if err != nil {
    51  		t.Fatal("Unable to set strings in priority config")
    52  	}
    53  
    54  	if str, err := ch.GetString("both"); err != nil || str != "priority" {
    55  		t.Error("\"both\" key error.")
    56  	}
    57  
    58  	if str, err := ch.GetString("fallback::both"); err != nil || str != "whatever" {
    59  		t.Error("\"fallback::both\" key error.")
    60  	}
    61  
    62  	if str, err := ch.GetString("only_priority"); err != nil || str != "only_priority_value" {
    63  		t.Error("\"only_priority\" key error.")
    64  	}
    65  
    66  	if str, err := ch.GetString("only_fallback"); err != nil || str != "only_fallback_value" {
    67  		t.Error("\"only_fallback\" key error.")
    68  	}
    69  
    70  	if _, err := ch.GetString("missing"); err != ErrConfigParamNotFound {
    71  		t.Error("\"missing\" returned the wrong error.")
    72  	}
    73  
    74  	if _, err := ch.GetString("priority::missing"); err != ErrConfigParamNotFound {
    75  		t.Error("\"missing\" returned the wrong error.")
    76  	}
    77  
    78  	if _, err := ch.GetString("invalid::missing"); err == nil || err == ErrConfigParamNotFound {
    79  		t.Error("invalid namespace returned unexpected result")
    80  	}
    81  
    82  	ch.SetStrings(map[string]string{
    83  		"priority::new":           "new_value",
    84  		"fallback::only_fallback": "updated",
    85  	})
    86  
    87  	expectedIter := map[string]string{
    88  		"priority::new":           "new_value",
    89  		"priority::both":          "priority",
    90  		"priority::only_priority": "only_priority_value",
    91  		"fallback::both":          "whatever",
    92  		"fallback::only_fallback": "updated",
    93  	}
    94  
    95  	testIteration(t, expectedIter, ch)
    96  
    97  	err = ch.Unset([]string{"priority::only_priority"})
    98  
    99  	if err != nil {
   100  		t.Error("Failed to unset value. " + err.Error())
   101  	}
   102  
   103  	if _, err := ch.GetString("only_priority"); err != ErrConfigParamNotFound {
   104  		t.Error("Unset failed.")
   105  	}
   106  }
   107  
   108  func TestSplitParamName(t *testing.T) {
   109  	testSplitParamName(t, "", "", "")
   110  	testSplitParamName(t, "param_no_ns", "", "param_no_ns")
   111  	testSplitParamName(t, "ns::param", "ns", "param")
   112  	testSplitParamName(t, "ns::param::token", "ns", "param::token")
   113  	testSplitParamName(t, "NS :: param :: token ", "ns", "param::token")
   114  }
   115  
   116  func testSplitParamName(t *testing.T, paramName, expectedNS, expectedName string) {
   117  	ns, name := splitParamName(paramName)
   118  
   119  	if ns != expectedNS || expectedName != name {
   120  		t.Error("Error splitting " + paramName)
   121  	}
   122  }