github.com/prebid/prebid-server/v2@v2.18.0/util/maputil/maputil_test.go (about)

     1  package maputil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestReadEmbeddedMap(t *testing.T) {
    10  	testCases := []struct {
    11  		description string
    12  		value       map[string]interface{}
    13  		key         string
    14  		expectedMap map[string]interface{}
    15  		expectedOK  bool
    16  	}{
    17  		{
    18  			description: "Nil",
    19  			value:       nil,
    20  			key:         "",
    21  			expectedMap: nil,
    22  			expectedOK:  false,
    23  		},
    24  		{
    25  			description: "Empty",
    26  			value:       map[string]interface{}{},
    27  			key:         "foo",
    28  			expectedMap: nil,
    29  			expectedOK:  false,
    30  		},
    31  		{
    32  			description: "Success",
    33  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": 42}},
    34  			key:         "foo",
    35  			expectedMap: map[string]interface{}{"bar": 42},
    36  			expectedOK:  true,
    37  		},
    38  		{
    39  			description: "Not Found",
    40  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": 42}},
    41  			key:         "notFound",
    42  			expectedMap: nil,
    43  			expectedOK:  false,
    44  		},
    45  		{
    46  			description: "Wrong Type",
    47  			value:       map[string]interface{}{"foo": 42},
    48  			key:         "foo",
    49  			expectedMap: nil,
    50  			expectedOK:  false,
    51  		},
    52  	}
    53  
    54  	for _, test := range testCases {
    55  		resultMap, resultOK := ReadEmbeddedMap(test.value, test.key)
    56  
    57  		assert.Equal(t, test.expectedMap, resultMap, test.description+":map")
    58  		assert.Equal(t, test.expectedOK, resultOK, test.description+":ok")
    59  	}
    60  }
    61  
    62  func TestReadEmbeddedSlice(t *testing.T) {
    63  	testCases := []struct {
    64  		description   string
    65  		value         map[string]interface{}
    66  		key           string
    67  		expectedSlice []interface{}
    68  		expectedOK    bool
    69  	}{
    70  		{
    71  			description:   "Nil",
    72  			value:         nil,
    73  			key:           "",
    74  			expectedSlice: nil,
    75  			expectedOK:    false,
    76  		},
    77  		{
    78  			description:   "Empty",
    79  			value:         map[string]interface{}{},
    80  			key:           "foo",
    81  			expectedSlice: nil,
    82  			expectedOK:    false,
    83  		},
    84  		{
    85  			description:   "Success",
    86  			value:         map[string]interface{}{"foo": []interface{}{42}},
    87  			key:           "foo",
    88  			expectedSlice: []interface{}{42},
    89  			expectedOK:    true,
    90  		},
    91  		{
    92  			description:   "Not Found",
    93  			value:         map[string]interface{}{"foo": []interface{}{42}},
    94  			key:           "notFound",
    95  			expectedSlice: nil,
    96  			expectedOK:    false,
    97  		},
    98  		{
    99  			description:   "Wrong Type",
   100  			value:         map[string]interface{}{"foo": 42},
   101  			key:           "foo",
   102  			expectedSlice: nil,
   103  			expectedOK:    false,
   104  		},
   105  	}
   106  
   107  	for _, test := range testCases {
   108  		resultSlice, resultOK := ReadEmbeddedSlice(test.value, test.key)
   109  
   110  		assert.Equal(t, test.expectedSlice, resultSlice, test.description+":slicd")
   111  		assert.Equal(t, test.expectedOK, resultOK, test.description+":ok")
   112  	}
   113  }
   114  
   115  func TestReadEmbeddedString(t *testing.T) {
   116  	testCases := []struct {
   117  		description    string
   118  		value          map[string]interface{}
   119  		key            string
   120  		expectedString string
   121  		expectedOK     bool
   122  	}{
   123  		{
   124  			description:    "Nil",
   125  			value:          nil,
   126  			key:            "",
   127  			expectedString: "",
   128  			expectedOK:     false,
   129  		},
   130  		{
   131  			description:    "Empty",
   132  			value:          map[string]interface{}{},
   133  			key:            "foo",
   134  			expectedString: "",
   135  			expectedOK:     false,
   136  		},
   137  		{
   138  			description:    "Success",
   139  			value:          map[string]interface{}{"foo": "stringValue"},
   140  			key:            "foo",
   141  			expectedString: "stringValue",
   142  			expectedOK:     true,
   143  		},
   144  		{
   145  			description:    "Not Found",
   146  			value:          map[string]interface{}{"foo": "stringValue"},
   147  			key:            "notFound",
   148  			expectedString: "",
   149  			expectedOK:     false,
   150  		},
   151  		{
   152  			description:    "Wrong Type",
   153  			value:          map[string]interface{}{"foo": []interface{}{42}},
   154  			key:            "foo",
   155  			expectedString: "",
   156  			expectedOK:     false,
   157  		},
   158  	}
   159  
   160  	for _, test := range testCases {
   161  		resultString, resultOK := ReadEmbeddedString(test.value, test.key)
   162  
   163  		assert.Equal(t, test.expectedString, resultString, test.description+":string")
   164  		assert.Equal(t, test.expectedOK, resultOK, test.description+":ok")
   165  	}
   166  }
   167  
   168  func TestHasElement(t *testing.T) {
   169  	testCases := []struct {
   170  		description string
   171  		value       map[string]interface{}
   172  		keys        []string
   173  		expected    bool
   174  	}{
   175  		{
   176  			description: "Level 1 - Exists",
   177  			value:       map[string]interface{}{"foo": "exists"},
   178  			keys:        []string{"foo"},
   179  			expected:    true,
   180  		},
   181  		{
   182  			description: "Level 1 - Does Not Exist",
   183  			value:       map[string]interface{}{"foo": "exists"},
   184  			keys:        []string{"doesnotexist"},
   185  			expected:    false,
   186  		},
   187  		{
   188  			description: "Level 2 - Exists",
   189  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": "exists"}},
   190  			keys:        []string{"foo", "bar"},
   191  			expected:    true,
   192  		},
   193  		{
   194  			description: "Level 2 - Top Level Does Not Exist",
   195  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": "exists"}},
   196  			keys:        []string{"doesnotexist", "bar"},
   197  			expected:    false,
   198  		},
   199  		{
   200  			description: "Level 2 - Lower Level Does Not Exist",
   201  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": "exists"}},
   202  			keys:        []string{"foo", "doesnotexist"},
   203  			expected:    false,
   204  		},
   205  		{
   206  			description: "Level 2 - Does Not Exist At All",
   207  			value:       map[string]interface{}{"foo": map[string]interface{}{"bar": "exists"}},
   208  			keys:        []string{"doesnotexist", "doesnotexist"},
   209  			expected:    false,
   210  		},
   211  		{
   212  			description: "Keys Nil",
   213  			value:       map[string]interface{}{"foo": "exists"},
   214  			keys:        nil,
   215  			expected:    false,
   216  		},
   217  		{
   218  			description: "Keys Empty",
   219  			value:       map[string]interface{}{"foo": "exists"},
   220  			keys:        []string{},
   221  			expected:    false,
   222  		},
   223  		{
   224  			description: "Map Nil",
   225  			value:       nil,
   226  			keys:        []string{"foo"},
   227  			expected:    false,
   228  		},
   229  		{
   230  			description: "Map Empty",
   231  			value:       map[string]interface{}{},
   232  			keys:        []string{"foo"},
   233  			expected:    false,
   234  		},
   235  		{
   236  			description: "Nil",
   237  			value:       nil,
   238  			keys:        nil,
   239  			expected:    false,
   240  		},
   241  	}
   242  
   243  	for _, test := range testCases {
   244  		result := HasElement(test.value, test.keys...)
   245  		assert.Equal(t, test.expected, result, test.description)
   246  	}
   247  }
   248  
   249  func TestCloneMap(t *testing.T) {
   250  	// Test we handle nils properly
   251  	t.Run("NilMap", func(t *testing.T) {
   252  		var testMap, copyMap map[string]string = nil, nil // copyMap is a manual copy of testMap
   253  		clone := Clone(testMap)
   254  		testMap = map[string]string{"foo": "bar"}
   255  		assert.Equal(t, copyMap, clone)
   256  	})
   257  	// Test a simple string map
   258  	t.Run("StringMap", func(t *testing.T) {
   259  		var testMap, copyMap map[string]string = map[string]string{"foo": "bar", "first": "one"}, map[string]string{"foo": "bar", "first": "one"}
   260  		clone := Clone(testMap)
   261  		testMap["foo"] = "baz"
   262  		testMap["bozo"] = "the clown"
   263  		assert.Equal(t, copyMap, clone)
   264  	})
   265  	// Test a simple map[string]int
   266  	t.Run("StringInt", func(t *testing.T) {
   267  		var testMap, copyMap map[string]int = map[string]int{"foo": 1, "first": 2}, map[string]int{"foo": 1, "first": 2}
   268  		clone := Clone(testMap)
   269  		testMap["foo"] = 7
   270  		testMap["bozo"] = 13
   271  		assert.Equal(t, copyMap, clone)
   272  	})
   273  }