-
Notifications
You must be signed in to change notification settings - Fork 4
/
leveldb_helper_test.go
191 lines (157 loc) · 3.41 KB
/
leveldb_helper_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
package leveldbwrapper
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
func getDB(path string) *DB {
readOpts := &opt.ReadOptions{}
writeOptsNoSync := &opt.WriteOptions{}
writeOptsSync := &opt.WriteOptions{}
writeOptsSync.Sync = true
return &DB{
levelDBPath: path,
dbState: closed,
readOpts: readOpts,
writeOptsNoSync: writeOptsNoSync,
writeOptsSync: writeOptsSync,
}
}
func TestCreateNewDB(t *testing.T) {
//when
path := "./test_db_path"
//then
db := CreateNewDB(path)
assert.Equal(t, db.dbState, closed)
assert.Equal(t, db.levelDBPath, path)
}
func TestDB_Open(t *testing.T) {
//when
path := "./test_db_path/"
db := getDB(path)
//then
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
//result
assert.Equal(t, db.dbState, opened)
assert.NotNil(t, db.db)
}
func TestDB_Get_ExistValue(t *testing.T) {
//when
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
err := db.db.Put([]byte("jun"), []byte("jun"), nil)
if err != nil {
t.Error("get error!")
}
//then
byteValue, err := db.Get([]byte("jun"))
//result
assert.Equal(t, string(byteValue), "jun")
}
func TestDB_Get_UNExistValue(t *testing.T) {
//when
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
//then
byteValue, err := db.Get([]byte("jun"))
//result
assert.Equal(t, []byte(nil), byteValue)
assert.Equal(t, nil, err)
}
//없으면 write 있으면 update로 작용
func TestDB_Put(t *testing.T) {
//when
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
//then
db.Put([]byte("jun"), []byte("jun"), true)
//result
byteValue, err := db.db.Get([]byte("jun"), nil)
if err != nil {
t.Error("get error!")
}
assert.Equal(t, string(byteValue), "jun")
}
//UNExistValue삭제는 아무것도 하지 않음 따라서 existValue만 체크
func TestDB_Delete_ExistValue(t *testing.T) {
//when
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
var err error
err = db.db.Put([]byte("jun"), []byte("jun"), nil)
if err != nil {
t.Error("put error!")
}
//then
err = db.Delete([]byte("jun"), true)
if err != nil {
t.Error("delete error!")
}
_, err = db.db.Get([]byte("jun"), nil)
assert.Equal(t, err, leveldb.ErrNotFound)
}
func TestDB_WriteBatch(t *testing.T) {
//when
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
//then
batch := new(leveldb.Batch)
batch.Put([]byte("jun"), []byte("jun"))
db.writeBatch(batch, true)
//result
byteValue, err := db.db.Get([]byte("jun"), nil)
if err != nil {
t.Error("get error!")
}
assert.Equal(t, string(byteValue), "jun")
}
func TestDB_snapSnapshot(t *testing.T) {
path := "./test_db_path"
db := getDB(path)
db.Open()
defer func() {
db.Close()
os.RemoveAll(path)
}()
batch := new(leveldb.Batch)
batch.Put([]byte("key1"), []byte("val1"))
batch.Put([]byte("key2"), []byte("val2"))
batch.Put([]byte("key3"), []byte("val3"))
db.writeBatch(batch, true)
snap, err := db.Snapshot()
assert.NoError(t, err)
assert.Equal(t, "val1", string(snap["key1"]))
assert.Equal(t, "val2", string(snap["key2"]))
assert.Equal(t, "val3", string(snap["key3"]))
}