Skip to content

Commit

Permalink
test(collection): refactor overall unittests of x/collection (#1139)
Browse files Browse the repository at this point in the history
* modify MsgOperatorBurnFT

* clarify events

* remove deterministic flag, always test for non-deterministic

* add CHANGELOG

* chore: refactor

* chore: encoding unittest
  • Loading branch information
tkxkd0159 authored Oct 16, 2023
1 parent 8920362 commit 08a4586
Show file tree
Hide file tree
Showing 5 changed files with 616 additions and 378 deletions.
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

0 comments on commit 08a4586

Please sign in to comment.