-
Notifications
You must be signed in to change notification settings - Fork 1
/
datastore_test.go
123 lines (117 loc) · 2.5 KB
/
datastore_test.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package dsbbolt
import (
"fmt"
"testing"
"reflect"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
func Test_NewDatastore(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
wantErr bool
}{
{"Success", args{"./tmp"}, false},
{"Fail", args{"/root/toor"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if ds, err := NewDatastore(tt.args.path, nil, nil); (err != nil) != tt.wantErr {
t.Fatalf("NewDatastore() err = %v, wantErr %v", err, tt.wantErr)
} else if !tt.wantErr {
if err := ds.Close(); err != nil {
t.Fatal(err)
}
}
})
}
}
func Test_Datastore(t *testing.T) {
ds, err := NewDatastore("./tmp", nil, nil)
if err != nil {
t.Fatal(err)
}
defer ds.Close()
key := datastore.NewKey("keks")
key2 := datastore.NewKey("keks2")
if err := ds.Put(key, []byte("hello world")); err != nil {
t.Fatal(err)
}
if err := ds.Put(key2, []byte("hello world")); err != nil {
t.Fatal(err)
}
data, err := ds.Get(key)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(data, []byte("hello world")) {
t.Fatal("bad data")
}
if has, err := ds.Has(key); err != nil {
t.Fatal(err)
} else if !has {
t.Fatal("should have key")
}
if size, err := ds.GetSize(key); err != nil {
t.Fatal(err)
} else if size != len([]byte("hello world")) {
t.Fatal("incorrect data size")
}
// test a query where we specify a search key
rs, err := ds.Query(query.Query{Prefix: key.String()})
if err != nil {
t.Fatal(err)
}
res, err := rs.Rest()
if err != nil {
t.Fatal(err)
}
if len(res) != 2 {
fmt.Printf("only found %v results \n", len(res))
for _, v := range res {
fmt.Printf("%+v\n", v)
}
t.Fatal("bad number of results")
}
// test a query where we dont specify a search key
rs, err = ds.Query(query.Query{})
if err != nil {
t.Fatal(err)
}
res, err = rs.Rest()
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Fatal("bad number of results")
}
// test a query where we specify a partial prefix
rs, err = ds.Query(query.Query{Prefix: "/kek"})
if err != nil {
t.Fatal(err)
}
res, err = rs.Rest()
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Fatal("bad number of results")
}
if err := ds.Delete(key); err != nil {
t.Fatal(err)
}
if has, err := ds.Has(key); err != nil {
t.Fatal(err)
} else if has {
t.Fatal("should not have key")
}
if size, err := ds.GetSize(key); err != nil {
t.Fatal(err)
} else if size != 0 {
t.Fatal("bad size")
}
}