2018-05-11 01:29:42 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-05-15 18:48:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding"
|
2018-05-11 01:29:42 +00:00
|
|
|
)
|
|
|
|
|
2018-05-15 18:48:13 +00:00
|
|
|
// Verifies that ShardKV implements the ShardBackend interface.
|
|
|
|
var _ = sharding.ShardBackend(&shardKV{})
|
|
|
|
|
2018-05-11 15:43:24 +00:00
|
|
|
func Test_ShardKVPut(t *testing.T) {
|
2018-05-14 13:53:20 +00:00
|
|
|
kv := NewShardKV()
|
2018-05-11 15:43:24 +00:00
|
|
|
hash := common.StringToHash("ralph merkle")
|
|
|
|
|
|
|
|
if err := kv.Put(hash, []byte{1, 2, 3}); err != nil {
|
|
|
|
t.Errorf("could not save value in kv store: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ShardKVHas(t *testing.T) {
|
2018-05-14 13:53:20 +00:00
|
|
|
kv := NewShardKV()
|
2018-05-11 15:43:24 +00:00
|
|
|
hash := common.StringToHash("ralph merkle")
|
|
|
|
|
|
|
|
if err := kv.Put(hash, []byte{1, 2, 3}); err != nil {
|
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !kv.Has(hash) {
|
|
|
|
t.Errorf("kv store does not have hash: %v", hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash2 := common.StringToHash("")
|
|
|
|
if kv.Has(hash2) {
|
|
|
|
t.Errorf("kv store should not contain unset key: %v", hash2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:29:42 +00:00
|
|
|
func Test_ShardKVGet(t *testing.T) {
|
2018-05-14 13:53:20 +00:00
|
|
|
kv := NewShardKV()
|
2018-05-11 01:29:42 +00:00
|
|
|
hash := common.StringToHash("ralph merkle")
|
2018-05-11 15:43:24 +00:00
|
|
|
|
|
|
|
if err := kv.Put(hash, []byte{1, 2, 3}); err != nil {
|
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
2018-05-11 01:29:42 +00:00
|
|
|
|
|
|
|
val, err := kv.Get(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("get failed: %v", err)
|
|
|
|
}
|
|
|
|
if val == nil {
|
|
|
|
t.Errorf("no value stored for key")
|
|
|
|
}
|
2018-05-11 02:00:54 +00:00
|
|
|
|
|
|
|
hash2 := common.StringToHash("")
|
|
|
|
val2, err := kv.Get(hash2)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("non-existent key should not have a value. key=%v, value=%v", hash2, val2)
|
|
|
|
}
|
2018-05-11 01:29:42 +00:00
|
|
|
}
|
2018-05-11 15:43:24 +00:00
|
|
|
|
|
|
|
func Test_ShardKVDelete(t *testing.T) {
|
2018-05-14 13:53:20 +00:00
|
|
|
kv := NewShardKV()
|
2018-05-11 15:43:24 +00:00
|
|
|
hash := common.StringToHash("ralph merkle")
|
|
|
|
|
|
|
|
if err := kv.Put(hash, []byte{1, 2, 3}); err != nil {
|
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kv.Delete(hash); err != nil {
|
|
|
|
t.Errorf("could not delete key: %v", hash)
|
|
|
|
}
|
|
|
|
}
|