github.com/onflow/flow-go@v0.33.17/engine/access/rpc/backend/event_index_test.go (about)

     1  package backend
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/mock"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/onflow/flow-go/model/flow"
    14  	storagemock "github.com/onflow/flow-go/storage/mock"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  // TestGetEvents tests that GetEvents returns the events in the correct order
    19  func TestGetEvents(t *testing.T) {
    20  	expectedEvents := make(flow.EventsList, 0, 6)
    21  	expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 0, 1)...)
    22  	expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 1, 3)...)
    23  	expectedEvents = append(expectedEvents, generateTxEvents(unittest.IdentifierFixture(), 2, 2)...)
    24  
    25  	storedEvents := make([]flow.Event, len(expectedEvents))
    26  	copy(storedEvents, expectedEvents)
    27  
    28  	// sort events in storage order (by tx ID)
    29  	sort.Slice(storedEvents, func(i, j int) bool {
    30  		cmp := bytes.Compare(storedEvents[i].TransactionID[:], storedEvents[j].TransactionID[:])
    31  		if cmp == 0 {
    32  			if storedEvents[i].TransactionIndex == storedEvents[j].TransactionIndex {
    33  				return storedEvents[i].EventIndex < storedEvents[j].EventIndex
    34  			}
    35  			return storedEvents[i].TransactionIndex < storedEvents[j].TransactionIndex
    36  		}
    37  		return cmp < 0
    38  	})
    39  
    40  	events := storagemock.NewEvents(t)
    41  	header := unittest.BlockHeaderFixture()
    42  
    43  	events.On("ByBlockID", mock.Anything).Return(func(blockID flow.Identifier) ([]flow.Event, error) {
    44  		return storedEvents, nil
    45  	})
    46  
    47  	eventsIndex := NewEventsIndex(events)
    48  	err := eventsIndex.Initialize(&mockIndexReporter{})
    49  	require.NoError(t, err)
    50  
    51  	actualEvents, err := eventsIndex.ByBlockID(header.ID(), header.Height)
    52  	require.NoError(t, err)
    53  
    54  	// output events should be in the same order as the expected events
    55  	assert.Len(t, actualEvents, len(expectedEvents))
    56  	for i, event := range actualEvents {
    57  		assert.Equal(t, expectedEvents[i], event)
    58  	}
    59  }
    60  
    61  func generateTxEvents(txID flow.Identifier, txIndex uint32, count int) flow.EventsList {
    62  	events := make(flow.EventsList, count)
    63  	for i := 0; i < count; i++ {
    64  		events[i] = flow.Event{
    65  			Type:             unittest.EventTypeFixture(flow.Localnet),
    66  			TransactionID:    txID,
    67  			TransactionIndex: txIndex,
    68  			EventIndex:       uint32(i),
    69  		}
    70  	}
    71  	return events
    72  }
    73  
    74  type mockIndexReporter struct{}
    75  
    76  func (r *mockIndexReporter) LowestIndexedHeight() (uint64, error) {
    77  	return 0, nil
    78  }
    79  
    80  func (r *mockIndexReporter) HighestIndexedHeight() (uint64, error) {
    81  	return math.MaxUint64, nil
    82  }