knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmap/lookup_test.go (about)

     1  /*
     2  Copyright 2021 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kmap
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestKeyPriority(t *testing.T) {
    26  	tests := []struct {
    27  		name     string
    28  		keys     KeyPriority
    29  		in       map[string]string
    30  		outKey   string
    31  		outValue string
    32  		outOk    bool
    33  	}{{
    34  		name:     "single key in map",
    35  		keys:     KeyPriority{"old", "new"},
    36  		in:       map[string]string{"old": "1"},
    37  		outKey:   "old",
    38  		outValue: "1",
    39  		outOk:    true,
    40  	}, {
    41  		name:     "another single key in map",
    42  		keys:     KeyPriority{"old", "new"},
    43  		in:       map[string]string{"new": "1"},
    44  		outKey:   "new",
    45  		outValue: "1",
    46  		outOk:    true,
    47  	}, {
    48  		name:     "lower ordinal takes priority",
    49  		keys:     KeyPriority{"old", "new"},
    50  		in:       map[string]string{"new": "1", "old": "2"},
    51  		outKey:   "old",
    52  		outValue: "2",
    53  		outOk:    true,
    54  	}, {
    55  		name: "missing key in map",
    56  		keys: []string{"old", "new"},
    57  		in:   map[string]string{},
    58  
    59  		// We still return what key we used to access the values
    60  		// and we first key since it has priority
    61  		outKey: "old",
    62  		outOk:  false,
    63  	}}
    64  
    65  	for _, tc := range tests {
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			k, v, ok := tc.keys.Get(tc.in)
    68  
    69  			if tc.outOk != ok {
    70  				t.Error("expected ok to be", tc.outOk)
    71  			}
    72  
    73  			if tc.outValue != v {
    74  				t.Errorf("expected value to be %q got: %q", tc.outValue, v)
    75  			}
    76  
    77  			if tc.outKey != k {
    78  				t.Errorf("expected key to be %q got: %q", tc.outKey, k)
    79  			}
    80  
    81  			if want, got := v, tc.keys.Value(tc.in); got != want {
    82  				t.Errorf("Value() diff %q != %q", want, got)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestUpdateKeys(t *testing.T) {
    89  	tests := []struct {
    90  		name string
    91  		in   map[string]string
    92  		out  map[string]string
    93  		ols  []KeyPriority
    94  	}{{
    95  		name: "identity",
    96  		in:   map[string]string{"1": "2"},
    97  		out:  map[string]string{"1": "2"},
    98  	}, {
    99  		name: "single replacement",
   100  		in:   map[string]string{"1": "2", "4": "5"},
   101  		ols: []KeyPriority{
   102  			{"3", "1"},
   103  		},
   104  		out: map[string]string{"3": "2", "4": "5"},
   105  	}, {
   106  		name: "multiple replacement",
   107  		in:   map[string]string{"1": "2", "6": "5", "8": "9"},
   108  		ols: []KeyPriority{
   109  			{"3", "1"},
   110  			{"4", "6"},
   111  		},
   112  		out: map[string]string{"3": "2", "4": "5", "8": "9"},
   113  	}, {
   114  		name: "default not replaced",
   115  		in:   map[string]string{"1": "2", "4": "5"},
   116  		ols: []KeyPriority{
   117  			{"1", "3"},
   118  		},
   119  		out: map[string]string{"1": "2", "4": "5"},
   120  	}}
   121  
   122  	for _, tc := range tests {
   123  		t.Run(tc.name, func(t *testing.T) {
   124  			out := UpdateKeys(tc.in, tc.ols...)
   125  			if diff := cmp.Diff(tc.out, out); diff != "" {
   126  				t.Error("Migrate diff (-want,+got):", diff)
   127  			}
   128  		})
   129  	}
   130  }