github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/label/valueconverter_test.go (about)

     1  package label
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_ValueToStringsSlice(t *testing.T) {
    13  	testCases := []struct {
    14  		Name     string
    15  		Input    interface{}
    16  		Expected []string
    17  		Error    error
    18  	}{
    19  		{
    20  			Name:     "Single value",
    21  			Input:    []interface{}{`abc`},
    22  			Expected: []string{"abc"},
    23  		}, {
    24  			Name:     "Multiple values",
    25  			Input:    []interface{}{`abc`, `cde`},
    26  			Expected: []string{"abc", "cde"},
    27  		}, {
    28  			Name:  "Error when unable to convert to slice of interfaces",
    29  			Input: "some text",
    30  			Error: errors.New("Internal Server Error: cannot convert label value to slice of strings"),
    31  		}, {
    32  			Name:  "Error when unable to convert element to string",
    33  			Input: []interface{}{byte(1)},
    34  			Error: errors.New("Internal Server Error: cannot cast label value as a string"),
    35  		},
    36  	}
    37  
    38  	for _, testCase := range testCases {
    39  		t.Run(testCase.Name, func(t *testing.T) {
    40  			sliceOfStrings, err := ValueToStringsSlice(testCase.Input)
    41  
    42  			if testCase.Error == nil {
    43  				require.NotNil(t, sliceOfStrings)
    44  				require.Equal(t, testCase.Expected, sliceOfStrings)
    45  			} else {
    46  				assert.EqualError(t, err, testCase.Error.Error())
    47  			}
    48  		})
    49  	}
    50  }