forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase coverage across the store package (cosmos#5727)
- Loading branch information
Alessio Treglia
authored
Feb 28, 2020
1 parent
7d6fc7e
commit 5f14dc2
Showing
6 changed files
with
380 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.