From 49d9b984470eb630f6f2032356b6c1a2a1e782aa Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Fri, 28 Feb 2025 15:13:00 +1100 Subject: [PATCH 1/2] chore: remove RandomAlphaNum --- internal/utils/rand.go | 22 ---------------------- internal/utils/rand_test.go | 21 --------------------- 2 files changed, 43 deletions(-) delete mode 100644 internal/utils/rand.go delete mode 100644 internal/utils/rand_test.go diff --git a/internal/utils/rand.go b/internal/utils/rand.go deleted file mode 100644 index f07118d..0000000 --- a/internal/utils/rand.go +++ /dev/null @@ -1,22 +0,0 @@ -package utils - -import ( - "math/rand" -) - -// RandomAlphaNum generates random alphanumeric string -// in case length <= 0 it returns empty string -func RandomAlphaNum(length int) string { - const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - - if length <= 0 { - return "" - } - - randomString := make([]byte, length) - for i := range randomString { - randomString[i] = charset[rand.Intn(len(charset))] - } - - return string(randomString) -} diff --git a/internal/utils/rand_test.go b/internal/utils/rand_test.go deleted file mode 100644 index 4bc472f..0000000 --- a/internal/utils/rand_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package utils - -import ( - "testing" - "github.com/stretchr/testify/assert" -) - -func TestRandomAlphaNum(t *testing.T) { - // negative case - str := RandomAlphaNum(-1) - assert.Empty(t, str) - - // zero length - str = RandomAlphaNum(0) - assert.Empty(t, str) - - // usual case - length := 9 - str = RandomAlphaNum(length) - assert.Len(t, str, length) -} From f814e2109b2d8b40c83bea95a8c3783df244212a Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Fri, 28 Feb 2025 20:58:14 +1100 Subject: [PATCH 2/2] move the RandomAlphaNum under testutil --- internal/db/dbclient_test.go | 13 +++++++------ internal/db/timelock_test.go | 13 +++++++------ testutil/rand.go | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 testutil/rand.go diff --git a/internal/db/dbclient_test.go b/internal/db/dbclient_test.go index 78a128e..4f383a0 100644 --- a/internal/db/dbclient_test.go +++ b/internal/db/dbclient_test.go @@ -5,20 +5,21 @@ package db_test import ( "context" "fmt" + "log" + "os" + "testing" + "time" + "github.com/babylonlabs-io/babylon-staking-indexer/internal/config" "github.com/babylonlabs-io/babylon-staking-indexer/internal/db" "github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils" + "github.com/babylonlabs-io/babylon-staking-indexer/testutil" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "log" - "os" - "testing" - "time" ) const ( @@ -81,7 +82,7 @@ func setupMongoContainer() (*config.DbConfig, func(), error) { // there can be only 1 container with the same name, so we add // random string in the end in case there is still old container running - containerName := "mongo-integration-tests-db-" + utils.RandomAlphaNum(3) + containerName := "mongo-integration-tests-db-" + testutil.RandomAlphaNum(3) resource, err := pool.RunWithOptions(&dockertest.RunOptions{ Name: containerName, Repository: "mongo", diff --git a/internal/db/timelock_test.go b/internal/db/timelock_test.go index 1aee8d2..3ad8dff 100644 --- a/internal/db/timelock_test.go +++ b/internal/db/timelock_test.go @@ -4,13 +4,14 @@ package db_test import ( "context" + "math" + "testing" + "github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" "github.com/babylonlabs-io/babylon-staking-indexer/internal/types" - "github.com/babylonlabs-io/babylon-staking-indexer/internal/utils" + "github.com/babylonlabs-io/babylon-staking-indexer/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "math" - "testing" ) func TestTimeLock(t *testing.T) { @@ -25,17 +26,17 @@ func TestTimeLock(t *testing.T) { }) t.Run("find documents", func(t *testing.T) { expiredDelegation1 := model.TimeLockDocument{ - StakingTxHashHex: utils.RandomAlphaNum(10), + StakingTxHashHex: testutil.RandomAlphaNum(10), ExpireHeight: 1, DelegationSubState: types.SubStateTimelock, } expiredDelegation2 := model.TimeLockDocument{ - StakingTxHashHex: utils.RandomAlphaNum(10), + StakingTxHashHex: testutil.RandomAlphaNum(10), ExpireHeight: 5, DelegationSubState: types.SubStateTimelock, } nonExpiredDelegation := model.TimeLockDocument{ - StakingTxHashHex: utils.RandomAlphaNum(10), + StakingTxHashHex: testutil.RandomAlphaNum(10), ExpireHeight: 10, DelegationSubState: types.SubStateTimelock, } diff --git a/testutil/rand.go b/testutil/rand.go new file mode 100644 index 0000000..881fff4 --- /dev/null +++ b/testutil/rand.go @@ -0,0 +1,22 @@ +package testutil + +import ( + "math/rand" +) + +// RandomAlphaNum generates random alphanumeric string +// in case length <= 0 it returns empty string +func RandomAlphaNum(length int) string { + const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + + if length <= 0 { + return "" + } + + randomString := make([]byte, length) + for i := range randomString { + randomString[i] = charset[rand.Intn(len(charset))] + } + + return string(randomString) +}