@@ -6,40 +6,10 @@ import (
6
6
7
7
"github.com/stretchr/testify/require"
8
8
9
- abci "github.com/tendermint/tendermint/abci/types"
10
- dbm "github.com/tendermint/tendermint/libs/db"
11
- "github.com/tendermint/tendermint/libs/log"
12
-
13
- "github.com/cosmos/cosmos-sdk/codec"
14
- "github.com/cosmos/cosmos-sdk/store"
15
9
"github.com/cosmos/cosmos-sdk/store/prefix"
16
10
sdk "github.com/cosmos/cosmos-sdk/types"
17
11
)
18
12
19
- func defaultContext (key sdk.StoreKey , tkey sdk.StoreKey ) sdk.Context {
20
- db := dbm .NewMemDB ()
21
- cms := store .NewCommitMultiStore (db )
22
- cms .MountStoreWithDB (key , sdk .StoreTypeIAVL , db )
23
- cms .MountStoreWithDB (tkey , sdk .StoreTypeTransient , db )
24
- cms .LoadLatestVersion ()
25
- ctx := sdk .NewContext (cms , abci.Header {}, false , log .NewNopLogger ())
26
- return ctx
27
- }
28
-
29
- type invalid struct {}
30
-
31
- type s struct {
32
- I int
33
- }
34
-
35
- func createTestCodec () * codec.Codec {
36
- cdc := codec .New ()
37
- sdk .RegisterCodec (cdc )
38
- cdc .RegisterConcrete (s {}, "test/s" , nil )
39
- cdc .RegisterConcrete (invalid {}, "test/invalid" , nil )
40
- return cdc
41
- }
42
-
43
13
func TestKeeper (t * testing.T ) {
44
14
kvs := []struct {
45
15
key string
@@ -66,11 +36,8 @@ func TestKeeper(t *testing.T) {
66
36
[]byte ("extra2" ), string ("" ),
67
37
)
68
38
69
- cdc := codec .New ()
70
- skey := sdk .NewKVStoreKey ("test" )
71
- tkey := sdk .NewTransientStoreKey ("transient_test" )
72
- ctx := defaultContext (skey , tkey )
73
- keeper := NewKeeper (cdc , skey , tkey , DefaultCodespace )
39
+ cdc , ctx , skey , _ , keeper := testComponents ()
40
+
74
41
store := prefix .NewStore (ctx .KVStore (skey ), []byte ("test/" ))
75
42
space := keeper .Subspace ("test" ).WithKeyTable (table )
76
43
@@ -137,11 +104,7 @@ func indirect(ptr interface{}) interface{} {
137
104
}
138
105
139
106
func TestSubspace (t * testing.T ) {
140
- cdc := createTestCodec ()
141
- key := sdk .NewKVStoreKey ("test" )
142
- tkey := sdk .NewTransientStoreKey ("transient_test" )
143
- ctx := defaultContext (key , tkey )
144
- keeper := NewKeeper (cdc , key , tkey , DefaultCodespace )
107
+ cdc , ctx , key , _ , keeper := testComponents ()
145
108
146
109
kvs := []struct {
147
110
key string
@@ -216,3 +179,34 @@ func TestSubspace(t *testing.T) {
216
179
require .Equal (t , kv .param , indirect (kv .ptr ), "stored param not equal, tc #%d" , i )
217
180
}
218
181
}
182
+
183
+ type paramJSON struct {
184
+ Param1 int64 `json:"param1,omitempty"`
185
+ Param2 string `json:"param2,omitempty"`
186
+ }
187
+
188
+ func TestJSONUpdate (t * testing.T ) {
189
+ _ , ctx , _ , _ , keeper := testComponents ()
190
+
191
+ key := []byte ("key" )
192
+
193
+ space := keeper .Subspace ("test" ).WithKeyTable (NewKeyTable (key , paramJSON {}))
194
+
195
+ var param paramJSON
196
+
197
+ space .Update (ctx , key , []byte (`{"param1": "10241024"}` ))
198
+ space .Get (ctx , key , & param )
199
+ require .Equal (t , paramJSON {10241024 , "" }, param )
200
+
201
+ space .Update (ctx , key , []byte (`{"param2": "helloworld"}` ))
202
+ space .Get (ctx , key , & param )
203
+ require .Equal (t , paramJSON {10241024 , "helloworld" }, param )
204
+
205
+ space .Update (ctx , key , []byte (`{"param1": "20482048"}` ))
206
+ space .Get (ctx , key , & param )
207
+ require .Equal (t , paramJSON {20482048 , "helloworld" }, param )
208
+
209
+ space .Update (ctx , key , []byte (`{"param1": "40964096", "param2": "goodbyeworld"}` ))
210
+ space .Get (ctx , key , & param )
211
+ require .Equal (t , paramJSON {40964096 , "goodbyeworld" }, param )
212
+ }
0 commit comments