suite.go 1.43 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8
package dstest

import (
	"reflect"
	"runtime"
	"testing"

	dstore "github.com/ipfs/go-datastore"
9
	query "github.com/ipfs/go-datastore/query"
Steven Allen's avatar
Steven Allen committed
10 11 12 13 14 15
)

// BasicSubtests is a list of all basic tests.
var BasicSubtests = []func(t *testing.T, ds dstore.Datastore){
	SubtestBasicPutGet,
	SubtestNotFounds,
16
	SubtestCombinations,
17
	SubtestPrefix,
Steven Allen's avatar
Steven Allen committed
18
	SubtestOrder,
Steven Allen's avatar
Steven Allen committed
19
	SubtestLimit,
Steven Allen's avatar
Steven Allen committed
20
	SubtestFilter,
Steven Allen's avatar
Steven Allen committed
21
	SubtestManyKeysAndQuery,
22
	SubtestReturnSizes,
23
	SubtestBasicSync,
Steven Allen's avatar
Steven Allen committed
24 25 26 27 28 29
}

// BatchSubtests is a list of all basic batching datastore tests.
var BatchSubtests = []func(t *testing.T, ds dstore.Batching){
	RunBatchTest,
	RunBatchDeleteTest,
30
	RunBatchPutAndDeleteTest,
Steven Allen's avatar
Steven Allen committed
31 32 33 34 35 36
}

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

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
func clearDs(t *testing.T, ds dstore.Datastore) {
	q, err := ds.Query(query.Query{KeysOnly: true})
	if err != nil {
		t.Fatal(err)
	}
	res, err := q.Rest()
	if err != nil {
		t.Fatal(err)
	}
	for _, r := range res {
		if err := ds.Delete(dstore.RawKey(r.Key)); err != nil {
			t.Fatal(err)
		}
	}
}

Steven Allen's avatar
Steven Allen committed
53 54 55 56 57
// 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)
58
			clearDs(t, ds)
Steven Allen's avatar
Steven Allen committed
59 60
		})
	}
Steven Allen's avatar
Steven Allen committed
61 62
	if ds, ok := ds.(dstore.Batching); ok {
		for _, f := range BatchSubtests {
Steven Allen's avatar
Steven Allen committed
63 64
			t.Run(getFunctionName(f), func(t *testing.T) {
				f(t, ds)
65
				clearDs(t, ds)
Steven Allen's avatar
Steven Allen committed
66 67 68 69
			})
		}
	}
}