-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix DiffKVStores(), store/types gets 100% coverage
DiffKVStores() used to return duplicated entries in some cases. Add test cases, aiming to reach 100% coverage for store package. Remove duplicate Cp function from the store package. Same functionality is provided by types.CopyBytes().
- Loading branch information
Alessio Treglia
committed
Feb 29, 2020
1 parent
5f14dc2
commit 208ec50
Showing
12 changed files
with
248 additions
and
35 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
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,37 @@ | ||
package iavl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/iavl" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func TestImmutableTreePanics(t *testing.T) { | ||
t.Parallel() | ||
immTree := iavl.NewImmutableTree(dbm.NewMemDB(), 100) | ||
it := &immutableTree{immTree} | ||
require.Panics(t, func() { it.Set([]byte{}, []byte{}) }) | ||
require.Panics(t, func() { it.Remove([]byte{}) }) | ||
require.Panics(t, func() { it.SaveVersion() }) | ||
require.Panics(t, func() { it.DeleteVersion(int64(1)) }) | ||
v, _ := it.GetVersioned([]byte{0x01}, 1) | ||
require.Equal(t, int64(-1), v) | ||
v, _ = it.GetVersioned([]byte{0x01}, 0) | ||
require.Equal(t, int64(0), v) | ||
|
||
val, proof, err := it.GetVersionedWithProof(nil, 1) | ||
require.Error(t, err) | ||
require.Nil(t, val) | ||
require.Nil(t, proof) | ||
|
||
imm, err := it.GetImmutable(1) | ||
require.Error(t, err) | ||
require.Nil(t, imm) | ||
|
||
imm, err = it.GetImmutable(0) | ||
require.NoError(t, err) | ||
require.NotNil(t, imm) | ||
require.Equal(t, immTree, imm) | ||
} |
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
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,88 @@ | ||
package types_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
func initTestStores(t *testing.T) (types.KVStore, types.KVStore) { | ||
db := dbm.NewMemDB() | ||
ms := rootmulti.NewStore(db) | ||
|
||
key1 := types.NewKVStoreKey("store1") | ||
key2 := types.NewKVStoreKey("store2") | ||
require.NotPanics(t, func() { ms.MountStoreWithDB(key1, types.StoreTypeIAVL, db) }) | ||
require.NotPanics(t, func() { ms.MountStoreWithDB(key2, types.StoreTypeIAVL, db) }) | ||
require.NoError(t, ms.LoadLatestVersion()) | ||
return ms.GetKVStore(key1), ms.GetKVStore(key2) | ||
} | ||
|
||
func TestDiffKVStores(t *testing.T) { | ||
t.Parallel() | ||
store1, store2 := initTestStores(t) | ||
// Two equal stores | ||
k1, v1 := []byte("k1"), []byte("v1") | ||
store1.Set(k1, v1) | ||
store2.Set(k1, v1) | ||
|
||
kvAs, kvBs := types.DiffKVStores(store1, store2, nil) | ||
require.Equal(t, 0, len(kvAs)) | ||
require.Equal(t, len(kvAs), len(kvBs)) | ||
|
||
// delete k1 from store2, which is now empty | ||
store2.Delete(k1) | ||
kvAs, kvBs = types.DiffKVStores(store1, store2, nil) | ||
require.Equal(t, 1, len(kvAs)) | ||
require.Equal(t, len(kvAs), len(kvBs)) | ||
|
||
// set k1 in store2, different value than what store1 holds for k1 | ||
v2 := []byte("v2") | ||
store2.Set(k1, v2) | ||
kvAs, kvBs = types.DiffKVStores(store1, store2, nil) | ||
require.Equal(t, 1, len(kvAs)) | ||
require.Equal(t, len(kvAs), len(kvBs)) | ||
|
||
// add k2 to store2 | ||
k2 := []byte("k2") | ||
store2.Set(k2, v2) | ||
kvAs, kvBs = types.DiffKVStores(store1, store2, nil) | ||
require.Equal(t, 2, len(kvAs)) | ||
require.Equal(t, len(kvAs), len(kvBs)) | ||
|
||
// Reset stores | ||
store1.Delete(k1) | ||
store2.Delete(k1) | ||
store2.Delete(k2) | ||
|
||
// Same keys, different value. Comparisons will be nil as prefixes are skipped. | ||
prefix := []byte("prefix:") | ||
k1Prefixed := append(prefix, k1...) | ||
store1.Set(k1Prefixed, v1) | ||
store2.Set(k1Prefixed, v2) | ||
kvAs, kvBs = types.DiffKVStores(store1, store2, [][]byte{prefix}) | ||
require.Equal(t, 0, len(kvAs)) | ||
require.Equal(t, len(kvAs), len(kvBs)) | ||
} | ||
|
||
func TestPrefixEndBytes(t *testing.T) { | ||
t.Parallel() | ||
bs1 := []byte{0x23, 0xA5, 0x06} | ||
require.True(t, bytes.Equal([]byte{0x23, 0xA5, 0x07}, types.PrefixEndBytes(bs1))) | ||
bs2 := []byte{0x23, 0xA5, 0xFF} | ||
require.True(t, bytes.Equal([]byte{0x23, 0xA6}, types.PrefixEndBytes(bs2))) | ||
require.Nil(t, types.PrefixEndBytes([]byte{0xFF})) | ||
require.Nil(t, types.PrefixEndBytes(nil)) | ||
} | ||
|
||
func TestInclusiveEndBytes(t *testing.T) { | ||
t.Parallel() | ||
require.True(t, bytes.Equal([]byte{0x00}, types.InclusiveEndBytes(nil))) | ||
bs := []byte("test") | ||
require.True(t, bytes.Equal(append(bs, byte(0x00)), types.InclusiveEndBytes(bs))) | ||
} |
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,23 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
func TestAssertValidKey(t *testing.T) { | ||
t.Parallel() | ||
require.NotPanics(t, func() { types.AssertValidKey([]byte{}) }) | ||
require.NotPanics(t, func() { types.AssertValidKey([]byte{0x01}) }) | ||
require.Panics(t, func() { types.AssertValidKey(nil) }) | ||
} | ||
|
||
func TestAssertValidValue(t *testing.T) { | ||
t.Parallel() | ||
require.NotPanics(t, func() { types.AssertValidValue([]byte{}) }) | ||
require.NotPanics(t, func() { types.AssertValidValue([]byte{0x01}) }) | ||
require.Panics(t, func() { types.AssertValidValue(nil) }) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.