github.com/onflow/flow-go@v0.33.17/storage/badger/operation/interactions_test.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package operation
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/dgraph-io/badger/v2"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/onflow/flow-go/fvm/storage/snapshot"
    13  	"github.com/onflow/flow-go/model/flow"
    14  	"github.com/onflow/flow-go/utils/unittest"
    15  )
    16  
    17  func TestStateInteractionsInsertCheckRetrieve(t *testing.T) {
    18  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    19  
    20  		id1 := flow.NewRegisterID(
    21  			flow.BytesToAddress([]byte("\x89krg\u007fBN\x1d\xf5\xfb\xb8r\xbc4\xbd\x98ռ\xf1\xd0twU\xbf\x16N\xb4?,\xa0&;")),
    22  			"")
    23  		id2 := flow.NewRegisterID(flow.BytesToAddress([]byte{2}), "")
    24  		id3 := flow.NewRegisterID(flow.BytesToAddress([]byte{3}), "")
    25  
    26  		executionSnapshot := &snapshot.ExecutionSnapshot{
    27  			ReadSet: map[flow.RegisterID]struct{}{
    28  				id2: {},
    29  				id3: {},
    30  			},
    31  			WriteSet: map[flow.RegisterID]flow.RegisterValue{
    32  				id1: []byte("zażółć gęślą jaźń"),
    33  				id2: []byte("c"),
    34  			},
    35  		}
    36  
    37  		interactions := []*snapshot.ExecutionSnapshot{
    38  			executionSnapshot,
    39  			{},
    40  		}
    41  
    42  		blockID := unittest.IdentifierFixture()
    43  
    44  		err := db.Update(InsertExecutionStateInteractions(blockID, interactions))
    45  		require.Nil(t, err)
    46  
    47  		var readInteractions []*snapshot.ExecutionSnapshot
    48  
    49  		err = db.View(RetrieveExecutionStateInteractions(blockID, &readInteractions))
    50  		require.NoError(t, err)
    51  
    52  		assert.Equal(t, interactions, readInteractions)
    53  		assert.Equal(
    54  			t,
    55  			executionSnapshot.WriteSet,
    56  			readInteractions[0].WriteSet)
    57  		assert.Equal(
    58  			t,
    59  			executionSnapshot.ReadSet,
    60  			readInteractions[0].ReadSet)
    61  	})
    62  }