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

     1  package inputvalidation_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	validation "github.com/go-ozzo/ozzo-validation/v4"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation"
     8  	"github.com/pkg/errors"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestEach(t *testing.T) {
    13  	// GIVEN
    14  	testCases := []struct {
    15  		Name          string
    16  		Value         interface{}
    17  		ExpectedError error
    18  	}{
    19  		{
    20  			Name:          "Success when nil map",
    21  			Value:         map[string]string{},
    22  			ExpectedError: nil,
    23  		},
    24  		{
    25  			Name:          "Success when map",
    26  			Value:         map[string]string{"key1": "val1", "key2": "val2"},
    27  			ExpectedError: nil,
    28  		},
    29  		{
    30  			Name:          "Success when pointer to map",
    31  			Value:         &map[string]string{"key1": "val1", "key2": "val2"},
    32  			ExpectedError: nil,
    33  		},
    34  		{
    35  			Name:          "Success when slice",
    36  			Value:         []string{"a", "b", "c"},
    37  			ExpectedError: nil,
    38  		},
    39  		{
    40  			Name:          "Success when nil slice",
    41  			Value:         []string{},
    42  			ExpectedError: nil,
    43  		},
    44  		{
    45  			Name:          "Success when pointer slice",
    46  			Value:         &[]string{"a", "b", "c"},
    47  			ExpectedError: nil,
    48  		},
    49  		{
    50  			Name:          "Returns error when map value is empty",
    51  			Value:         map[string]string{"key1": "", "key2": "val2"},
    52  			ExpectedError: errors.New("key1: cannot be blank."),
    53  		},
    54  		{
    55  			Name:          "Returns error when pointer to map with empty value",
    56  			Value:         &map[string]string{"key1": "", "key2": "val2"},
    57  			ExpectedError: errors.New("key1: cannot be blank."),
    58  		},
    59  		{
    60  			Name:          "Returns error when multiple slice values are empty",
    61  			Value:         []string{"", "test", ""},
    62  			ExpectedError: errors.New("0: cannot be blank; 2: cannot be blank."),
    63  		},
    64  		{
    65  			Name:          "Returns error when pointer to slice with empty value",
    66  			Value:         &[]string{"ok", "", "ok"},
    67  			ExpectedError: errors.New("1: cannot be blank."),
    68  		},
    69  		{
    70  			Name:          "Returns error when when nil",
    71  			Value:         nil,
    72  			ExpectedError: errors.New("must be an iterable (map, slice or array) or a pointer to iterable"),
    73  		},
    74  	}
    75  
    76  	for _, testCase := range testCases {
    77  		t.Run(testCase.Name, func(t *testing.T) {
    78  			r := inputvalidation.Each(validation.Required)
    79  			// WHEN
    80  			err := r.Validate(testCase.Value)
    81  			// THEN
    82  			if testCase.ExpectedError == nil {
    83  				require.NoError(t, err)
    84  			} else {
    85  				require.Error(t, err)
    86  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
    87  			}
    88  		})
    89  	}
    90  }