github.com/PikeEcosystem/tendermint@v0.0.4/state/indexer/block/kv/kv_test.go (about) 1 package kv_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 abci "github.com/tendermint/tendermint/abci/types" 10 db "github.com/tendermint/tm-db" 11 12 "github.com/PikeEcosystem/tendermint/libs/pubsub/query" 13 blockidxkv "github.com/PikeEcosystem/tendermint/state/indexer/block/kv" 14 "github.com/PikeEcosystem/tendermint/types" 15 ) 16 17 func TestBlockIndexer(t *testing.T) { 18 store := db.NewPrefixDB(db.NewMemDB(), []byte("block_events")) 19 indexer := blockidxkv.New(store) 20 require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{ 21 Header: types.Header{Height: 1}, 22 ResultBeginBlock: abci.ResponseBeginBlock{ 23 Events: []abci.Event{ 24 { 25 Type: "begin_event", 26 Attributes: []abci.EventAttribute{ 27 { 28 Key: []byte("proposer"), 29 Value: []byte("FCAA001"), 30 Index: true, 31 }, 32 }, 33 }, 34 }, 35 }, 36 ResultEndBlock: abci.ResponseEndBlock{ 37 Events: []abci.Event{ 38 { 39 Type: "end_event", 40 Attributes: []abci.EventAttribute{ 41 { 42 Key: []byte("foo"), 43 Value: []byte("100"), 44 Index: true, 45 }, 46 }, 47 }, 48 }, 49 }, 50 })) 51 52 for i := 2; i < 12; i++ { 53 var index bool 54 if i%2 == 0 { 55 index = true 56 } 57 58 require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{ 59 Header: types.Header{Height: int64(i)}, 60 ResultBeginBlock: abci.ResponseBeginBlock{ 61 Events: []abci.Event{ 62 { 63 Type: "begin_event", 64 Attributes: []abci.EventAttribute{ 65 { 66 Key: []byte("proposer"), 67 Value: []byte("FCAA001"), 68 Index: true, 69 }, 70 }, 71 }, 72 }, 73 }, 74 ResultEndBlock: abci.ResponseEndBlock{ 75 Events: []abci.Event{ 76 { 77 Type: "end_event", 78 Attributes: []abci.EventAttribute{ 79 { 80 Key: []byte("foo"), 81 Value: []byte(fmt.Sprintf("%d", i)), 82 Index: index, 83 }, 84 }, 85 }, 86 }, 87 }, 88 })) 89 } 90 91 testCases := map[string]struct { 92 q *query.Query 93 results []int64 94 }{ 95 "block.height = 100": { 96 q: query.MustParse("block.height = 100"), 97 results: []int64{}, 98 }, 99 "block.height = 5": { 100 q: query.MustParse("block.height = 5"), 101 results: []int64{5}, 102 }, 103 "begin_event.key1 = 'value1'": { 104 q: query.MustParse("begin_event.key1 = 'value1'"), 105 results: []int64{}, 106 }, 107 "begin_event.proposer = 'FCAA001'": { 108 q: query.MustParse("begin_event.proposer = 'FCAA001'"), 109 results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 110 }, 111 "end_event.foo <= 5": { 112 q: query.MustParse("end_event.foo <= 5"), 113 results: []int64{2, 4}, 114 }, 115 "end_event.foo >= 100": { 116 q: query.MustParse("end_event.foo >= 100"), 117 results: []int64{1}, 118 }, 119 "block.height > 2 AND end_event.foo <= 8": { 120 q: query.MustParse("block.height > 2 AND end_event.foo <= 8"), 121 results: []int64{4, 6, 8}, 122 }, 123 "begin_event.proposer CONTAINS 'FFFFFFF'": { 124 q: query.MustParse("begin_event.proposer CONTAINS 'FFFFFFF'"), 125 results: []int64{}, 126 }, 127 "begin_event.proposer CONTAINS 'FCAA001'": { 128 q: query.MustParse("begin_event.proposer CONTAINS 'FCAA001'"), 129 results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 130 }, 131 } 132 133 for name, tc := range testCases { 134 tc := tc 135 t.Run(name, func(t *testing.T) { 136 results, err := indexer.Search(context.Background(), tc.q) 137 require.NoError(t, err) 138 require.Equal(t, tc.results, results) 139 }) 140 } 141 }