github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/graphqlizer/graphqlizer_test.go (about)

     1  package graphqlizer_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/graphql/graphqlizer"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestGraphqlizer_LabelsToGQL(t *testing.T) {
    14  	// GIVEN
    15  	g := graphqlizer.Graphqlizer{}
    16  
    17  	testCases := []struct {
    18  		Name          string
    19  		Input         graphql.Labels
    20  		Expected      string
    21  		ExpectedError error
    22  	}{
    23  		{
    24  			Name: "Success when slice of strings",
    25  			Input: graphql.Labels{
    26  				"foo": []string{"test", "best", "asdf"},
    27  			},
    28  			Expected:      "{foo:[\"test\",\"best\",\"asdf\"],}",
    29  			ExpectedError: nil,
    30  		},
    31  		{
    32  			Name: "Success when string",
    33  			Input: graphql.Labels{
    34  				"bar": "test",
    35  			},
    36  			Expected:      "{bar:\"test\",}",
    37  			ExpectedError: nil,
    38  		},
    39  		{
    40  			Name: "Success when bool",
    41  			Input: graphql.Labels{
    42  				"baz": true,
    43  			},
    44  			Expected:      "{baz:true,}",
    45  			ExpectedError: nil,
    46  		},
    47  		{
    48  			Name: "Success when map of strings",
    49  			Input: graphql.Labels{
    50  				"biz": map[string]string{"test": "a", "best": "b", "asdf": "c"},
    51  			},
    52  			Expected:      "{biz:{asdf:\"c\",best:\"b\",test:\"a\",},}",
    53  			ExpectedError: nil,
    54  		},
    55  		{
    56  			Name: "Success when number",
    57  			Input: graphql.Labels{
    58  				"buz": 10,
    59  			},
    60  			Expected:      "{buz:10,}",
    61  			ExpectedError: nil,
    62  		},
    63  		{
    64  			Name: "Success when mixed",
    65  			Input: graphql.Labels{
    66  				"foo": []string{"test", "best", "asdf"},
    67  				"bar": "test",
    68  				"baz": true,
    69  				"biz": map[string]string{"test": "a", "best": "b", "asdf": "c"},
    70  				"buz": 10,
    71  			},
    72  			Expected:      "{bar:\"test\",baz:true,biz:{asdf:\"c\",best:\"b\",test:\"a\",},buz:10,foo:[\"test\",\"best\",\"asdf\"],}",
    73  			ExpectedError: nil,
    74  		},
    75  		{
    76  			Name: "Success when nested iterables",
    77  			Input: graphql.Labels{
    78  				"foo": []interface{}{"test", map[string]string{"asdf": "fdsa", "fdsa": "asdf"}, []string{"aaaa", "bbbb"}},
    79  			},
    80  			Expected:      "{foo:[\"test\",{asdf:\"fdsa\",fdsa:\"asdf\",},[\"aaaa\",\"bbbb\"]],}",
    81  			ExpectedError: nil,
    82  		},
    83  	}
    84  
    85  	for _, testCase := range testCases {
    86  		t.Run(testCase.Name, func(t *testing.T) {
    87  			// WHEN
    88  			result, err := g.LabelsToGQL(testCase.Input)
    89  			// THEN
    90  			if testCase.ExpectedError != nil {
    91  				require.EqualError(t, err, testCase.ExpectedError.Error())
    92  			} else {
    93  				require.NoError(t, err)
    94  			}
    95  			assert.Equal(t, testCase.Expected, result)
    96  		})
    97  	}
    98  }