test(kv): add unit tests for the helpers functions kv.AssertKeyAtLeas… (#19965)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Marko <marko@baricevic.me>
This commit is contained in:
Emil Georgiev 2024-04-11 01:27:40 +03:00 committed by GitHub
parent 4be248eef6
commit c56152dfac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

89
types/kv/helpers_test.go Normal file
View File

@ -0,0 +1,89 @@
package kv_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/cosmos/cosmos-sdk/types/kv"
)
func TestAssertKeyAtLeastLength(t *testing.T) {
cases := []struct {
name string
key []byte
length int
expectPanic bool
}{
{
name: "Store key length is less than the given length",
key: []byte("hello"),
length: 10,
expectPanic: true,
},
{
name: "Store key length is equal to the given length",
key: []byte("store-key"),
length: 9,
expectPanic: false,
},
{
name: "Store key length is greater than the given length",
key: []byte("unique"),
length: 3,
expectPanic: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.expectPanic {
assert.Panics(t, func() {
kv.AssertKeyAtLeastLength(tc.key, tc.length)
})
return
}
kv.AssertKeyAtLeastLength(tc.key, tc.length)
})
}
}
func TestAssertKeyLength(t *testing.T) {
cases := []struct {
name string
key []byte
length int
expectPanic bool
}{
{
name: "Store key length is less than the given length",
key: []byte("hello"),
length: 10,
expectPanic: true,
},
{
name: "Store key length is equal to the given length",
key: []byte("store-key"),
length: 9,
expectPanic: false,
},
{
name: "Store key length is greater than the given length",
key: []byte("unique"),
length: 3,
expectPanic: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if tc.expectPanic {
assert.Panics(t, func() {
kv.AssertKeyLength(tc.key, tc.length)
})
return
}
kv.AssertKeyLength(tc.key, tc.length)
})
}
}