github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/utils/map_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCombineStringMaps(t *testing.T) {
    11  	testCases := []struct {
    12  		name        string
    13  		left        map[string]interface{}
    14  		right       map[string]interface{}
    15  		expected    map[string]string
    16  		expectedErr error
    17  	}{
    18  		{
    19  			name:        "combines the maps",
    20  			left:        map[string]interface{}{"foo": "bar"},
    21  			right:       map[string]interface{}{"a": "b"},
    22  			expected:    map[string]string{"a": "b", "foo": "bar"},
    23  			expectedErr: nil,
    24  		},
    25  		{
    26  			name:        "fails if keys are the same but value isn't",
    27  			left:        map[string]interface{}{"foo": "bar", "a": "fail"},
    28  			right:       map[string]interface{}{"a": "b", "c": "d"},
    29  			expected:    map[string]string{"a": "b", "foo": "bar"},
    30  			expectedErr: fmt.Errorf("found duplicate key a with different value, a: fail ,b: b"),
    31  		},
    32  		{
    33  			name:        "pass if keys & values are the same",
    34  			left:        map[string]interface{}{"foo": "bar", "a": "b"},
    35  			right:       map[string]interface{}{"a": "b", "c": "d"},
    36  			expected:    map[string]string{"a": "b", "c": "d", "foo": "bar"},
    37  			expectedErr: nil,
    38  		},
    39  	}
    40  
    41  	for _, testCase := range testCases {
    42  		testCaseCopy := testCase
    43  
    44  		t.Run(testCaseCopy.name, func(t *testing.T) {
    45  			t.Parallel()
    46  
    47  			got, err := CombineStringMaps(testCaseCopy.left, testCaseCopy.right)
    48  
    49  			if testCaseCopy.expectedErr != nil {
    50  				assert.EqualError(t, err, testCaseCopy.expectedErr.Error())
    51  			} else {
    52  				assert.NoError(t, err)
    53  				assert.Equal(t, testCaseCopy.expected, got)
    54  			}
    55  
    56  		})
    57  	}
    58  }