From 21837618bec6a43a9e22c13a8b5f6aa551af81e8 Mon Sep 17 00:00:00 2001 From: Makis Christou Date: Thu, 29 Feb 2024 12:20:40 +0200 Subject: [PATCH] Increase coverage (#669) --- kv/bucket_test.go | 193 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/kv/bucket_test.go b/kv/bucket_test.go index c5c86477a..a18b0050d 100644 --- a/kv/bucket_test.go +++ b/kv/bucket_test.go @@ -6,9 +6,12 @@ package kv import ( + "context" "errors" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) type mem map[string]string @@ -85,3 +88,193 @@ func TestBucket_GetterHas(t *testing.T) { }) } } + +func TestNewPutter(t *testing.T) { + m := mem{"k1": "v1", "k2": "v2"} + + tests := []struct { + b Bucket + key string + want string + }{ + {Bucket(""), "k1", "v1"}, + {Bucket(""), "k2", "v2"}, + {Bucket("k"), "k1", ""}, + {Bucket("k"), "1", "v1"}, + {Bucket("k"), "2", "v2"}, + {Bucket("k1"), "", "v1"}, + } + for _, tt := range tests { + t.Run("", func(t *testing.T) { + if err := tt.b.NewPutter(m).Put([]byte(tt.key), []byte(tt.want)); err != nil { + t.Errorf("Bucket.NewPutter.Put failed") + } + }) + } + + // test delete too + err := tests[0].b.NewPutter(m).Delete([]byte("k1")) + assert.Nil(t, err) +} + +// Mock DummyStore Implementations +type DummyStore struct { + data map[string][]byte +} + +func NewDummyStore() *DummyStore { + return &DummyStore{ + data: make(map[string][]byte), + } +} + +func (s *DummyStore) Get(key []byte) ([]byte, error) { + val, exists := s.data[string(key)] + if !exists { + return nil, errors.New("key not found") + } + + return val, nil +} + +func (s *DummyStore) Has(key []byte) (bool, error) { + _, exists := s.data[string(key)] + return exists, nil +} + +func (s *DummyStore) IsNotFound(err error) bool { + return err.Error() == "key not found" +} + +func (s *DummyStore) Put(key, val []byte) error { + s.data[string(key)] = val + return nil +} + +func (s *DummyStore) Delete(key []byte) error { + delete(s.data, string(key)) + return nil +} + +func (s *DummyStore) DeleteRange(ctx context.Context, r Range) error { + for k := range s.data { + if k >= string(r.Start) && k < string(r.Limit) { + delete(s.data, k) + } + } + return nil +} + +func (s *DummyStore) Iterate(r Range) Iterator { + return &DummyIterator{} +} + +func (s *DummyStore) Bulk() Bulk { + return &DummyBulk{} +} + +func (s *DummyStore) Snapshot() Snapshot { + return &DummySnapshot{} +} + +// Dummy Bulk Implementation +type DummyBulk struct{} + +func (db *DummyBulk) Put(key, val []byte) error { + return nil +} + +func (db *DummyBulk) Delete(key []byte) error { + return nil +} + +func (db *DummyBulk) EnableAutoFlush() { +} + +func (db *DummyBulk) Write() error { + return nil +} + +// Dummy Iterator Implementation +type DummyIterator struct{} + +func (di *DummyIterator) First() bool { + return true +} + +func (di *DummyIterator) Last() bool { + return true +} + +func (di *DummyIterator) Next() bool { + return false +} + +func (di *DummyIterator) Prev() bool { + return false +} + +func (di *DummyIterator) Key() []byte { + return []byte{} +} + +func (di *DummyIterator) Value() []byte { + return []byte{} +} + +func (di *DummyIterator) Release() { +} + +func (di *DummyIterator) Error() error { + return nil +} + +// Dummy Snapshot implementation +type DummySnapshot struct{} + +func (ds *DummySnapshot) Get(key []byte) ([]byte, error) { + return nil, errors.New("key not found") +} + +func (ds *DummySnapshot) Has(key []byte) (bool, error) { + return false, nil +} + +func (ds *DummySnapshot) IsNotFound(err error) bool { + return err.Error() == "key not found" +} + +func (ds *DummySnapshot) Release() { +} + +func TestNewStore(t *testing.T) { + bucket := Bucket("") + + DummyStore := NewDummyStore() + DummyStore.Put([]byte("k1"), []byte("v1")) + DummyStore.Put([]byte("k2"), []byte("v2")) + + store := bucket.NewStore(DummyStore) + + store.Bulk() + + myRange := Range{ + Start: []byte("k1"), + Limit: []byte("k1"), + } + + interateRes := store.Iterate(myRange) + assert.NotNil(t, interateRes) + + snapshotRes := store.Snapshot() + assert.NotNil(t, snapshotRes) + + isNotFoundRes := store.IsNotFound(errors.New("key not found")) + assert.NotNil(t, isNotFoundRes) + + err := store.Bulk().Write() + assert.Nil(t, err) + + err = store.DeleteRange(context.Background(), myRange) + assert.Nil(t, err) +}