github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/labels_test.go (about) 1 package graphql_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 8 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestLabels_UnmarshalGQL(t *testing.T) { 15 for name, tc := range map[string]struct { 16 input interface{} 17 err bool 18 errMsg string 19 expected graphql.Labels 20 }{ 21 //given 22 "correct input map[string]string": { 23 input: map[string]interface{}{"annotation": "val1"}, 24 err: false, 25 expected: graphql.Labels{"annotation": "val1"}, 26 }, 27 "correct input map[string]int": { 28 input: map[string]interface{}{"annotation": 123}, 29 err: false, 30 expected: graphql.Labels{"annotation": 123}, 31 }, 32 "correct input map[string][]string": { 33 input: map[string]interface{}{"annotation": []string{"val1", "val2"}}, 34 err: false, 35 expected: graphql.Labels{"annotation": []string{"val1", "val2"}}, 36 }, 37 "error: input is nil": { 38 input: nil, 39 err: true, 40 errMsg: apperrors.NewInternalError("input should not be nil").Error()}, 41 "error: invalid input type": { 42 input: map[int]interface{}{123: "invalid map"}, 43 err: true, 44 errMsg: "unexpected Labels type: map[int]interface {}, should be map[string]interface{}"}, 45 } { 46 t.Run(name, func(t *testing.T) { 47 // WHEN 48 a := graphql.Labels{} 49 err := a.UnmarshalGQL(tc.input) 50 51 // THEN 52 if tc.err { 53 assert.Error(t, err) 54 assert.EqualError(t, err, tc.errMsg) 55 assert.Empty(t, a) 56 } else { 57 assert.NoError(t, err) 58 assert.Equal(t, tc.expected, a) 59 } 60 }) 61 } 62 } 63 64 func TestLabels_MarshalGQL(t *testing.T) { 65 as := assert.New(t) 66 67 var tests = []struct { 68 input graphql.Labels 69 expected string 70 }{ 71 //given 72 {graphql.Labels{"annotation": 123}, `{"annotation":123}`}, 73 {graphql.Labels{"annotation": []string{"val1", "val2"}}, `{"annotation":["val1","val2"]}`}, 74 } 75 76 for _, test := range tests { 77 // WHEN 78 buf := bytes.Buffer{} 79 test.input.MarshalGQL(&buf) 80 81 // THEN 82 as.NotNil(buf) 83 as.Equal(test.expected, buf.String()) 84 } 85 }