2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-09-05 17:10:35 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testsuite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2023-04-06 14:54:41 +01:00
|
|
|
"storj.io/storj/private/kvstore"
|
2018-09-05 17:10:35 +01:00
|
|
|
)
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
func testCRUD(t *testing.T, ctx *testcontext.Context, store kvstore.Store) {
|
|
|
|
items := kvstore.Items{
|
2020-10-13 13:47:55 +01:00
|
|
|
// newItem("0", "", false), // TODO: broken
|
2018-09-05 17:10:35 +01:00
|
|
|
newItem("\x00", "\x00", false),
|
|
|
|
newItem("a/b", "\x01\x00", false),
|
|
|
|
newItem("a\\b", "\xFF", false),
|
|
|
|
newItem("full/path/1", "\x00\xFF\xFF\x00", false),
|
|
|
|
newItem("full/path/2", "\x00\xFF\xFF\x01", false),
|
|
|
|
newItem("full/path/3", "\x00\xFF\xFF\x02", false),
|
|
|
|
newItem("full/path/4", "\x00\xFF\xFF\x03", false),
|
|
|
|
newItem("full/path/5", "\x00\xFF\xFF\x04", false),
|
|
|
|
newItem("öö", "üü", false),
|
|
|
|
}
|
|
|
|
rand.Shuffle(len(items), items.Swap)
|
2019-09-24 19:06:22 +01:00
|
|
|
defer cleanupItems(t, ctx, store, items)
|
2018-09-05 17:10:35 +01:00
|
|
|
|
|
|
|
t.Run("Put", func(t *testing.T) {
|
|
|
|
for _, item := range items {
|
2019-06-05 15:23:10 +01:00
|
|
|
err := store.Put(ctx, item.Key, item.Value)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to put %q = %v: %v", item.Key, item.Value, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
rand.Shuffle(len(items), items.Swap)
|
|
|
|
|
|
|
|
t.Run("Get", func(t *testing.T) {
|
|
|
|
for _, item := range items {
|
2019-06-05 15:23:10 +01:00
|
|
|
value, err := store.Get(ctx, item.Key)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get %q = %v: %v", item.Key, item.Value, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal([]byte(value), []byte(item.Value)) {
|
|
|
|
t.Fatalf("invalid value for %q = %v: got %v", item.Key, item.Value, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
2023-04-06 12:44:23 +01:00
|
|
|
for _, item := range items {
|
|
|
|
_, err := store.Get(ctx, item.Key)
|
2020-01-28 20:52:04 +00:00
|
|
|
if err != nil {
|
2023-04-06 12:44:23 +01:00
|
|
|
t.Fatalf("failed to get %v", item.Key)
|
2020-01-28 20:52:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// individual deletes
|
2023-04-06 12:44:23 +01:00
|
|
|
for _, item := range items {
|
2019-06-05 15:23:10 +01:00
|
|
|
err := store.Delete(ctx, item.Key)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to delete %v: %v", item.Key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
2019-06-05 15:23:10 +01:00
|
|
|
value, err := store.Get(ctx, item.Key)
|
2018-09-05 17:10:35 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("got deleted value %q = %v", item.Key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|