2018-05-11 01:29:42 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-05-15 18:48:13 +00:00
|
|
|
// Verifies that ShardKV implements the ShardBackend interface.
|
2018-05-25 00:03:24 +00:00
|
|
|
var _ = ShardBackend(&ShardKV{})
|
2018-05-15 18:48:13 +00:00
|
|
|
|
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
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
if err := kv.Put([]byte("ralph merkle"), []byte{1, 2, 3}); err != nil {
|
2018-05-11 15:43:24 +00:00
|
|
|
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-25 00:03:24 +00:00
|
|
|
key := []byte("ralph merkle")
|
2018-05-11 15:43:24 +00:00
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
if err := kv.Put(key, []byte{1, 2, 3}); err != nil {
|
2018-05-11 15:43:24 +00:00
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
has, err := kv.Has(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not check if kv store has key: %v", err)
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
t.Errorf("kv store should have key: %v", key)
|
2018-05-11 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
key2 := []byte{}
|
|
|
|
has2, err := kv.Has(key2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("could not check if kv store has key: %v", err)
|
|
|
|
}
|
|
|
|
if has2 {
|
|
|
|
t.Errorf("kv store should not have non-existent key: %v", key2)
|
2018-05-11 15:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 00:03:24 +00:00
|
|
|
key := []byte("ralph merkle")
|
2018-05-11 15:43:24 +00:00
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
if err := kv.Put(key, []byte{1, 2, 3}); err != nil {
|
2018-05-11 15:43:24 +00:00
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
2018-05-11 01:29:42 +00:00
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
val, err := kv.Get(key)
|
2018-05-11 01:29:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("get failed: %v", err)
|
|
|
|
}
|
2018-05-25 00:03:24 +00:00
|
|
|
if len(val) == 0 {
|
2018-05-11 01:29:42 +00:00
|
|
|
t.Errorf("no value stored for key")
|
|
|
|
}
|
2018-05-11 02:00:54 +00:00
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
key2 := []byte{}
|
|
|
|
val2, err := kv.Get(key2)
|
|
|
|
if len(val2) != 0 {
|
|
|
|
t.Errorf("non-existent key should not have a value. key=%v, value=%v", key2, val2)
|
2018-05-11 02:00:54 +00:00
|
|
|
}
|
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-25 00:03:24 +00:00
|
|
|
key := []byte("ralph merkle")
|
2018-05-11 15:43:24 +00:00
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
if err := kv.Put(key, []byte{1, 2, 3}); err != nil {
|
2018-05-11 15:43:24 +00:00
|
|
|
t.Fatalf("could not save value in kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-25 00:03:24 +00:00
|
|
|
if err := kv.Delete(key); err != nil {
|
|
|
|
t.Errorf("could not delete key: %v", key)
|
2018-05-11 15:43:24 +00:00
|
|
|
}
|
|
|
|
}
|