github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/collections_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/v3" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/koko1123/flow-go-1/model/flow" 13 "github.com/koko1123/flow-go-1/utils/unittest" 14 ) 15 16 func TestCollections(t *testing.T) { 17 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 18 expected := unittest.CollectionFixture(2).Light() 19 20 t.Run("Retrieve nonexistant", func(t *testing.T) { 21 var actual flow.LightCollection 22 err := db.View(RetrieveCollection(expected.ID(), &actual)) 23 assert.Error(t, err) 24 }) 25 26 t.Run("Save", func(t *testing.T) { 27 err := db.Update(InsertCollection(&expected)) 28 require.NoError(t, err) 29 30 var actual flow.LightCollection 31 err = db.View(RetrieveCollection(expected.ID(), &actual)) 32 assert.NoError(t, err) 33 34 assert.Equal(t, expected, actual) 35 }) 36 37 t.Run("Remove", func(t *testing.T) { 38 err := db.Update(RemoveCollection(expected.ID())) 39 require.NoError(t, err) 40 41 var actual flow.LightCollection 42 err = db.View(RetrieveCollection(expected.ID(), &actual)) 43 assert.Error(t, err) 44 }) 45 46 t.Run("Index and lookup", func(t *testing.T) { 47 expected := unittest.CollectionFixture(1).Light() 48 blockID := unittest.IdentifierFixture() 49 50 _ = db.Update(func(tx *badger.Txn) error { 51 err := InsertCollection(&expected)(tx) 52 assert.Nil(t, err) 53 err = IndexCollectionPayload(blockID, expected.Transactions)(tx) 54 assert.Nil(t, err) 55 return nil 56 }) 57 58 var actual flow.LightCollection 59 err := db.View(LookupCollectionPayload(blockID, &actual.Transactions)) 60 assert.Nil(t, err) 61 62 assert.Equal(t, expected, actual) 63 }) 64 65 t.Run("Index and lookup by transaction ID", func(t *testing.T) { 66 expected := unittest.IdentifierFixture() 67 transactionID := unittest.IdentifierFixture() 68 actual := flow.Identifier{} 69 70 _ = db.Update(func(tx *badger.Txn) error { 71 err := IndexCollectionByTransaction(transactionID, expected)(tx) 72 assert.Nil(t, err) 73 err = RetrieveCollectionID(transactionID, &actual)(tx) 74 assert.Nil(t, err) 75 return nil 76 }) 77 assert.Equal(t, expected, actual) 78 }) 79 }) 80 }