-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
101 lines (84 loc) · 2.49 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package testapp
import (
"context"
"fmt"
"testing"
"time"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
bankkeeper "github.com/ODIN-PROTOCOL/odin-core/x/bank/keeper"
)
// ParseTime is a helper function to parse from number to time.Time with UTC locale.
func ParseTime(t int64) time.Time {
return time.Unix(t, 0).UTC()
}
type GasRecord struct {
Gas storetypes.Gas
Descriptor string
}
// GasMeterWrapper wrap gas meter for testing purpose
type GasMeterWrapper struct {
storetypes.GasMeter
GasRecords []GasRecord
}
func (m *GasMeterWrapper) ConsumeGas(amount storetypes.Gas, descriptor string) {
m.GasRecords = append(m.GasRecords, GasRecord{amount, descriptor})
m.GasMeter.ConsumeGas(amount, descriptor)
}
func (m *GasMeterWrapper) CountRecord(amount storetypes.Gas, descriptor string) int {
count := 0
for _, r := range m.GasRecords {
if r.Gas == amount && r.Descriptor == descriptor {
count++
}
}
return count
}
func (m *GasMeterWrapper) CountDescriptor(descriptor string) int {
count := 0
for _, r := range m.GasRecords {
if r.Descriptor == descriptor {
count++
}
}
return count
}
// NewGasMeterWrapper to wrap gas meters for testing purposes
func NewGasMeterWrapper(meter storetypes.GasMeter) *GasMeterWrapper {
return &GasMeterWrapper{meter, nil}
}
func MustGetBalances(ctx context.Context, bankKeeper bankkeeper.WrappedBankKeeper, address sdk.AccAddress) sdk.Coins {
balancesRes, err := bankKeeper.AllBalances(
ctx,
banktypes.NewQueryAllBalancesRequest(address, &query.PageRequest{}, false),
)
if err != nil {
panic(err)
}
return balancesRes.Balances
}
func CheckBalances(
t *testing.T,
ctx context.Context,
bankKeeper bankkeeper.WrappedBankKeeper,
address sdk.AccAddress,
expected sdk.Coins,
) {
balancesRes, err := bankKeeper.AllBalances(
ctx,
banktypes.NewQueryAllBalancesRequest(address, &query.PageRequest{}, false),
)
require.NoError(t, err)
require.True(t, expected.Equal(balancesRes.Balances))
}
// CheckErrorf checks whether
// - error type is wrapped inside the given error
// - error match given message string combined with error type
func CheckErrorf(t *testing.T, err error, errType error, msg string, a ...interface{}) {
require.ErrorIs(t, err, errType)
formattedMsg := fmt.Sprintf(msg, a...)
require.EqualError(t, err, fmt.Sprintf("%s: %s", formattedMsg, errType))
}