github.com/prebid/prebid-server/v2@v2.18.0/adservertargeting/utils_test.go (about)

     1  package adservertargeting
     2  
     3  import (
     4  	"github.com/buger/jsonparser"
     5  	"github.com/stretchr/testify/assert"
     6  	"testing"
     7  )
     8  
     9  func TestSplitAndGet(t *testing.T) {
    10  
    11  	testCases := []struct {
    12  		description   string
    13  		inputPath     string
    14  		inputData     []byte
    15  		inputDelim    string
    16  		expectedValue string
    17  		expectedError bool
    18  	}{
    19  		{
    20  			description:   "get value from valid input",
    21  			inputData:     []byte(`{"site": {"page": "test.com"}}`),
    22  			inputPath:     "site.page",
    23  			inputDelim:    ".",
    24  			expectedValue: "test.com",
    25  			expectedError: false,
    26  		},
    27  		{
    28  			description:   "get value from invalid input",
    29  			inputData:     nil,
    30  			inputPath:     "site.page",
    31  			inputDelim:    ".",
    32  			expectedValue: "",
    33  			expectedError: true,
    34  		},
    35  	}
    36  
    37  	for _, test := range testCases {
    38  		res, err := splitAndGet(test.inputPath, test.inputData, test.inputDelim)
    39  
    40  		assert.Equal(t, test.expectedValue, res, "incorrect result")
    41  
    42  		if test.expectedError {
    43  			assert.Error(t, err, "expected error not returned")
    44  		} else {
    45  			assert.NoError(t, err, "unexpected error returned")
    46  		}
    47  	}
    48  }
    49  
    50  func TestVerifyPrefixAndTrimFound(t *testing.T) {
    51  
    52  	testCases := []struct {
    53  		description   string
    54  		inputPath     string
    55  		inputPrefix   string
    56  		expectedValue string
    57  		valueFound    bool
    58  	}{
    59  		{
    60  			description:   "get existing value ",
    61  			inputPath:     "site.page.id",
    62  			inputPrefix:   "site.page.",
    63  			expectedValue: "id",
    64  			valueFound:    true,
    65  		},
    66  		{
    67  			description:   "get non-existing value",
    68  			inputPath:     "site.page.id",
    69  			inputPrefix:   "incorrect",
    70  			expectedValue: "",
    71  			valueFound:    false,
    72  		},
    73  	}
    74  
    75  	for _, test := range testCases {
    76  		res, found := verifyPrefixAndTrim(test.inputPath, test.inputPrefix)
    77  		assert.Equal(t, test.valueFound, found, "found value incorrect")
    78  		assert.Equal(t, test.expectedValue, res, "incorrect returned result")
    79  	}
    80  }
    81  
    82  func TestTypedLookup(t *testing.T) {
    83  
    84  	testCases := []struct {
    85  		description   string
    86  		inputData     []byte
    87  		inputPath     string
    88  		inputKeys     []string
    89  		expectedValue []byte
    90  		expectedError bool
    91  	}{
    92  		{
    93  			description:   "lookup existing value",
    94  			inputData:     []byte(`{"site": {"page": "test.com"}}`),
    95  			inputPath:     "site.page",
    96  			inputKeys:     []string{"site", "page"},
    97  			expectedValue: []byte(`test.com`),
    98  			expectedError: false,
    99  		},
   100  		{
   101  			description:   "lookup non-existing value",
   102  			inputData:     []byte(`{"site": {"page": "test.com"}}`),
   103  			inputPath:     "site.page",
   104  			inputKeys:     []string{"site", "id"},
   105  			expectedValue: []byte(nil),
   106  			expectedError: true,
   107  		},
   108  		{
   109  			description:   "lookup value in incorrect json",
   110  			inputData:     []byte(`[`),
   111  			inputPath:     "site.page",
   112  			inputKeys:     []string{"site", "page"},
   113  			expectedValue: []byte(nil),
   114  			expectedError: true,
   115  		},
   116  		{
   117  			description:   "lookup object value",
   118  			inputData:     []byte(`{"site": {"page": "test.com"}}`),
   119  			inputPath:     "site",
   120  			inputKeys:     []string{"site"},
   121  			expectedValue: []byte(nil),
   122  			expectedError: true,
   123  		},
   124  	}
   125  
   126  	for _, test := range testCases {
   127  
   128  		res, err := typedLookup(test.inputData, test.inputPath, test.inputKeys...)
   129  
   130  		assert.Equal(t, test.expectedValue, res, "incorrect returned result")
   131  
   132  		if test.expectedError {
   133  			assert.Error(t, err, "expected error not returned")
   134  		} else {
   135  			assert.NoError(t, err, "unexpected error returned")
   136  		}
   137  	}
   138  }
   139  
   140  func TestVerifyType(t *testing.T) {
   141  
   142  	testCases := []struct {
   143  		description string
   144  		inputValue  jsonparser.ValueType
   145  		expectedRes bool
   146  	}{
   147  		{
   148  			description: "verify correct value",
   149  			inputValue:  jsonparser.String,
   150  			expectedRes: true,
   151  		},
   152  		{
   153  			description: "verify correct value",
   154  			inputValue:  jsonparser.Object,
   155  			expectedRes: false,
   156  		},
   157  	}
   158  
   159  	for _, test := range testCases {
   160  		correctType := verifyType(test.inputValue)
   161  		assert.Equal(t, test.expectedRes, correctType, "incorrect verified type result")
   162  
   163  	}
   164  }