-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.go
108 lines (94 loc) · 2.57 KB
/
batch_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
package godb
import (
"fmt"
"testing"
"github.com/jakub-galecki/godb/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_NewBatch(t *testing.T) {
b := NewBatch()
assert.NotNil(t, b)
b.Set([]byte("test0"), []byte("testVal"))
b.Set([]byte("test1"), []byte("testVal"))
b.Set([]byte("test2"), []byte("testVal"))
b.Set([]byte("test3"), []byte("testVal"))
}
func Test_BatchIter(t *testing.T) {
b := NewBatch()
b.seqNum = 0
assert.NotNil(t, b)
b.Set([]byte("test0"), []byte("testVal"))
b.Set([]byte("test1"), []byte("testVal"))
b.Set([]byte("test2"), []byte("testVal"))
b.Set([]byte("test3"), []byte("testVal"))
b.Delete([]byte("test4"))
it := b.Iter()
assert.NotNil(t, it)
i := 0
for {
op, seq, key, val := it.Next()
if op == 0 && key == nil && val == nil {
require.Equal(t, i, 5)
return
}
assert.Equal(t, uint64(i), seq)
if i == 4 {
require.Equal(t, op, common.DELETE)
require.Equal(t, key, []byte(fmt.Sprintf("test%d", i)))
require.Nil(t, val)
} else {
require.Equal(t, op, common.SET)
require.Equal(t, key, []byte(fmt.Sprintf("test%d", i)))
require.Equal(t, val, []byte("testVal"))
}
i++
}
}
func Test_Encode(t *testing.T) {
b := NewBatch()
assert.NotNil(t, b)
b.Set([]byte("test0"), []byte("testVal"))
b.Set([]byte("test1"), []byte("testVal"))
b.Set([]byte("test2"), []byte("testVal"))
b.Set([]byte("test3"), []byte("testVal"))
encoded := b.encode()
assert.Equal(t, encoded, []byte("\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05test0\atestVal\x00\x05test1\atestVal\x00\x05test2\atestVal\x00\x05test3\atestVal"))
}
func Test_Decode(t *testing.T) {
b := NewBatch()
assert.NotNil(t, b)
b.seqNum = 100
b.Set([]byte("test0"), []byte("testVal"))
b.Set([]byte("test1"), []byte("testVal"))
b.Set([]byte("test2"), []byte("testVal"))
b.Set([]byte("test3"), []byte("testVal"))
b.Delete([]byte("test4"))
encoded := b.encode()
newB := NewBatch()
newB.decode(encoded)
assert.Equal(t, uint32(5), newB.size)
assert.Equal(t, uint64(100), b.seqNum)
assert.Equal(t, uint64(100), newB.seqNum)
it := b.Iter()
assert.NotNil(t, it)
i := 0
for {
op, seq, key, val := it.Next()
if op == 0 && key == nil && val == nil {
require.Equal(t, i, 5)
return
}
assert.Equal(t, uint64(i+100), seq)
if i == 4 {
require.Equal(t, op, common.DELETE)
require.Equal(t, key, []byte(fmt.Sprintf("test%d", i)))
require.Nil(t, val)
} else {
require.Equal(t, op, common.SET)
require.Equal(t, key, []byte(fmt.Sprintf("test%d", i)))
require.Equal(t, val, []byte("testVal"))
}
i++
}
}