Skip to content

Commit

Permalink
Increase coverage (vechain#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
MakisChristou authored Feb 29, 2024
1 parent 45d9e89 commit 2183761
Showing 1 changed file with 193 additions and 0 deletions.
193 changes: 193 additions & 0 deletions kv/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package kv

import (
"context"
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

type mem map[string]string
Expand Down Expand Up @@ -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)
}

0 comments on commit 2183761

Please sign in to comment.