Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: ItalyPaleAle <[email protected]>
  • Loading branch information
ItalyPaleAle committed Mar 6, 2024
1 parent 0722db7 commit 4336514
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions metadata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func GetMetadataPropertyWithMatchedKey(props map[string]string, keys ...string)
lcProps[strings.ToLower(k)] = v
}
for _, k := range keys {
if v, found := lcProps[strings.ToLower(k)]; found {
return k, v, true
val, ok = lcProps[strings.ToLower(k)]
if ok {
return k, val, true
}
}
return "", "", false
Expand Down
44 changes: 44 additions & 0 deletions metadata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,47 @@ func TestResolveAliases(t *testing.T) {
})
}
}

func TestGetMetadataPropertyWithMatchedKey(t *testing.T) {
props := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"emptyKey": "",
}

t.Run("Existing key", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "key1", "key2")
assert.True(t, ok)
assert.Equal(t, "key1", key)
assert.Equal(t, "value1", val)
})

t.Run("Case-insensitive matching", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "KEY1")
assert.True(t, ok)
assert.Equal(t, "KEY1", key)
assert.Equal(t, "value1", val)
})

t.Run("Non-existing key", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "key4")
assert.False(t, ok)
assert.Equal(t, "", key)
assert.Equal(t, "", val)
})

t.Run("Empty properties", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(nil, "key1")
assert.False(t, ok)
assert.Equal(t, "", key)
assert.Equal(t, "", val)
})

t.Run("Value is empty", func(t *testing.T) {
key, val, ok := GetMetadataPropertyWithMatchedKey(props, "EmptyKey")
assert.True(t, ok)
assert.Equal(t, "EmptyKey", key)
assert.Equal(t, "", val)
})
}

0 comments on commit 4336514

Please sign in to comment.