storj/storage/testsuite/utils.go
Egon Elbre 83df0ee1b0
Implement ListV2 with storage rework (#303)
1. Added KeyValueStore.Iterate for implementing the different List, ListV2 etc. implementations. This allows for more efficient use of memory depending on the situation.
2. Implemented an inmemory teststore for running tests. This should allow to replace MockKeyValueStore in most places.
3. Rewrote tests
4. Pulled out logger from bolt implementation so it can be used for all other storage implementations.
5. Fixed multiple things in bolt and redis implementations.
2018-09-05 19:10:35 +03:00

61 lines
1.3 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package testsuite
import (
"testing"
"storj.io/storj/storage"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func newItem(key, value string, isPrefix bool) storage.ListItem {
return storage.ListItem{
Key: storage.Key(key),
Value: storage.Value(value),
IsPrefix: isPrefix,
}
}
func cleanupItems(store storage.KeyValueStore, items storage.Items) {
for _, item := range items {
_ = store.Delete(item.Key)
}
}
type iterationTest struct {
Name string
Options storage.IterateOptions
Expected storage.Items
}
func testIterations(t *testing.T, store storage.KeyValueStore, tests []iterationTest) {
t.Helper()
for _, test := range tests {
collect := &collector{}
err := store.Iterate(test.Options, collect.include)
if err != nil {
t.Errorf("%s: %v", test.Name, err)
continue
}
if diff := cmp.Diff(test.Expected, collect.Items, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("%s: (-want +got)\n%s", test.Name, diff)
}
}
}
type collector struct {
Items storage.Items
}
func (collect *collector) include(it storage.Iterator) error {
var item storage.ListItem
for it.Next(&item) {
collect.Items = append(collect.Items, storage.CloneItem(item))
}
return nil
}