-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_test.go
More file actions
59 lines (47 loc) · 1.2 KB
/
Copy pathdb_test.go
File metadata and controls
59 lines (47 loc) · 1.2 KB
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
package memdb
import (
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDB_FileImport(t *testing.T) {
path := "test.db"
os.RemoveAll(path)
fs, err := openFileStorage(path)
require.Nil(t, err)
err = fs.write([]fileItem{
{item: item{key: "FIRSTKEY", value: "FIRSTVALUE"}, command: commandSET},
{item: item{key: "FIRSTKEY"}, command: commandDEL},
{item: item{key: "FIRSTKEY", value: "THIRDVALUE"}, command: commandSET},
}...)
require.Nil(t, err)
err = fs.close()
require.Nil(t, err)
db, err := OpenDB("test.db", true)
assert.Nil(t, err)
last := db.items.get("FIRSTKEY")
assert.Equal(t, "THIRDVALUE", last.current.value)
tx := db.Begin(false)
value, err := tx.Get("FIRSTKEY")
assert.Nil(t, err)
assert.Equal(t, "THIRDVALUE", value)
}
func BenchmarkDatabaseSet(b *testing.B) {
b.ReportAllocs()
require.Nil(b, os.RemoveAll("bench.db"))
db, err := OpenDB("bench.db", false)
require.Nil(b, err)
b.ResetTimer()
tx := db.Begin(true)
for n := 0; n < b.N; n++ {
err = tx.Set(strconv.FormatInt(int64(n), 10), "somevalue")
if err != nil {
require.Nil(b, err)
}
}
if err := tx.Commit(); err != nil {
require.Nil(b, err)
}
}