-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
65 lines (58 loc) · 1.81 KB
/
db_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
package godb
import (
"fmt"
"os"
"path"
"testing"
"time"
"github.com/jakub-galecki/godb/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func cleanup(dir string) {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}
func TestCore(t *testing.T) {
dbName := time.Now().Format(time.RFC3339Nano)
lsmt, err := Open(dbName, WithDbPath(os.TempDir()), WithLogger(log.JsonLogger))
assert.NoError(t, err)
for i := 0; i < 100000; i++ {
err := lsmt.Set([]byte(fmt.Sprintf("foo.%d", i)), []byte(fmt.Sprintf("bar.%d", i)))
require.NoError(t, err)
}
assert.NoError(t, lsmt.Close())
lsmt, err = Open(dbName, WithDbPath(os.TempDir()))
assert.NoError(t, err)
for i := 0; i < 100000; i++ {
val, found := lsmt.Get([]byte(fmt.Sprintf("foo.%d", i)))
require.Truef(t, found, "key %s not found", fmt.Sprintf("foo.%d", i))
require.Equal(t, []byte(fmt.Sprintf("bar.%d", i)), val)
}
cleanup(path.Join(os.TempDir(), dbName))
}
func TestOverwrite(t *testing.T) {
dbName := time.Now().Format(time.RFC3339Nano)
lsmt, err := Open(dbName, WithDbPath(os.TempDir()))
assert.NoError(t, err)
N := 1000
for i := 0; i < N; i++ {
err := lsmt.Set([]byte(fmt.Sprintf("foo.%d", i)), []byte(fmt.Sprintf("bar.%d", i)))
require.NoError(t, err)
}
for i := 0; i < N; i++ {
err := lsmt.Set([]byte(fmt.Sprintf("foo.%d", i)), []byte(fmt.Sprintf("barr2.%d", i)))
require.NoError(t, err)
}
assert.NoError(t, lsmt.Close())
lsmt, err = Open(dbName, WithDbPath(os.TempDir()))
assert.NoError(t, err)
for i := 0; i < N; i++ {
val, found := lsmt.Get([]byte(fmt.Sprintf("foo.%d", i)))
require.Truef(t, found, "key %s not found", fmt.Sprintf("foo.%d", i))
// check that new values are returned from database
require.Equal(t, []byte(fmt.Sprintf("barr2.%d", i)), val)
}
cleanup(path.Join(os.TempDir(), dbName))
}