From 2cabd5b274e1f775100038c4c6140fd269042b36 Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Sat, 14 Sep 2019 10:29:08 +0300 Subject: [PATCH] storage: add boltDB unit tests --- pkg/core/storage/boltdb_store_test.go | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 pkg/core/storage/boltdb_store_test.go diff --git a/pkg/core/storage/boltdb_store_test.go b/pkg/core/storage/boltdb_store_test.go new file mode 100644 index 000000000..73dc4097d --- /dev/null +++ b/pkg/core/storage/boltdb_store_test.go @@ -0,0 +1,89 @@ +package storage + +import ( + "context" + "io/ioutil" + "os" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBoltDBBatch(t *testing.T) { + boltDB := BoltDBStore{} + want := &BoltDBBatch{mem: map[*[]byte][]byte{}} + if got := boltDB.Batch(); !reflect.DeepEqual(got, want) { + t.Errorf("BoltDB Batch() = %v, want %v", got, want) + } +} + +func TestBoltDBBatch_Len(t *testing.T) { + batch := &BoltDBBatch{mem: map[*[]byte][]byte{}} + want := len(map[*[]byte][]byte{}) + assert.Equal(t, want, batch.Len()) +} + +func TestBoltDBBatch_PutBatchAndGet(t *testing.T) { + key := []byte("foo") + value := []byte("bar") + batch := &BoltDBBatch{mem: map[*[]byte][]byte{&key: value}} + + boltDBStore := openStore(t) + + errPut := boltDBStore.PutBatch(batch) + assert.Nil(t, errPut, "Error while PutBatch") + + result, err := boltDBStore.Get(key) + assert.Nil(t, err) + assert.Equal(t, value, result) + + require.NoError(t, boltDBStore.Close()) +} + +func TestBoltDBBatch_PutAndGet(t *testing.T) { + key := []byte("foo") + value := []byte("bar") + + boltDBStore := openStore(t) + + errPut := boltDBStore.Put(key, value) + assert.Nil(t, errPut, "Error while Put") + + result, err := boltDBStore.Get(key) + assert.Nil(t, err) + assert.Equal(t, value, result) + + require.NoError(t, boltDBStore.Close()) +} + +func TestBoltDBStore_Seek(t *testing.T) { + key := []byte("foo") + value := []byte("bar") + + boltDBStore := openStore(t) + + errPut := boltDBStore.Put(key, value) + assert.Nil(t, errPut, "Error while Put") + + boltDBStore.Seek(key, func(k, v []byte) { + assert.Equal(t, value, v) + }) + + require.NoError(t, boltDBStore.Close()) +} + +func openStore(t *testing.T) *BoltDBStore { + testFileName := "test_bolt_db" + file, err := ioutil.TempFile("", testFileName) + defer func() { + err := os.RemoveAll(testFileName) + require.NoError(t, err) + }() + require.NoError(t, err) + require.NoError(t, file.Close()) + boltDBStore, err := NewBoltDBStore(context.Background(), BoltDBOptions{FilePath: testFileName}) + require.NoError(t, err) + return boltDBStore +}