Skip to content

Commit

Permalink
Merge branch 'rc/v0.48.0-rcx' into bump/ostracon
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeseung-bae authored Oct 17, 2023
2 parents 190f105 + ca32838 commit 4b0727b
Show file tree
Hide file tree
Showing 7 changed files with 832 additions and 681 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ 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`
* (x/token) [\#1140](https://github.com/Finschia/finschia-sdk/pull/1140) Refactor unittest for `x/token`
* (ostracon) [\#1142](https://github.com/Finschia/finschia-sdk/pull/1142) Bump up ostracon from v1.1.2-0.20230822110903-449aa3148b12 to v1.1.2

### Bug Fixes
Expand Down
25 changes: 25 additions & 0 deletions testutil/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testutil

import (
"encoding/json"
"fmt"
)

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

return b
}

// W wraps input with double quotes if it is a string or fmt.Stringer.
func W(input any) []byte {
switch input.(type) {
case string, fmt.Stringer:
return []byte(fmt.Sprintf("\"%s\"", input))
default:
panic("unsupported type")
}
}
63 changes: 63 additions & 0 deletions testutil/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package testutil_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"

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

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) {
testCases := map[string]struct {
acceptedType any
}{
"string": {
acceptedType: "test",
},
"sdk.AccAddress": {
acceptedType: sdk.AccAddress("address"),
},
"sdk.Coin": {
acceptedType: sdk.NewInt(1),
},
"should panic for unsupported types": {
acceptedType: 1,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
switch v := tc.acceptedType.(type) {
case string, fmt.Stringer:
require.Equal(t, []byte(fmt.Sprintf(`"%s"`, v)), testutil.W(v))
default:
require.Panics(t, func() {
testutil.W(tc.acceptedType)
})
}
})
}
}
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 4b0727b

Please sign in to comment.