From 873024e9aa5c96579ec99a12d159200356c8bee5 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Fri, 5 Apr 2024 09:21:12 +0300 Subject: [PATCH 1/5] test(kv): add unit tests for the helpers functions kv.AssertKeyAtLeastLength and kv.AssertKeyLength --- types/kv/helpers_test.go | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 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 000000000000..8f08a2d0a930 --- /dev/null +++ b/types/kv/helpers_test.go @@ -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", + 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", + 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", + 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", + key: []byte("unique"), + length: 3, + expectPanic: true, + }, + } + + 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.AssertKeyLength(tc.key, tc.length) + }) + } +} From b4f4edb1aa20e3220fd469be99535e9443eae691 Mon Sep 17 00:00:00 2001 From: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Date: Fri, 5 Apr 2024 11:56:18 +0200 Subject: [PATCH 2/5] Update types/kv/helpers_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- types/kv/helpers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/kv/helpers_test.go b/types/kv/helpers_test.go index 8f08a2d0a930..861c544e5629 100644 --- a/types/kv/helpers_test.go +++ b/types/kv/helpers_test.go @@ -69,7 +69,7 @@ func TestAssertKeyLength(t *testing.T) { expectPanic: false, }, { - name: "Store key is greater then the given length", + name: "Store key is greater than the given length", key: []byte("unique"), length: 3, expectPanic: true, From 22d800c14a61f15bbe59d41177c2c04fa53b7533 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Fri, 5 Apr 2024 15:05:59 +0300 Subject: [PATCH 3/5] fix typo and use assert.Panic --- types/kv/helpers_test.go | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/types/kv/helpers_test.go b/types/kv/helpers_test.go index 861c544e5629..af035a313651 100644 --- a/types/kv/helpers_test.go +++ b/types/kv/helpers_test.go @@ -15,19 +15,19 @@ func TestAssertKeyAtLeastLength(t *testing.T) { expectPanic bool }{ { - name: "Store key is less then the given length", + name: "Store key length is less than the given length", key: []byte("hello"), length: 10, expectPanic: true, }, { - name: "Store key is equal to the given length", + name: "Store key length is equal to the given length", key: []byte("store-key"), length: 9, expectPanic: false, }, { - name: "Store key is greater then the given length", + name: "Store key length is greater than the given length", key: []byte("unique"), length: 3, expectPanic: false, @@ -36,14 +36,12 @@ func TestAssertKeyAtLeastLength(t *testing.T) { 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) - }() + if tc.expectPanic { + assert.Panics(t, func() { + kv.AssertKeyAtLeastLength(tc.key, tc.length) + }) + return + } kv.AssertKeyAtLeastLength(tc.key, tc.length) }) } @@ -57,19 +55,19 @@ func TestAssertKeyLength(t *testing.T) { expectPanic bool }{ { - name: "Store key is less then the given length", + name: "Store key length is less than the given length", key: []byte("hello"), length: 10, expectPanic: true, }, { - name: "Store key is equal to the given length", + name: "Store key length is equal to the given length", key: []byte("store-key"), length: 9, expectPanic: false, }, { - name: "Store key is greater than the given length", + name: "Store key length is greater than the given length", key: []byte("unique"), length: 3, expectPanic: true, @@ -78,14 +76,12 @@ func TestAssertKeyLength(t *testing.T) { 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) - }() + if tc.expectPanic { + assert.Panics(t, func() { + kv.AssertKeyLength(tc.key, tc.length) + }) + return + } kv.AssertKeyLength(tc.key, tc.length) }) } From b0b8540cd9fe43d94ccf05b9723bd48c3f011dba Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Wed, 10 Apr 2024 18:54:26 +0300 Subject: [PATCH 4/5] fix lint --- types/kv/helpers_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/types/kv/helpers_test.go b/types/kv/helpers_test.go index af035a313651..0a49332534bf 100644 --- a/types/kv/helpers_test.go +++ b/types/kv/helpers_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/types/kv" + "github.com/stretchr/testify/assert" ) From 4bddefd8a42868c95b02ac6e8be8da58f4d13410 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Wed, 10 Apr 2024 21:54:28 +0300 Subject: [PATCH 5/5] fix lint --- types/kv/helpers_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/kv/helpers_test.go b/types/kv/helpers_test.go index 0a49332534bf..37e9f368532b 100644 --- a/types/kv/helpers_test.go +++ b/types/kv/helpers_test.go @@ -3,9 +3,9 @@ package kv_test import ( "testing" - "github.com/cosmos/cosmos-sdk/types/kv" - "github.com/stretchr/testify/assert" + + "github.com/cosmos/cosmos-sdk/types/kv" ) func TestAssertKeyAtLeastLength(t *testing.T) {