Skip to content

Commit

Permalink
Add unit test for data.GetValue
Browse files Browse the repository at this point in the history
  • Loading branch information
cmurphy committed Sep 18, 2023
1 parent 35cb42e commit f6f8c00
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pkg/data/values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package data

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetValue(t *testing.T) {
tests := []struct {
name string
data map[string]interface{}
keys []string
wantValue interface{}
wantSuccess bool
}{
{
name: "nil map",
data: nil,
keys: []string{"somekey"},
wantValue: nil,
wantSuccess: false,
},
{
name: "key is not in map",
data: map[string]interface{}{
"realKey": "realVal",
},
keys: []string{"badKey"},
wantValue: nil,
wantSuccess: false,
},
{
name: "key is in first level of map",
data: map[string]interface{}{
"realKey": "realVal",
},
keys: []string{"realKey"},
wantValue: "realVal",
wantSuccess: true,
},
{
name: "key is nested in map",
data: map[string]interface{}{
"parent": map[string]interface{}{
"child": map[string]interface{}{
"grandchild": "someValue",
},
},
},
keys: []string{"parent", "child", "grandchild"},
wantValue: "someValue",
wantSuccess: true,
},
{
name: "get index of slice",
data: map[string]interface{}{
"parent": map[string]interface{}{
"children": []string{
"alice",
"bob",
"eve",
},
},
},
keys: []string{"parent", "children", "2"},
wantValue: nil,
wantSuccess: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotValue, gotSuccess := GetValue(test.data, test.keys...)
assert.Equal(t, test.wantValue, gotValue)
assert.Equal(t, test.wantSuccess, gotSuccess)
})
}
}

0 comments on commit f6f8c00

Please sign in to comment.