github.com/koko1123/flow-go-1@v0.29.6/storage/badger/transactions_test.go (about)

     1  package badger_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/dgraph-io/badger/v3"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/koko1123/flow-go-1/module/metrics"
    12  	"github.com/koko1123/flow-go-1/storage"
    13  	"github.com/koko1123/flow-go-1/utils/unittest"
    14  
    15  	badgerstorage "github.com/koko1123/flow-go-1/storage/badger"
    16  )
    17  
    18  func TestTransactionStoreRetrieve(t *testing.T) {
    19  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    20  		metrics := metrics.NewNoopCollector()
    21  		store := badgerstorage.NewTransactions(metrics, db)
    22  
    23  		// store a transaction in db
    24  		expected := unittest.TransactionFixture()
    25  		err := store.Store(&expected.TransactionBody)
    26  		require.NoError(t, err)
    27  
    28  		// retrieve the transaction by ID
    29  		actual, err := store.ByID(expected.ID())
    30  		require.NoError(t, err)
    31  		assert.Equal(t, &expected.TransactionBody, actual)
    32  
    33  		// re-insert the transaction - should be idempotent
    34  		err = store.Store(&expected.TransactionBody)
    35  		require.NoError(t, err)
    36  	})
    37  }
    38  
    39  func TestTransactionRetrieveWithoutStore(t *testing.T) {
    40  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    41  		metrics := metrics.NewNoopCollector()
    42  		store := badgerstorage.NewTransactions(metrics, db)
    43  
    44  		// attempt to get a invalid transaction
    45  		_, err := store.ByID(unittest.IdentifierFixture())
    46  		assert.True(t, errors.Is(err, storage.ErrNotFound))
    47  	})
    48  }