@@ -12,21 +12,13 @@ import (
12
12
13
13
func TestState (t * testing.T ) {
14
14
15
- s := NewState (types .NewMemKVStore ())
16
-
17
- s .SetChainID ("testchain" )
18
- assert .True (t , s .GetChainID () == "testchain" , "ChainID is improperly stored" )
19
-
20
- setRecords := func (kv types.KVStore ) {
21
- kv .Set ([]byte ("foo" ), []byte ("snake" ))
22
- kv .Set ([]byte ("bar" ), []byte ("mouse" ))
23
- }
24
-
25
- setRecords (s )
26
- assert .True (t , bytes .Equal (s .Get ([]byte ("foo" )), []byte ("snake" )), "state doesn't retrieve after Set" )
27
- assert .True (t , bytes .Equal (s .Get ([]byte ("bar" )), []byte ("mouse" )), "state doesn't retrieve after Set" )
15
+ //States and Stores for tests
16
+ store := types .NewMemKVStore ()
17
+ state := NewState (store )
18
+ cache := state .CacheWrap ()
19
+ eyesCli := eyes .NewLocalClient ("" , 0 )
28
20
29
- // Test account retrieve
21
+ //Account and address for tests
30
22
dumAddr := []byte ("dummyAddress" )
31
23
32
24
acc := & types.Account {
@@ -35,34 +27,83 @@ func TestState(t *testing.T) {
35
27
Balance : nil ,
36
28
}
37
29
38
- s .SetAccount (dumAddr , acc )
39
- assert .True (t , s .GetAccount (dumAddr ).Sequence == 1 , "GetAccount not retrieving" )
30
+ //reset the store/state/cache
31
+ reset := func () {
32
+ store = types .NewMemKVStore ()
33
+ state = NewState (store )
34
+ cache = state .CacheWrap ()
35
+ }
40
36
41
- //Test CacheWrap with local mem store
42
- store := types .NewMemKVStore ()
43
- s = NewState (store )
44
- cache := s .CacheWrap ()
45
- setRecords (cache )
46
- assert .True (t , ! bytes .Equal (store .Get ([]byte ("foo" )), []byte ("snake" )), "store retrieving before Commit" )
47
- assert .True (t , ! bytes .Equal (store .Get ([]byte ("bar" )), []byte ("mouse" )), "store retrieving before Commit" )
48
- cache .CacheSync ()
49
- assert .True (t , bytes .Equal (store .Get ([]byte ("foo" )), []byte ("snake" )), "store doesn't retrieve after Commit" )
50
- assert .True (t , bytes .Equal (store .Get ([]byte ("bar" )), []byte ("mouse" )), "store doesn't retrieve after Commit" )
51
-
52
- //Test Commit on state with non-merkle store
53
- assert .True (t , ! s .Commit ().IsOK (), "Commit shouldn't work with non-merkle store" )
54
-
55
- //Test CacheWrap with merkleeyes client store
56
- eyesCli := eyes .NewLocalClient ("" , 0 )
57
- s = NewState (eyesCli )
37
+ //set the state to using the eyesCli instead of MemKVStore
38
+ useEyesCli := func () {
39
+ state = NewState (eyesCli )
40
+ cache = state .CacheWrap ()
41
+ }
42
+
43
+ //key value pairs to be tested within the system
44
+ keyvalue := []struct {
45
+ key string
46
+ value string
47
+ }{
48
+ {"foo" , "snake" },
49
+ {"bar" , "mouse" },
50
+ }
51
+
52
+ //set the kvc to have all the key value pairs
53
+ setRecords := func (kv types.KVStore ) {
54
+ for _ , n := range keyvalue {
55
+ kv .Set ([]byte (n .key ), []byte (n .value ))
56
+ }
57
+ }
58
58
59
- cache = s .CacheWrap ()
60
- setRecords (cache )
61
- assert .True (t , ! bytes .Equal (eyesCli .Get ([]byte ("foo" )), []byte ("snake" )), "store retrieving before Commit" )
62
- assert .True (t , ! bytes .Equal (eyesCli .Get ([]byte ("bar" )), []byte ("mouse" )), "store retrieving before Commit" )
63
- cache .CacheSync ()
64
- assert .True (t , s .Commit ().IsOK (), "Bad Commit" )
65
- assert .True (t , bytes .Equal (eyesCli .Get ([]byte ("foo" )), []byte ("snake" )), "store doesn't retrieve after Commit" )
66
- assert .True (t , bytes .Equal (eyesCli .Get ([]byte ("bar" )), []byte ("mouse" )), "store doesn't retrieve after Commit" )
59
+ //store has all the key value pairs
60
+ storeHasAll := func (kv types.KVStore ) bool {
61
+ for _ , n := range keyvalue {
62
+ if ! bytes .Equal (kv .Get ([]byte (n .key )), []byte (n .value )) {
63
+ return false
64
+ }
65
+ }
66
+ return true
67
+ }
67
68
69
+ //define the test list
70
+ testList := []struct {
71
+ testPass func () bool
72
+ errMsg string
73
+ }{
74
+ //test chainID
75
+ {func () bool { state .SetChainID ("testchain" ); return state .GetChainID () == "testchain" },
76
+ "ChainID is improperly stored" },
77
+
78
+ //test basic retrieve
79
+ {func () bool { setRecords (state ); return storeHasAll (state ) },
80
+ "state doesn't retrieve after Set" },
81
+
82
+ // Test account retrieve
83
+ {func () bool { state .SetAccount (dumAddr , acc ); return state .GetAccount (dumAddr ).Sequence == 1 },
84
+ "GetAccount not retrieving" },
85
+
86
+ //Test CacheWrap with local mem store
87
+ {func () bool { reset (); setRecords (cache ); return ! storeHasAll (store ) },
88
+ "store retrieving before CacheSync" },
89
+ {func () bool { cache .CacheSync (); return storeHasAll (store ) },
90
+ "store doesn't retrieve after CacheSync" },
91
+
92
+ //Test Commit on state with non-merkle store
93
+ {func () bool { return ! state .Commit ().IsOK () },
94
+ "Commit shouldn't work with non-merkle store" },
95
+
96
+ //Test CacheWrap with merkleeyes client store
97
+ {func () bool { useEyesCli (); setRecords (cache ); return ! storeHasAll (eyesCli ) },
98
+ "eyesCli retrieving before Commit" },
99
+ {func () bool { cache .CacheSync (); return state .Commit ().IsOK () },
100
+ "Bad Commit" },
101
+ {func () bool { return storeHasAll (eyesCli ) },
102
+ "eyesCli doesn't retrieve after Commit" },
103
+ }
104
+
105
+ //execute the tests
106
+ for _ , tl := range testList {
107
+ assert .True (t , tl .testPass (), tl .errMsg )
108
+ }
68
109
}
0 commit comments