Skip to content

Commit

Permalink
Bump x/simulation coverage (cosmos#5407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia authored Dec 13, 2019
1 parent d7b0f4b commit bc7c8c7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
77 changes: 77 additions & 0 deletions x/simulation/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package simulation_test

import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

func TestRandomAccounts(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tests := []struct {
name string
n int
want int
}{
{"0-accounts", 0, 0},
{"0-accounts", 1, 1},
{"0-accounts", 1_000, 1_000},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := simulation.RandomAccounts(r, tt.n)
require.Equal(t, tt.want, len(got))
if tt.n == 0 {
return
}
acc, i := simulation.RandomAcc(r, got)
require.True(t, acc.Equals(got[i]))
accFound, found := simulation.FindAccount(got, acc.Address)
require.True(t, found)
require.True(t, accFound.Equals(acc))
})
}
}

func TestFindAccountEmptySlice(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
accs := simulation.RandomAccounts(r, 1)
require.Equal(t, 1, len(accs))
acc, found := simulation.FindAccount(nil, accs[0].Address)
require.False(t, found)
require.Nil(t, acc.Address)
require.Nil(t, acc.PrivKey)
require.Nil(t, acc.PubKey)
}

func TestRandomFees(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tests := []struct {
name string
spendableCoins sdk.Coins
wantEmpty bool
wantErr bool
}{
{"0 coins", sdk.Coins{}, true, false},
{"0 coins", sdk.NewCoins(sdk.NewInt64Coin("aaa", 10), sdk.NewInt64Coin("bbb", 5)), false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := simulation.RandomFees(r, sdk.Context{}, tt.spendableCoins)
if (err != nil) != tt.wantErr {
t.Errorf("RandomFees() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.Equal(t, tt.wantEmpty, got.Empty())
})
}
}
22 changes: 22 additions & 0 deletions x/simulation/rand_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simulation_test
import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand All @@ -11,6 +12,7 @@ import (
)

func TestRandSubsetCoins(t *testing.T) {
t.Parallel()
tests := []struct {
name string
r *rand.Rand
Expand All @@ -31,6 +33,26 @@ func TestRandSubsetCoins(t *testing.T) {
}
}

func TestRandStringOfLength(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().Unix()))
tests := []struct {
name string
n int
want int
}{
{"0-size", 0, 0},
{"10-size", 10, 10},
{"10-size", 1_000_000_000, 1_000_000_000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := simulation.RandStringOfLength(r, tt.n)
require.Equal(t, tt.want, len(got))
})
}
}

func mustParseCoins(s string) sdk.Coins {
coins, err := sdk.ParseCoins(s)
if err != nil {
Expand Down

0 comments on commit bc7c8c7

Please sign in to comment.