Skip to content

Commit

Permalink
Increase coverage across the store package (cosmos#5727)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia authored Feb 28, 2020
1 parent 7d6fc7e commit 5f14dc2
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build: go.sum

mocks: $(MOCKS_DIR)
mockgen -source=x/auth/types/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
mockgen -package mocks -destination tests/mocks/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
.PHONY: mocks

$(MOCKS_DIR):
Expand Down
70 changes: 70 additions & 0 deletions store/dbadapter/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dbadapter_test

import (
"bytes"
"errors"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/tests/mocks"
)

var errFoo = errors.New("dummy")

func TestAccessors(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockDB := mocks.NewMockDB(mockCtrl)
store := dbadapter.Store{mockDB}
key := []byte("test")
value := []byte("testvalue")

require.Equal(t, types.StoreTypeDB, store.GetStoreType())
store.GetStoreType()

retFoo := []byte("xxx")
mockDB.EXPECT().Get(gomock.Eq(key)).Times(1).Return(retFoo, nil)
require.True(t, bytes.Equal(retFoo, store.Get(key)))

mockDB.EXPECT().Get(gomock.Eq(key)).Times(1).Return(nil, errFoo)
require.Panics(t, func() { store.Get(key) })

mockDB.EXPECT().Has(gomock.Eq(key)).Times(1).Return(true, nil)
require.True(t, store.Has(key))

mockDB.EXPECT().Has(gomock.Eq(key)).Times(1).Return(false, nil)
require.False(t, store.Has(key))

mockDB.EXPECT().Has(gomock.Eq(key)).Times(1).Return(false, errFoo)
require.Panics(t, func() { store.Has(key) })

mockDB.EXPECT().Set(gomock.Eq(key), gomock.Eq(value)).Times(1).Return(nil)
require.NotPanics(t, func() { store.Set(key, value) })

mockDB.EXPECT().Set(gomock.Eq(key), gomock.Eq(value)).Times(1).Return(errFoo)
require.Panics(t, func() { store.Set(key, value) })

mockDB.EXPECT().Delete(gomock.Eq(key)).Times(1).Return(nil)
require.NotPanics(t, func() { store.Delete(key) })

mockDB.EXPECT().Delete(gomock.Eq(key)).Times(1).Return(errFoo)
require.Panics(t, func() { store.Delete(key) })

start, end := []byte("start"), []byte("end")
mockDB.EXPECT().Iterator(gomock.Eq(start), gomock.Eq(end)).Times(1).Return(nil, nil)
require.NotPanics(t, func() { store.Iterator(start, end) })

mockDB.EXPECT().Iterator(gomock.Eq(start), gomock.Eq(end)).Times(1).Return(nil, errFoo)
require.Panics(t, func() { store.Iterator(start, end) })

mockDB.EXPECT().ReverseIterator(gomock.Eq(start), gomock.Eq(end)).Times(1).Return(nil, nil)
require.NotPanics(t, func() { store.ReverseIterator(start, end) })

mockDB.EXPECT().ReverseIterator(gomock.Eq(start), gomock.Eq(end)).Times(1).Return(nil, errFoo)
require.Panics(t, func() { store.ReverseIterator(start, end) })
}
15 changes: 13 additions & 2 deletions store/transient/store_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package transient
package transient_test

import (
"bytes"
"testing"

"github.com/cosmos/cosmos-sdk/store/transient"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/stretchr/testify/require"
)

var k, v = []byte("hello"), []byte("world")

func TestTransientStore(t *testing.T) {
tstore := NewStore()
tstore := transient.NewStore()

require.Nil(t, tstore.Get(k))

Expand All @@ -20,4 +23,12 @@ func TestTransientStore(t *testing.T) {
tstore.Commit()

require.Nil(t, tstore.Get(k))

// no-op
tstore.SetPruning(types.PruningOptions{})

emptyCommitID := tstore.LastCommitID()
require.Equal(t, emptyCommitID.Version, int64(0))
require.True(t, bytes.Equal(emptyCommitID.Hash, nil))
require.Equal(t, types.StoreTypeTransient, tstore.GetStoreType())
}
79 changes: 79 additions & 0 deletions store/types/pruning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/store/types"
)

func TestPruningOptions_FlushVersion(t *testing.T) {
t.Parallel()
require.True(t, types.PruneEverything.FlushVersion(-1))
require.True(t, types.PruneEverything.FlushVersion(0))
require.True(t, types.PruneEverything.FlushVersion(1))
require.True(t, types.PruneEverything.FlushVersion(2))

require.True(t, types.PruneNothing.FlushVersion(-1))
require.True(t, types.PruneNothing.FlushVersion(0))
require.True(t, types.PruneNothing.FlushVersion(1))
require.True(t, types.PruneNothing.FlushVersion(2))

require.False(t, types.PruneSyncable.FlushVersion(-1))
require.True(t, types.PruneSyncable.FlushVersion(0))
require.False(t, types.PruneSyncable.FlushVersion(1))
require.True(t, types.PruneSyncable.FlushVersion(100))
require.False(t, types.PruneSyncable.FlushVersion(101))
}

func TestPruningOptions_SnapshotVersion(t *testing.T) {
t.Parallel()
require.False(t, types.PruneEverything.SnapshotVersion(-1))
require.False(t, types.PruneEverything.SnapshotVersion(0))
require.False(t, types.PruneEverything.SnapshotVersion(1))
require.False(t, types.PruneEverything.SnapshotVersion(2))

require.True(t, types.PruneNothing.SnapshotVersion(-1))
require.True(t, types.PruneNothing.SnapshotVersion(0))
require.True(t, types.PruneNothing.SnapshotVersion(1))
require.True(t, types.PruneNothing.SnapshotVersion(2))

require.False(t, types.PruneSyncable.SnapshotVersion(-1))
require.True(t, types.PruneSyncable.SnapshotVersion(0))
require.False(t, types.PruneSyncable.SnapshotVersion(1))
require.True(t, types.PruneSyncable.SnapshotVersion(10000))
require.False(t, types.PruneSyncable.SnapshotVersion(10001))
}

func TestPruningOptions_IsValid(t *testing.T) {
t.Parallel()
type fields struct {
KeepEvery int64
SnapshotEvery int64
}
tests := []struct {
name string
fields fields
want bool
}{
{"PruneEverything", fields{types.PruneEverything.KeepEvery, types.PruneEverything.SnapshotEvery}, true},
{"PruneNothing", fields{types.PruneNothing.KeepEvery, types.PruneNothing.SnapshotEvery}, true},
{"PruneSyncable", fields{types.PruneSyncable.KeepEvery, types.PruneSyncable.SnapshotEvery}, true},
{"KeepEvery=0", fields{0, 0}, false},
{"KeepEvery<0", fields{-1, 0}, false},
{"SnapshotEvery<0", fields{1, -1}, false},
{"SnapshotEvery%KeepEvery!=0", fields{15, 30}, true},
{"SnapshotEvery%KeepEvery!=0", fields{15, 20}, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
po := types.PruningOptions{
KeepEvery: tt.fields.KeepEvery,
SnapshotEvery: tt.fields.SnapshotEvery,
}
require.Equal(t, tt.want, po.IsValid(), "IsValid() = %v, want %v", po.IsValid(), tt.want)
})
}
}
13 changes: 11 additions & 2 deletions tests/mocks/account_retriever.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5f14dc2

Please sign in to comment.