github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/emulator/state/collection_test.go (about)

     1  package state_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/onflow/atree"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/fvm/evm/emulator/state"
    10  	"github.com/onflow/flow-go/fvm/evm/testutils"
    11  )
    12  
    13  func TestCollection(t *testing.T) {
    14  
    15  	cp := setupTestCollection(t)
    16  	c1, err := cp.NewCollection()
    17  	require.NoError(t, err)
    18  
    19  	key1 := []byte("A")
    20  	key2 := []byte("B")
    21  	value1 := []byte{1}
    22  	value2 := []byte{2}
    23  
    24  	// get value for A
    25  	ret, err := c1.Get(key1)
    26  	require.NoError(t, err)
    27  	require.Empty(t, ret)
    28  
    29  	// set value1 for A
    30  	err = c1.Set(key1, value1)
    31  	require.NoError(t, err)
    32  
    33  	ret, err = c1.Get(key1)
    34  	require.NoError(t, err)
    35  	require.Equal(t, ret, value1)
    36  
    37  	err = c1.Remove(key1)
    38  	require.NoError(t, err)
    39  
    40  	ret, err = c1.Get(key1)
    41  	require.NoError(t, err)
    42  	require.Empty(t, ret)
    43  
    44  	err = c1.Set(key2, value2)
    45  	require.NoError(t, err)
    46  
    47  	c2, err := cp.CollectionByID(c1.CollectionID())
    48  	require.NoError(t, err)
    49  
    50  	ret, err = c2.Get(key2)
    51  	require.NoError(t, err)
    52  	require.Equal(t, value2, ret)
    53  
    54  	// destroy
    55  	keys, err := c1.Destroy()
    56  	require.NoError(t, err)
    57  	require.Len(t, keys, 1)
    58  	require.Equal(t, key2, keys[0])
    59  
    60  	_, err = cp.CollectionByID(c1.CollectionID())
    61  	require.Error(t, err)
    62  }
    63  
    64  func setupTestCollection(t *testing.T) *state.CollectionProvider {
    65  	ledger := testutils.GetSimpleValueStore()
    66  	cp, err := state.NewCollectionProvider(atree.Address{1, 2, 3, 4, 5, 6, 7, 8}, ledger)
    67  	require.NoError(t, err)
    68  	return cp
    69  }