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

     1  package badger_test
     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/module/metrics"
    11  	bstorage "github.com/onflow/flow-go/storage/badger"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func TestCollections(t *testing.T) {
    16  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  
    18  		metrics := metrics.NewNoopCollector()
    19  		transactions := bstorage.NewTransactions(metrics, db)
    20  		collections := bstorage.NewCollections(db, transactions)
    21  
    22  		// create a light collection with three transactions
    23  		expected := unittest.CollectionFixture(3).Light()
    24  
    25  		// store the light collection and the transaction index
    26  		err := collections.StoreLightAndIndexByTransaction(&expected)
    27  		require.Nil(t, err)
    28  
    29  		// retrieve the light collection by collection id
    30  		actual, err := collections.LightByID(expected.ID())
    31  		require.Nil(t, err)
    32  
    33  		// check if the light collection was indeed persisted
    34  		assert.Equal(t, &expected, actual)
    35  
    36  		expectedID := expected.ID()
    37  
    38  		// retrieve the collection light id by each of its transaction id
    39  		for _, txID := range expected.Transactions {
    40  			collLight, err := collections.LightByTransactionID(txID)
    41  			actualID := collLight.ID()
    42  			// check that the collection id can indeed be retrieved by transaction id
    43  			require.Nil(t, err)
    44  			assert.Equal(t, expectedID, actualID)
    45  		}
    46  
    47  	})
    48  }
    49  
    50  func TestCollections_IndexDuplicateTx(t *testing.T) {
    51  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    52  		metrics := metrics.NewNoopCollector()
    53  		transactions := bstorage.NewTransactions(metrics, db)
    54  		collections := bstorage.NewCollections(db, transactions)
    55  
    56  		// create two collections which share 1 transaction
    57  		col1 := unittest.CollectionFixture(2)
    58  		col2 := unittest.CollectionFixture(1)
    59  		dupTx := col1.Transactions[0]  // the duplicated transaction
    60  		col2Tx := col2.Transactions[0] // transaction that's only in col2
    61  		col2.Transactions = append(col2.Transactions, dupTx)
    62  
    63  		// insert col1
    64  		col1Light := col1.Light()
    65  		err := collections.StoreLightAndIndexByTransaction(&col1Light)
    66  		require.NoError(t, err)
    67  
    68  		// insert col2
    69  		col2Light := col2.Light()
    70  		err = collections.StoreLightAndIndexByTransaction(&col2Light)
    71  		require.NoError(t, err)
    72  
    73  		// should be able to retrieve col2 by ID
    74  		gotLightByCol2ID, err := collections.LightByID(col2.ID())
    75  		require.NoError(t, err)
    76  		assert.Equal(t, &col2Light, gotLightByCol2ID)
    77  
    78  		// should be able to retrieve col2 by the transaction which only appears in col2
    79  		_, err = collections.LightByTransactionID(col2Tx.ID())
    80  		require.NoError(t, err)
    81  
    82  		// col1 (not col2) should be indexed by the shared transaction (since col1 was inserted first)
    83  		gotLightByDupTxID, err := collections.LightByTransactionID(dupTx.ID())
    84  		require.NoError(t, err)
    85  		assert.Equal(t, &col1Light, gotLightByDupTxID)
    86  	})
    87  }