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