suite.go 936 Bytes
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package dstest

import (
	"reflect"
	"runtime"
	"testing"

	dstore "github.com/ipfs/go-datastore"
)

// BasicSubtests is a list of all basic tests.
var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
	SubtestBasicPutGet,
	SubtestNotFounds,
	SubtestManyKeysAndQuery,
}

// BatchSubtests is a list of all basic batching datastore tests.
var BatchSubtests = []func(t *testing.T, ds dstore.Batching){
	RunBatchTest,
	RunBatchDeleteTest,
}

func getFunctionName(i interface{}) string {
	return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

// SubtestAll tests the given datastore against all the subtests.
func SubtestAll(t *testing.T, ds dstore.Datastore) {
	for _, f := range BasicSubtests {
		t.Run(getFunctionName(f), func(t *testing.T) {
			f(t, ds)
		})
	}
Steven Allen's avatar
Steven Allen committed
35 36
	if ds, ok := ds.(dstore.Batching); ok {
		for _, f := range BatchSubtests {
Steven Allen's avatar
Steven Allen committed
37 38 39 40 41 42
			t.Run(getFunctionName(f), func(t *testing.T) {
				f(t, ds)
			})
		}
	}
}