-
Notifications
You must be signed in to change notification settings - Fork 139
/
kv_test.go
174 lines (135 loc) · 3.88 KB
/
kv_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package storm
import (
"net/mail"
"testing"
"time"
"github.com/asdine/storm/v3/codec/gob"
"github.com/asdine/storm/v3/codec/json"
bolt "go.etcd.io/bbolt"
"github.com/stretchr/testify/require"
)
func TestGet(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Set("trash", 10, 100)
require.NoError(t, err)
var nb int
err = db.Get("trash", 10, &nb)
require.NoError(t, err)
require.Equal(t, 100, nb)
tm := time.Now()
err = db.Set("logs", tm, "I'm hungry")
require.NoError(t, err)
var message string
err = db.Get("logs", tm, &message)
require.NoError(t, err)
require.Equal(t, "I'm hungry", message)
var hand int
err = db.Get("wallet", "100 bucks", &hand)
require.Equal(t, ErrNotFound, err)
err = db.Set("wallet", "10 bucks", 10)
require.NoError(t, err)
err = db.Get("wallet", "100 bucks", &hand)
require.Equal(t, ErrNotFound, err)
err = db.Get("logs", tm, nil)
require.Equal(t, ErrPtrNeeded, err)
err = db.Get("", nil, nil)
require.Equal(t, ErrPtrNeeded, err)
err = db.Get("", "100 bucks", &hand)
require.Equal(t, ErrNotFound, err)
}
func TestGetBytes(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.SetBytes("trash", "a", []byte("hi"))
require.NoError(t, err)
val, err := db.GetBytes("trash", "a")
require.NoError(t, err)
require.Equal(t, []byte("hi"), val)
_, err = db.GetBytes("trash", "b")
require.Equal(t, ErrNotFound, err)
}
func TestSet(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Set("b1", 10, 10)
require.NoError(t, err)
err = db.Set("b1", "best friend's mail", &mail.Address{Name: "Gandalf", Address: "[email protected]"})
require.NoError(t, err)
err = db.Set("b2", []byte("i'm already a slice of bytes"), "a value")
require.NoError(t, err)
err = db.Set("b2", []byte("i'm already a slice of bytes"), nil)
require.NoError(t, err)
err = db.Set("b1", 0, 100)
require.NoError(t, err)
err = db.Set("b1", nil, 100)
require.Error(t, err)
db.Bolt.View(func(tx *bolt.Tx) error {
b1 := tx.Bucket([]byte("b1"))
require.NotNil(t, b1)
b2 := tx.Bucket([]byte("b2"))
require.NotNil(t, b2)
k1, err := toBytes(10, json.Codec)
require.NoError(t, err)
val := b1.Get(k1)
require.NotNil(t, val)
k2 := []byte("best friend's mail")
val = b1.Get(k2)
require.NotNil(t, val)
k3, err := toBytes(0, json.Codec)
require.NoError(t, err)
val = b1.Get(k3)
require.NotNil(t, val)
return nil
})
err = db.Set("", 0, 100)
require.Error(t, err)
err = db.Set("b", nil, 100)
require.Error(t, err)
err = db.Set("b", 10, nil)
require.NoError(t, err)
err = db.Set("b", nil, nil)
require.Error(t, err)
}
func TestSetMetadata(t *testing.T) {
db, cleanup := createDB(t, Batch())
defer cleanup()
w := User{ID: 10, Name: "John"}
err := db.Set("User", 10, &w)
require.NoError(t, err)
n := db.WithCodec(gob.Codec)
err = n.Set("User", 10, &w)
require.Equal(t, ErrDifferentCodec, err)
}
func TestDelete(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Set("files", "myfile.csv", "a,b,c,d")
require.NoError(t, err)
err = db.Delete("files", "myfile.csv")
require.NoError(t, err)
err = db.Delete("files", "myfile.csv")
require.NoError(t, err)
err = db.Delete("i don't exist", "myfile.csv")
require.Equal(t, ErrNotFound, err)
err = db.Delete("", nil)
require.Equal(t, ErrNotFound, err)
}
func TestKeyExists(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Set("files", "myfile.csv", "a,b,c,d")
require.NoError(t, err)
exists, err := db.KeyExists("files", "myfile.csv")
require.NoError(t, err)
require.True(t, exists)
err = db.Delete("files", "myfile.csv")
require.NoError(t, err)
exists, err = db.KeyExists("files", "myfile.csv")
require.NoError(t, err)
require.False(t, exists)
exists, err = db.KeyExists("i don't exist", "myfile.csv")
require.Equal(t, ErrNotFound, err)
exists, err = db.KeyExists("", nil)
require.Equal(t, ErrNotFound, err)
}