forked from ipfs/go-datastore
/
suite.go
86 lines (75 loc) · 2.09 KB
/
suite.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright for portions of this fork are held by [Juan Batiz-Benet, 2016]
// as part of the original go-datastore project. All other copyright for this
// fork are held by [DAOT Labs, 2020]. All rights reserved. Use of this source
// code is governed by MIT license that can be found in the LICENSE file.
package dstest
import (
"context"
"reflect"
"runtime"
"testing"
detectrace "github.com/ipfs/go-detect-race"
dstore "github.com/daotl/go-datastore"
"github.com/daotl/go-datastore/key"
query "github.com/daotl/go-datastore/query"
)
// BasicSubtests is a list of all basic tests.
var BasicSubtests = []func(t *testing.T, ktype key.KeyType, ds dstore.Datastore){
SubtestBasicPutGet,
SubtestNotFounds,
SubtestPrefix,
SubtestOrder,
SubtestLimit,
SubtestFilter,
SubtestManyKeysAndQuery,
SubtestReturnSizes,
SubtestBasicSync,
}
// Only enable the expensive "combinations" test when not running the race detector.
func init() {
if !detectrace.WithRace() {
BasicSubtests = append(BasicSubtests, SubtestCombinations)
}
}
// BatchSubtests is a list of all basic batching datastore tests.
var BatchSubtests = []func(t *testing.T, ktype key.KeyType, ds dstore.Batching){
RunBatchTest,
RunBatchDeleteTest,
RunBatchPutAndDeleteTest,
}
func getFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func clearDs(t *testing.T, ds dstore.Datastore) {
ctx := context.Background()
q, err := ds.Query(ctx, 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(ctx, r.Key); err != nil {
t.Fatal(err)
}
}
}
// SubtestAll tests the given datastore against all the subtests.
func SubtestAll(t *testing.T, ktype key.KeyType, ds dstore.Datastore) {
for _, f := range BasicSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ktype, ds)
clearDs(t, ds)
})
}
if ds, ok := ds.(dstore.Batching); ok {
for _, f := range BatchSubtests {
t.Run(getFunctionName(f), func(t *testing.T) {
f(t, ktype, ds)
clearDs(t, ds)
})
}
}
}