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(collection): refactor overall unittests of x/collection #1139

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/collection) [\#1131](https://github.com/Finschia/finschia-sdk/pull/1131) add additional unittest of x/collection(`MsgIssueFT`, `MsgMintFT`, `MsgBurnFT`)
* (x/collection) [\#1133](https://github.com/Finschia/finschia-sdk/pull/1133) Refactor unittest for `SendFT`, `OperatorSendFT`, `AuthorizeOperator`, and `RevokeOperator` to check more states
* (x/token) [\#1137](https://github.com/Finschia/finschia-sdk/pull/1137) Add test for event compatibility
* (x/collection) [\#1139](https://github.com/Finschia/finschia-sdk/pull/1139) refactor overall unittests of `x/collection`

### Bug Fixes
* (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu)
Expand Down
19 changes: 19 additions & 0 deletions testutil/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testutil

import (
"encoding/json"
"fmt"
)

func MustJSONMarshal(v any) []byte {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}

return b
}

func W(input string) []byte {
return []byte(fmt.Sprintf("\"%s\"", input))
}
33 changes: 33 additions & 0 deletions testutil/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package testutil_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/Finschia/finschia-sdk/testutil"
)

func TestMustJSONMarshal(t *testing.T) {
type tc struct {
Name string `json:"myName"`
Order string `json:"myOrder"`
}

a := tc{
Name: "test",
Order: "first",
}
b := new(tc)

marshaled := testutil.MustJSONMarshal(a)
err := json.Unmarshal(marshaled, b)
require.NoError(t, err)
require.Equal(t, a, *b)
require.Panics(t, func() { testutil.MustJSONMarshal(make(chan int)) })
}

func TestW(t *testing.T) {
require.Equal(t, []byte(`"test"`), testutil.W("test"))
}
42 changes: 13 additions & 29 deletions x/collection/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/suite"
Expand All @@ -18,8 +17,6 @@ import (
type KeeperTestSuite struct {
suite.Suite

deterministic bool

ctx sdk.Context
goCtx context.Context
keeper keeper.Keeper
Expand All @@ -44,29 +41,21 @@ type KeeperTestSuite struct {
}

func (s *KeeperTestSuite) createRandomAccounts(accNum int) []sdk.AccAddress {
if s.deterministic {
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
addresses[i] = sdk.AccAddress(fmt.Sprintf("address%d", i))
}
return addresses
} else {
seenAddresses := make(map[string]bool, accNum)
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
var address sdk.AccAddress
for {
pk := secp256k1.GenPrivKey().PubKey()
address = sdk.AccAddress(pk.Address())
if !seenAddresses[address.String()] {
seenAddresses[address.String()] = true
break
}
seenAddresses := make(map[string]bool, accNum)
addresses := make([]sdk.AccAddress, accNum)
for i := range addresses {
var address sdk.AccAddress
for {
pk := secp256k1.GenPrivKey().PubKey()
address = sdk.AccAddress(pk.Address())
if !seenAddresses[address.String()] {
seenAddresses[address.String()] = true
break
}
addresses[i] = address
}
return addresses
addresses[i] = address
}
return addresses
}

func (s *KeeperTestSuite) SetupTest() {
Expand Down Expand Up @@ -188,10 +177,5 @@ func (s *KeeperTestSuite) SetupTest() {
}

func TestKeeperTestSuite(t *testing.T) {
for _, deterministic := range []bool{
false,
true,
} {
suite.Run(t, &KeeperTestSuite{deterministic: deterministic})
}
suite.Run(t, new(KeeperTestSuite))
}
Loading
Loading