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

     1  package graphql_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/inputvalidation/inputvalidationtest"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestLabelInput_Validate_Key(t *testing.T) {
    12  	testCases := []struct {
    13  		Name          string
    14  		Value         string
    15  		ExpectedValid bool
    16  	}{
    17  		{
    18  			Name:          "ExpectedValid",
    19  			Value:         "valid",
    20  			ExpectedValid: true,
    21  		},
    22  		{
    23  			Name:          "Invalid - Empty",
    24  			Value:         inputvalidationtest.EmptyString,
    25  			ExpectedValid: false,
    26  		},
    27  		{
    28  			Name:          "Invalid - Too long",
    29  			Value:         inputvalidationtest.String257Long,
    30  			ExpectedValid: false,
    31  		},
    32  		{
    33  			Name:          "Invalid - Unsupported characters",
    34  			Value:         "not/valid",
    35  			ExpectedValid: false,
    36  		},
    37  	}
    38  
    39  	for _, testCase := range testCases {
    40  		t.Run(testCase.Name, func(t *testing.T) {
    41  			//GIVEN
    42  			sut := fixValidLabelInput()
    43  			sut.Key = testCase.Value
    44  			// WHEN
    45  			err := sut.Validate()
    46  			// THEN
    47  			if testCase.ExpectedValid {
    48  				require.NoError(t, err)
    49  			} else {
    50  				require.Error(t, err)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestLabelInput_Validate_Value(t *testing.T) {
    57  	testCases := []struct {
    58  		Name          string
    59  		Value         interface{}
    60  		ExpectedValid bool
    61  	}{
    62  		{
    63  			Name:          "ExpectedValid",
    64  			Value:         "valid",
    65  			ExpectedValid: true,
    66  		},
    67  		{
    68  			Name:          "ExpectedValid - Map with strings",
    69  			Value:         map[string]string{"a": "b"},
    70  			ExpectedValid: true,
    71  		},
    72  		{
    73  			Name:          "ExpectedValid - Slice of ints",
    74  			Value:         []int{1, 2, 3},
    75  			ExpectedValid: true,
    76  		},
    77  		{
    78  			Name:          "Invalid - Nil",
    79  			Value:         nil,
    80  			ExpectedValid: false,
    81  		},
    82  		{
    83  			Name:          "Invalid - Empty string",
    84  			Value:         inputvalidationtest.EmptyString,
    85  			ExpectedValid: false,
    86  		},
    87  	}
    88  
    89  	for _, testCase := range testCases {
    90  		t.Run(testCase.Name, func(t *testing.T) {
    91  			//GIVEN
    92  			sut := fixValidLabelInput()
    93  			sut.Value = testCase.Value
    94  			// WHEN
    95  			err := sut.Validate()
    96  			// THEN
    97  			if testCase.ExpectedValid {
    98  				require.NoError(t, err)
    99  			} else {
   100  				require.Error(t, err)
   101  			}
   102  		})
   103  	}
   104  }
   105  
   106  func fixValidLabelInput() graphql.LabelInput {
   107  	return graphql.LabelInput{
   108  		Key:   "valid",
   109  		Value: "valid",
   110  	}
   111  }