forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper_test.go
231 lines (191 loc) · 7.97 KB
/
keeper_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package params
import (
"reflect"
"testing"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
subspace "github.com/maticnetwork/heimdall/params/subspace"
)
func TestKeeper(t *testing.T) {
t.Parallel()
kvs := []struct {
key string
param int64
}{
{"key1", 10},
{"key2", 55},
{"key3", 182},
{"key4", 17582},
{"key5", 2768554},
{"key6", 1157279},
{"key7", 9058701},
}
table := subspace.NewKeyTable(
[]byte("key1"), int64(0),
[]byte("key2"), int64(0),
[]byte("key3"), int64(0),
[]byte("key4"), int64(0),
[]byte("key5"), int64(0),
[]byte("key6"), int64(0),
[]byte("key7"), int64(0),
[]byte("extra1"), bool(false),
[]byte("extra2"), string(""),
)
cdc, ctx, skey, _, keeper := testComponents()
store := prefix.NewStore(ctx.KVStore(skey), []byte("test/"))
space := keeper.Subspace("test").WithKeyTable(table)
// Set params
for i, kv := range kvs {
require.NotPanics(t, func() { space.Set(ctx, []byte(kv.key), kv.param) }, "space.Set panics, tc #%d", i)
}
// Test space.Get
for i, kv := range kvs {
var param int64
require.NotPanics(t, func() { space.Get(ctx, []byte(kv.key), ¶m) }, "space.Get panics, tc #%d", i)
require.Equal(t, kv.param, param, "stored param not equal, tc #%d", i)
}
// Test space.GetRaw
for i, kv := range kvs {
var param int64
bz := space.GetRaw(ctx, []byte(kv.key))
err := cdc.UnmarshalJSON(bz, ¶m)
require.Nil(t, err, "err is not nil, tc #%d", i)
require.Equal(t, kv.param, param, "stored param not equal, tc #%d", i)
}
// Test store.Get equals space.Get
for i, kv := range kvs {
var param int64
bz := store.Get([]byte(kv.key))
require.NotNil(t, bz, "KVStore.Get returns nil, tc #%d", i)
err := cdc.UnmarshalJSON(bz, ¶m)
require.NoError(t, err, "UnmarshalJSON returns error, tc #%d", i)
require.Equal(t, kv.param, param, "stored param not equal, tc #%d", i)
}
// Test invalid space.Get
for i, kv := range kvs {
var param bool
require.Panics(t, func() { space.Get(ctx, []byte(kv.key), ¶m) }, "invalid space.Get not panics, tc #%d", i)
}
// Test invalid space.Set
for i, kv := range kvs {
require.Panics(t, func() { space.Set(ctx, []byte(kv.key), true) }, "invalid space.Set not panics, tc #%d", i)
}
// Test GetSubspace
for i, kv := range kvs {
var gparam, param int64
gspace, ok := keeper.GetSubspace("test")
require.True(t, ok, "cannot retrieve subspace, tc #%d", i)
require.NotPanics(t, func() { gspace.Get(ctx, []byte(kv.key), &gparam) })
require.NotPanics(t, func() { space.Get(ctx, []byte(kv.key), ¶m) })
require.Equal(t, gparam, param, "GetSubspace().Get not equal with space.Get, tc #%d", i)
require.NotPanics(t, func() { gspace.Set(ctx, []byte(kv.key), int64(i)) })
require.NotPanics(t, func() { space.Get(ctx, []byte(kv.key), ¶m) })
require.Equal(t, int64(i), param, "GetSubspace().Set not equal with space.Get, tc #%d", i)
}
}
func indirect(ptr interface{}) interface{} {
return reflect.ValueOf(ptr).Elem().Interface()
}
func TestSubspaceCreation(t *testing.T) {
_, _, _, _, keeper := testComponents()
require.Panics(t, func() { keeper.Subspace("") }, "creating subspace with empty should panic")
// create keeper
_ = keeper.Subspace("test")
require.Panics(t, func() { keeper.Subspace("test") }, "creating subspace with same key should panic")
_, ok := keeper.GetSubspace("test1")
require.False(t, ok, "getting subspace with no key not return not-ok result")
}
func TestSubspace(t *testing.T) {
cdc, ctx, key, _, keeper := testComponents()
kvs := []struct {
key string
param interface{}
zero interface{}
ptr interface{}
}{
{"string", "test", "", new(string)},
{"bool", true, false, new(bool)},
{"int16", int16(1), int16(0), new(int16)},
{"int32", int32(1), int32(0), new(int32)},
{"int64", int64(1), int64(0), new(int64)},
{"uint16", uint16(1), uint16(0), new(uint16)},
{"uint32", uint32(1), uint32(0), new(uint32)},
{"uint64", uint64(1), uint64(0), new(uint64)},
{"int", sdk.NewInt(1), *new(sdk.Int), new(sdk.Int)},
{"uint", sdk.NewUint(1), *new(sdk.Uint), new(sdk.Uint)},
{"dec", sdk.NewDec(1), *new(sdk.Dec), new(sdk.Dec)},
{"struct", s{1}, s{0}, new(s)},
}
table := subspace.NewKeyTable(
[]byte("string"), string(""),
[]byte("bool"), bool(false),
[]byte("int16"), int16(0),
[]byte("int32"), int32(0),
[]byte("int64"), int64(0),
[]byte("uint16"), uint16(0),
[]byte("uint32"), uint32(0),
[]byte("uint64"), uint64(0),
[]byte("int"), sdk.Int{},
[]byte("uint"), sdk.Uint{},
[]byte("dec"), sdk.Dec{},
[]byte("struct"), s{},
)
store := prefix.NewStore(ctx.KVStore(key), []byte("test/"))
space := keeper.Subspace("test").WithKeyTable(table)
// Test space.Set, space.Modified
for i, kv := range kvs {
require.False(t, space.Modified(ctx, []byte(kv.key)), "space.Modified returns true before setting, tc #%d", i)
require.NotPanics(t, func() { space.Set(ctx, []byte(kv.key), kv.param) }, "space.Set panics, tc #%d", i)
require.True(t, space.Modified(ctx, []byte(kv.key)), "space.Modified returns false after setting, tc #%d", i)
}
// Test space.Get, space.GetIfExists
for i, kv := range kvs {
require.NotPanics(t, func() { space.GetIfExists(ctx, []byte("invalid"), kv.ptr) }, "space.GetIfExists panics when no value exists, tc #%d", i)
require.Equal(t, kv.zero, indirect(kv.ptr), "space.GetIfExists unmarshalls when no value exists, tc #%d", i)
require.Panics(t, func() { space.Get(ctx, []byte("invalid"), kv.ptr) }, "invalid space.Get not panics when no value exists, tc #%d", i)
require.Equal(t, kv.zero, indirect(kv.ptr), "invalid space.Get unmarshalls when no value exists, tc #%d", i)
require.NotPanics(t, func() { space.GetIfExists(ctx, []byte(kv.key), kv.ptr) }, "space.GetIfExists panics, tc #%d", i)
require.Equal(t, kv.param, indirect(kv.ptr), "stored param not equal, tc #%d", i)
require.NotPanics(t, func() { space.Get(ctx, []byte(kv.key), kv.ptr) }, "space.Get panics, tc #%d", i)
require.Equal(t, kv.param, indirect(kv.ptr), "stored param not equal, tc #%d", i)
require.Panics(t, func() { space.Get(ctx, []byte("invalid"), kv.ptr) }, "invalid space.Get not panics when no value exists, tc #%d", i)
require.Equal(t, kv.param, indirect(kv.ptr), "invalid space.Get unmarshalls when no value existt, tc #%d", i)
require.Panics(t, func() { space.Get(ctx, []byte(kv.key), nil) }, "invalid space.Get not panics when the pointer is nil, tc #%d", i)
require.Panics(t, func() { space.Get(ctx, []byte(kv.key), new(invalid)) }, "invalid space.Get not panics when the pointer is different type, tc #%d", i)
}
// Test store.Get equals space.Get
for i, kv := range kvs {
bz := store.Get([]byte(kv.key))
require.NotNil(t, bz, "store.Get() returns nil, tc #%d", i)
err := cdc.UnmarshalJSON(bz, kv.ptr)
require.NoError(t, err, "cdc.UnmarshalJSON() returns error, tc #%d", i)
require.Equal(t, kv.param, indirect(kv.ptr), "stored param not equal, tc #%d", i)
}
}
type paramJSON struct {
Param1 int64 `json:"param1,omitempty" yaml:"param1,omitempty"`
Param2 string `json:"param2,omitempty" yaml:"param2,omitempty"`
}
func TestJSONUpdate(t *testing.T) {
_, ctx, _, _, keeper := testComponents()
key := []byte("key")
space := keeper.Subspace("test").WithKeyTable(subspace.NewKeyTable(key, paramJSON{}))
var param paramJSON
err := space.Update(ctx, key, []byte(`{"param1": "10241024"}`))
require.NoError(t, err)
space.Get(ctx, key, ¶m)
require.Equal(t, paramJSON{10241024, ""}, param)
err = space.Update(ctx, key, []byte(`{"param2": "helloworld"}`))
require.NoError(t, err)
space.Get(ctx, key, ¶m)
require.Equal(t, paramJSON{10241024, "helloworld"}, param)
err = space.Update(ctx, key, []byte(`{"param1": "20482048"}`))
require.NoError(t, err)
space.Get(ctx, key, ¶m)
require.Equal(t, paramJSON{20482048, "helloworld"}, param)
err = space.Update(ctx, key, []byte(`{"param1": "40964096", "param2": "goodbyeworld"}`))
require.NoError(t, err)
space.Get(ctx, key, ¶m)
require.Equal(t, paramJSON{40964096, "goodbyeworld"}, param)
}