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

     1  package label
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_ExtractValueFromJSONPath_WithValidInput(t *testing.T) {
    12  	testCases := []struct {
    13  		Name     string
    14  		Input    string
    15  		Expected []interface{}
    16  	}{
    17  		{
    18  			Name:     "Single word",
    19  			Input:    `$[*] ? (@ == "dd")`,
    20  			Expected: []interface{}{"dd"},
    21  		}, {
    22  			Name:     "Single with space",
    23  			Input:    `$[*] ? (@ == "aa cc")`,
    24  			Expected: []interface{}{"aa cc"},
    25  		}, {
    26  			Name:     "Many words",
    27  			Input:    `$[*] ? (@ == "aacc" || @ == "bbee")`,
    28  			Expected: []interface{}{"aacc", "bbee"},
    29  		},
    30  	}
    31  
    32  	for _, testCase := range testCases {
    33  		t.Run(testCase.Name, func(t *testing.T) {
    34  			extractedVal, err := ExtractValueFromJSONPath(testCase.Input)
    35  			require.NotNil(t, extractedVal)
    36  			assert.Equal(t, testCase.Expected, extractedVal)
    37  			assert.NoError(t, err)
    38  		})
    39  	}
    40  }
    41  
    42  func Test_ExtractValueFromJSONPath_WithInvalidInput(t *testing.T) {
    43  	testCases := []struct {
    44  		Name  string
    45  		Input string
    46  	}{
    47  		{
    48  			Name:  "Empty input",
    49  			Input: ``,
    50  		}, {
    51  			Name:  "Invalid string",
    52  			Input: `some invalid stirng`,
    53  		},
    54  	}
    55  
    56  	for _, testCase := range testCases {
    57  		t.Run(testCase.Name, func(t *testing.T) {
    58  			extractedVal, err := ExtractValueFromJSONPath(testCase.Input)
    59  
    60  			assert.Nil(t, extractedVal)
    61  			assert.Error(t, err)
    62  		})
    63  	}
    64  }