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

     1  package operation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  	"github.com/koko1123/flow-go-1/storage"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  // Test_PersistBlocklist tests the operations:
    15  //   - PersistBlocklist(blocklist map[flow.Identifier]struct{})
    16  //   - RetrieveBlocklist(blocklist *map[flow.Identifier]struct{})
    17  //   - PurgeBlocklist()
    18  func Test_PersistBlocklist(t *testing.T) {
    19  	t.Run("Retrieving non-existing blocklist should return 'storage.ErrNotFound'", func(t *testing.T) {
    20  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    21  			var blocklist map[flow.Identifier]struct{}
    22  			err := db.View(RetrieveBlocklist(&blocklist))
    23  			require.ErrorIs(t, err, storage.ErrNotFound)
    24  
    25  		})
    26  	})
    27  
    28  	t.Run("Persisting and read blocklist", func(t *testing.T) {
    29  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    30  			blocklist := unittest.IdentifierListFixture(8).Lookup()
    31  			err := db.Update(PersistBlocklist(blocklist))
    32  			require.NoError(t, err)
    33  
    34  			var b map[flow.Identifier]struct{}
    35  			err = db.View(RetrieveBlocklist(&b))
    36  			require.NoError(t, err)
    37  			require.Equal(t, blocklist, b)
    38  		})
    39  	})
    40  
    41  	t.Run("Overwrite blocklist", func(t *testing.T) {
    42  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    43  			blocklist1 := unittest.IdentifierListFixture(8).Lookup()
    44  			err := db.Update(PersistBlocklist(blocklist1))
    45  			require.NoError(t, err)
    46  
    47  			blocklist2 := unittest.IdentifierListFixture(8).Lookup()
    48  			err = db.Update(PersistBlocklist(blocklist2))
    49  			require.NoError(t, err)
    50  
    51  			var b map[flow.Identifier]struct{}
    52  			err = db.View(RetrieveBlocklist(&b))
    53  			require.NoError(t, err)
    54  			require.Equal(t, blocklist2, b)
    55  		})
    56  	})
    57  
    58  	t.Run("Write & Purge & Write blocklist", func(t *testing.T) {
    59  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    60  			blocklist1 := unittest.IdentifierListFixture(8).Lookup()
    61  			err := db.Update(PersistBlocklist(blocklist1))
    62  			require.NoError(t, err)
    63  
    64  			err = db.Update(PurgeBlocklist())
    65  			require.NoError(t, err)
    66  
    67  			var b map[flow.Identifier]struct{}
    68  			err = db.View(RetrieveBlocklist(&b))
    69  			require.ErrorIs(t, err, storage.ErrNotFound)
    70  
    71  			blocklist2 := unittest.IdentifierListFixture(8).Lookup()
    72  			err = db.Update(PersistBlocklist(blocklist2))
    73  			require.NoError(t, err)
    74  
    75  			err = db.View(RetrieveBlocklist(&b))
    76  			require.NoError(t, err)
    77  			require.Equal(t, blocklist2, b)
    78  		})
    79  	})
    80  
    81  	t.Run("Purge non-existing blocklist", func(t *testing.T) {
    82  		unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    83  			var b map[flow.Identifier]struct{}
    84  
    85  			err := db.View(RetrieveBlocklist(&b))
    86  			require.ErrorIs(t, err, storage.ErrNotFound)
    87  
    88  			err = db.Update(PurgeBlocklist())
    89  			require.NoError(t, err)
    90  
    91  			err = db.View(RetrieveBlocklist(&b))
    92  			require.ErrorIs(t, err, storage.ErrNotFound)
    93  		})
    94  	})
    95  }