trpc.group/trpc-go/trpc-go@v1.0.2/config/trpc_config_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package config
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func Test_search(t *testing.T) {
    26  	type args struct {
    27  		unmarshalledData map[string]interface{}
    28  		keys             []string
    29  	}
    30  	tests := []struct {
    31  		name    string
    32  		args    args
    33  		want    interface{}
    34  		wantErr assert.ErrorAssertionFunc
    35  	}{
    36  		{
    37  			name: "empty keys",
    38  			args: args{
    39  				keys: nil,
    40  			},
    41  			want: nil,
    42  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
    43  				if !errors.Is(err, ErrConfigNotExist) {
    44  					t.Errorf("received unexpected error got: %+v, want: +%v", err, ErrCodecNotExist)
    45  					return false
    46  				}
    47  				return true
    48  			},
    49  		},
    50  		{
    51  			name: "key doesn't match",
    52  			args: args{
    53  				unmarshalledData: map[string]interface{}{
    54  					"1": []string{"x", "y"},
    55  				},
    56  				keys: []string{"not-1"},
    57  			},
    58  			want: nil,
    59  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
    60  				if !errors.Is(err, ErrConfigNotExist) {
    61  					t.Errorf("received unexpected error got: %+v, want: +%v", err, ErrCodecNotExist)
    62  					return false
    63  				}
    64  				return true
    65  			},
    66  		},
    67  		{
    68  			name: "value of unmarshalledData isn't map type",
    69  			args: args{
    70  				unmarshalledData: map[string]interface{}{
    71  					"1": []string{"x", "y"},
    72  				},
    73  				keys: []string{"1", "2"},
    74  			},
    75  			want: nil,
    76  			wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
    77  				if !errors.Is(err, ErrConfigNotExist) {
    78  					t.Errorf("received unexpected error got: %+v, want: +%v", err, ErrCodecNotExist)
    79  					return false
    80  				}
    81  				return true
    82  			},
    83  		},
    84  		{
    85  			name: "value of unmarshalledData is map[interface{}]interface{} type",
    86  			args: args{
    87  				unmarshalledData: map[string]interface{}{
    88  					"1": map[interface{}]interface{}{"x": "y"},
    89  				},
    90  				keys: []string{"1", "x"},
    91  			},
    92  			want:    "y",
    93  			wantErr: assert.NoError,
    94  		},
    95  		{
    96  			name: "value of unmarshalledData is map[string]interface{} type",
    97  			args: args{
    98  				unmarshalledData: map[string]interface{}{
    99  					"1": map[string]interface{}{"x": "y"},
   100  				},
   101  				keys: []string{"1", "x"},
   102  			},
   103  			want:    "y",
   104  			wantErr: assert.NoError,
   105  		},
   106  	}
   107  	for _, tt := range tests {
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			got, err := search(tt.args.unmarshalledData, tt.args.keys)
   110  			if !tt.wantErr(t, err, fmt.Sprintf("search(%v, %v)", tt.args.unmarshalledData, tt.args.keys)) {
   111  				return
   112  			}
   113  			assert.Equalf(t, tt.want, got, "search(%v, %v)", tt.args.unmarshalledData, tt.args.keys)
   114  		})
   115  	}
   116  }
   117  
   118  func TestYamlCodec_Unmarshal(t *testing.T) {
   119  	t.Run("interface", func(t *testing.T) {
   120  		var tt interface{}
   121  		tt = map[string]interface{}{}
   122  		require.Nil(t, GetCodec("yaml").Unmarshal([]byte("[1, 2]"), &tt))
   123  	})
   124  	t.Run("map[string]interface{}", func(t *testing.T) {
   125  		tt := map[string]interface{}{}
   126  		require.NotNil(t, GetCodec("yaml").Unmarshal([]byte("[1, 2]"), &tt))
   127  	})
   128  }