github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/interactions_test.go (about)

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