forked from jsimonetti/berkeleydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdb_test.go
232 lines (204 loc) · 5.64 KB
/
bdb_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package berkeleydb_test
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/jefchien/berkeleydb"
)
const (
testFilename = "test_db.db"
testKey = "key"
testValue = "value"
testNoSuchKey = "no_such_key"
)
func TestNewDB(t *testing.T) {
db, err := berkeleydb.NewDB()
require.NoError(t, err)
require.NotNil(t, db)
}
func TestVersion(t *testing.T) {
require.NotNil(t, berkeleydb.Version())
}
func TestOpen(t *testing.T) {
path := filepath.Join(t.TempDir(), testFilename)
db, err := berkeleydb.NewDB()
require.NoError(t, err)
err = db.Open(path, berkeleydb.DbBtree, berkeleydb.DbCreate)
require.NoError(t, err)
flags, err := db.Flags()
require.NoError(t, err)
require.Equal(t, berkeleydb.DbCreate, int(flags))
err = db.Open("", berkeleydb.DbQueue, berkeleydb.DbCreate|berkeleydb.DbExcl)
require.ErrorIs(t, err, berkeleydb.ErrOpened)
require.NoError(t, db.Close())
}
func TestClose(t *testing.T) {
db, err := setup(t)
require.NoError(t, err)
require.NoError(t, db.Close())
var errs []error
errs = append(errs, db.Close())
errs = append(errs, db.Rename("", ""))
errs = append(errs, db.Remove(""))
errs = append(errs, db.Put("", ""))
errs = append(errs, db.Delete(""))
err = db.Open("", berkeleydb.DbUnknown, berkeleydb.DbRdOnly)
errs = append(errs, err)
_, err = db.Flags()
errs = append(errs, err)
_, err = db.Get("")
errs = append(errs, err)
_, err = db.Cursor()
errs = append(errs, err)
for _, err = range errs {
require.ErrorIs(t, err, berkeleydb.ErrDatabaseClosed)
}
}
func TestPut(t *testing.T) {
db, err := setup(t)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})
require.NoError(t, db.Put(testKey, testValue))
}
func TestGet(t *testing.T) {
db, err := setup(t)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})
require.NoError(t, db.Put(testKey, testValue))
val, err := db.Get(testKey)
require.NoError(t, err)
require.Equal(t, testValue, val)
val, err = db.Get(testNoSuchKey)
require.Error(t, err)
}
func TestDelete(t *testing.T) {
db, err := setup(t)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})
require.NoError(t, db.Put(testKey, testValue))
require.NoError(t, db.Delete(testKey))
require.Error(t, db.Delete(testNoSuchKey))
}
func TestRemove(t *testing.T) {
path := filepath.Join(t.TempDir(), testFilename)
db, err := setupWith(t, path)
require.NoError(t, err)
// cannot call remove after open has been called
require.Error(t, db.Remove(path))
// cannot access db handle after remove
require.ErrorIs(t, db.Close(), berkeleydb.ErrDatabaseClosed)
db, err = berkeleydb.NewDB()
require.NoError(t, err)
require.NoError(t, db.Remove(path))
}
func TestRename(t *testing.T) {
dir := t.TempDir()
oldPath := filepath.Join(dir, testFilename)
db, err := setupWith(t, oldPath)
require.NoError(t, err)
newPath := filepath.Join(dir, "foo_"+testFilename)
// cannot call remove after open has been called
require.Error(t, db.Rename(oldPath, newPath))
// cannot access db handle after rename
require.ErrorIs(t, db.Close(), berkeleydb.ErrDatabaseClosed)
db, err = berkeleydb.NewDB()
require.NoError(t, err)
require.NoError(t, db.Rename(oldPath, newPath))
_, err = os.Stat(oldPath)
require.Error(t, err)
require.ErrorIs(t, err, fs.ErrNotExist)
_, err = os.Stat(newPath)
require.NoError(t, err)
}
func TestCursor(t *testing.T) {
path := filepath.Join(t.TempDir(), testFilename)
db, err := berkeleydb.NewDB()
require.NoError(t, err)
err = db.Open(path, berkeleydb.DbHash, berkeleydb.DbCreate)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, db.Close())
})
expected := map[string]string{
testKey: testValue,
testValue: testKey,
testKey + testValue: testFilename,
}
for k, v := range expected {
require.NoError(t, db.Put(k, v))
}
cursor, err := db.Cursor()
require.NoError(t, err)
actual := make(map[string]string)
for {
var k, v string
k, v, err = cursor.GetString(berkeleydb.DbNext)
if err != nil {
break
}
actual[k] = v
}
require.NoError(t, cursor.Close())
require.Equal(t, len(expected), len(actual))
require.Equal(t, expected, actual)
// cannot close again
require.ErrorIs(t, cursor.Close(), berkeleydb.ErrCursorClosed)
_, _, err = cursor.GetString(berkeleydb.DbPrev)
require.ErrorIs(t, err, berkeleydb.ErrCursorClosed)
cursor, err = db.Cursor()
require.NoError(t, err)
_, _, err = cursor.GetString(berkeleydb.DbFirst)
require.NoError(t, err)
_, _, err = cursor.GetString(berkeleydb.DbLast)
require.NoError(t, err)
require.NoError(t, cursor.Close())
}
func TestOpenWithMode(t *testing.T) {
path := filepath.Join(t.TempDir(), testFilename)
testCases := []struct {
mode int
want string
}{
{0, "0640"},
{0644, "0644"},
{0600, "0600"},
{0755, "0755"},
}
for _, testCase := range testCases {
db, err := berkeleydb.NewDB()
require.NoError(t, err)
err = db.OpenWithMode(path, berkeleydb.DbRecno, berkeleydb.DbCreate|berkeleydb.DbTruncate, testCase.mode)
require.NoError(t, err)
require.NoError(t, db.Close())
info, err := os.Lstat(path)
require.NoError(t, err)
require.Equal(t, testCase.want, fmt.Sprintf("%04o", info.Mode().Perm()))
require.NoError(t, os.Remove(path))
}
}
func setup(t *testing.T) (*berkeleydb.Db, error) {
t.Helper()
path := filepath.Join(t.TempDir(), testFilename)
return setupWith(t, path)
}
func setupWith(t *testing.T, filename string) (*berkeleydb.Db, error) {
t.Helper()
db, err := berkeleydb.NewDB()
if err != nil {
return nil, err
}
err = db.Open(filename, berkeleydb.DbHash, berkeleydb.DbCreate)
if err != nil {
return nil, err
}
return db, nil
}