github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/config/store/store_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://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,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/config/store/store_test.go
    14  package store
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/tickoalcantara12/micro/v3/service/config"
    21  	"github.com/tickoalcantara12/micro/v3/service/config/secrets"
    22  	"github.com/tickoalcantara12/micro/v3/service/store/memory"
    23  )
    24  
    25  type conf1 struct {
    26  	A string  `json:"a"`
    27  	B int64   `json:"b"`
    28  	C float64 `json:"c"`
    29  	D bool    `json:"d"`
    30  }
    31  
    32  func TestBasics(t *testing.T) {
    33  	conf, err := NewConfig(memory.NewStore(), "micro")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	testBasics(conf, t)
    38  	// We need to get a new config because existing config so
    39  	conf, err = NewConfig(memory.NewStore(), "micro1")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	secrets, err := secrets.NewSecrets(conf, "somethingRandomButLongEnough32by")
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	testBasics(secrets, t)
    48  }
    49  
    50  func testBasics(c config.Config, t *testing.T) {
    51  	original := &conf1{
    52  		"Hi", int64(42), float64(42.2), true,
    53  	}
    54  	err := c.Set("key", original)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	getted := &conf1{}
    59  	val, err := c.Get("key")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	err = val.Scan(getted)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if !reflect.DeepEqual(original, getted) {
    68  		t.Fatalf("Not equal: %v and %v", original, getted)
    69  	}
    70  
    71  	// Testing merges now
    72  	err = c.Set("key", map[string]interface{}{
    73  		"b": 55,
    74  		"e": map[string]interface{}{
    75  			"e1": true,
    76  		},
    77  	})
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	m := map[string]interface{}{}
    82  	val, err = c.Get("key")
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	err = val.Scan(&m)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	expected := map[string]interface{}{
    92  		"a": "Hi",
    93  		"b": float64(55),
    94  		"c": float64(42.2),
    95  		"d": true,
    96  		"e": map[string]interface{}{
    97  			"e1": true,
    98  		},
    99  	}
   100  	if !reflect.DeepEqual(m, expected) {
   101  		t.Fatalf("Not equal: %v and %v", m, expected)
   102  	}
   103  
   104  	// Set just one value
   105  	expected = map[string]interface{}{
   106  		"a": "Hi",
   107  		"b": float64(55),
   108  		"c": float64(42.2),
   109  		"d": true,
   110  		"e": map[string]interface{}{
   111  			"e1": float64(45),
   112  		},
   113  	}
   114  	err = c.Set("key.e.e1", 45)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	m = map[string]interface{}{}
   120  	val, err = c.Get("key")
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	err = val.Scan(&m)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	if !reflect.DeepEqual(m, expected) {
   130  		t.Fatalf("Not equal: %v and %v", m, expected)
   131  	}
   132  }