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

     1  package badger
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/module/metrics"
    10  	"github.com/onflow/flow-go/utils/unittest"
    11  )
    12  
    13  // TestCache_Exists tests existence checking items in the cache.
    14  func TestCache_Exists(t *testing.T) {
    15  	cache := newCache[flow.Identifier, any](metrics.NewNoopCollector(), "test")
    16  
    17  	t.Run("non-existent", func(t *testing.T) {
    18  		key := unittest.IdentifierFixture()
    19  		exists := cache.IsCached(key)
    20  		assert.False(t, exists)
    21  	})
    22  
    23  	t.Run("existent", func(t *testing.T) {
    24  		key := unittest.IdentifierFixture()
    25  		cache.Insert(key, unittest.RandomBytes(128))
    26  
    27  		exists := cache.IsCached(key)
    28  		assert.True(t, exists)
    29  	})
    30  
    31  	t.Run("removed", func(t *testing.T) {
    32  		key := unittest.IdentifierFixture()
    33  		// insert, then remove the item
    34  		cache.Insert(key, unittest.RandomBytes(128))
    35  		cache.Remove(key)
    36  
    37  		exists := cache.IsCached(key)
    38  		assert.False(t, exists)
    39  	})
    40  }