From c56152dfac3574b33266cac3224eacf7d29a4216 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Thu, 11 Apr 2024 01:27:40 +0300 Subject: [PATCH] =?UTF-8?q?test(kv):=20add=20unit=20tests=20for=20the=20he?= =?UTF-8?q?lpers=20functions=20kv.AssertKeyAtLeas=E2=80=A6=20(#19965)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- types/kv/helpers_test.go | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 types/kv/helpers_test.go diff --git a/types/kv/helpers_test.go b/types/kv/helpers_test.go new file mode 100644 index 0000000000..37e9f36853 --- /dev/null +++ b/types/kv/helpers_test.go @@ -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) + }) + } +}