Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Apr 10, 2024
92 changes: 92 additions & 0 deletions types/kv/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package kv_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/stretchr/testify/assert"
)

func TestAssertKeyAtLeastLength(t *testing.T) {
cases := []struct {
name string
key []byte
length int
expectPanic bool
}{
{
name: "Store key is less then the given length",
EmilGeorgiev marked this conversation as resolved.
Show resolved Hide resolved
key: []byte("hello"),
length: 10,
expectPanic: true,
},
{
name: "Store key is equal to the given length",
key: []byte("store-key"),
length: 9,
expectPanic: false,
},
{
name: "Store key is greater then the given length",
EmilGeorgiev marked this conversation as resolved.
Show resolved Hide resolved
key: []byte("unique"),
length: 3,
expectPanic: false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
defer func() {
r := recover()
if tc.expectPanic {
assert.NotNil(t, r)
return
}
assert.Nil(t, r)
}()
kv.AssertKeyAtLeastLength(tc.key, tc.length)
})
}
}

func TestAssertKeyLength(t *testing.T) {
cases := []struct {
name string
key []byte
length int
expectPanic bool
}{
{
name: "Store key is less then the given length",
EmilGeorgiev marked this conversation as resolved.
Show resolved Hide resolved
key: []byte("hello"),
length: 10,
expectPanic: true,
},
{
name: "Store key is equal to the given length",
key: []byte("store-key"),
length: 9,
expectPanic: false,
},
{
name: "Store key is greater then the given length",
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
key: []byte("unique"),
length: 3,
expectPanic: true,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
defer func() {
r := recover()
EmilGeorgiev marked this conversation as resolved.
Show resolved Hide resolved
if tc.expectPanic {
assert.NotNil(t, r)
return
}
assert.Nil(t, r)
}()
kv.AssertKeyLength(tc.key, tc.length)
})
}
}
Loading