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