forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_balances_remove_test.go
145 lines (118 loc) · 3.31 KB
/
genesis_balances_remove_test.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"testing"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGenesis_Balances_Remove(t *testing.T) {
t.Parallel()
t.Run("invalid genesis", func(t *testing.T) {
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"balances",
"remove",
"--genesis-path",
"dummy-path",
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, errUnableToLoadGenesis.Error())
})
t.Run("genesis app state not set", func(t *testing.T) {
t.Parallel()
dummyKey := getDummyKey(t)
tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)
genesis := getDefaultGenesis()
genesis.AppState = nil // not set
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"balances",
"remove",
"--genesis-path",
tempGenesis.Name(),
"--address",
dummyKey.Address().String(),
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, errAppStateNotSet.Error())
})
t.Run("address is present", func(t *testing.T) {
t.Parallel()
dummyKey := getDummyKey(t)
tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)
genesis := getDefaultGenesis()
state := gnoland.GnoGenesisState{
// Set an initial balance value
Balances: []gnoland.Balance{
{
Address: dummyKey.Address(),
Amount: std.NewCoins(std.NewCoin(ugnot.Denom, 100)),
},
},
}
genesis.AppState = state
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"balances",
"remove",
"--genesis-path",
tempGenesis.Name(),
"--address",
dummyKey.Address().String(),
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)
// Validate the genesis was updated
genesis, loadErr := types.GenesisDocFromFile(tempGenesis.Name())
require.NoError(t, loadErr)
require.NotNil(t, genesis.AppState)
state, ok := genesis.AppState.(gnoland.GnoGenesisState)
require.True(t, ok)
assert.Len(t, state.Balances, 0)
})
t.Run("address not present", func(t *testing.T) {
t.Parallel()
dummyKey := getDummyKey(t)
tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)
genesis := getDefaultGenesis()
state := gnoland.GnoGenesisState{
Balances: []gnoland.Balance{}, // Empty initial balance
}
genesis.AppState = state
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))
// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"balances",
"remove",
"--genesis-path",
tempGenesis.Name(),
"--address",
dummyKey.Address().String(),
}
// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, errBalanceNotFound.Error())
})
}