-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
206 lines (185 loc) · 5.13 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
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
package xisdb
import (
"fmt"
"testing"
"time"
)
func TestDBRollbackItemAdd(t *testing.T) {
fmt.Println("-- TestDBRollbackItemAdd")
db := openTestDB()
tx := NewTransaction(true, db)
assertDBKeyValue(t, db, "key", "value", false)
tx.addRollback("", "key", &Item{"key", "value", nil})
db.lock(true)
db.rollback(tx)
assertDBKeyValue(t, db, "key", "value", false)
}
func TestDBRollbackItemUpdate(t *testing.T) {
fmt.Println("-- TestDBRollbackItemUpdate")
db := openTestDB()
tx := NewTransaction(true, db)
db.Set("key", "value")
assertDBKeyValue(t, db, "key", "value", true)
tx.addRollback("", "key", &Item{"key", "value", nil})
db.Set("key", "value2")
assertDBKeyValue(t, db, "key", "value2", true)
db.lock(true)
db.rollback(tx)
assertDBKeyValue(t, db, "key", "value", true)
}
func TestDBRollbackItemDelete(t *testing.T) {
fmt.Println("-- TestDBRollbackItemDelete")
db := openTestDB()
tx := NewTransaction(true, db)
db.Set("key", "value")
assertDBKeyValue(t, db, "key", "value", true)
tx.addRollback("", "key", nil)
db.lock(true)
db.rollback(tx)
assertDBKeyValue(t, db, "key", "value", false)
}
func TestDBRollbackBucketAdd(t *testing.T) {
fmt.Println("-- TestDBRollbackBucketAdd")
db := openTestDB()
tx := NewTransaction(true, db)
assertBucketExists(t, db, "b1", false)
db.Bucket("b1")
assertBucketExists(t, db, "b1", true)
tx.addRollbackBucket("b1", nil) // didn't exist before
db.lock(true)
err := db.rollback(tx)
if err != nil {
t.Errorf("Got an error rolling back: %s", err)
}
assertBucketExists(t, db, "b1", false)
}
func TestDBRollbackBucketDelete(t *testing.T) {
fmt.Println("-- TestDBRollbackBucketDelete")
db := openTestDB()
tx := NewTransaction(true, db)
assertBucketExists(t, db, "b1", false)
db.Bucket("b1")
assertBucketExists(t, db, "b1", true)
b := db.buckets["b1"]
tx.addRollbackBucket("b1", b)
db.lock(true)
err := db.rollback(tx)
if err != nil {
t.Errorf("Got an error rolling back: %s", err)
}
assertBucketExists(t, db, "b1", true)
}
func TestDBRollbackBucketMultipleOperations(t *testing.T) {
fmt.Println("-- TestDBRollbackBucketMultipleOperations")
db := openTestDB()
tx := NewTransaction(true, db)
assertBucketExists(t, db, "b1", false)
db.Bucket("b1")
tx.addRollbackBucket("b1", nil) // didn't exist
assertBucketExists(t, db, "b1", true)
b := db.buckets["b1"]
db.DeleteBucket("b1")
tx.addRollbackBucket("b1", b) // did
assertBucketExists(t, db, "b1", false)
if len(tx.rollbackBuckets) != 1 {
t.Errorf("Expected 1 item in bucket rollback log, got %d", len(tx.rollbackBuckets))
}
db.Bucket("b1")
tx.addRollbackBucket("b1", nil) // didn't exist again
assertBucketExists(t, db, "b1", true)
if len(tx.rollbackBuckets) != 1 {
t.Errorf("Expected 1 item in bucket rollback log, got %d", len(tx.rollbackBuckets))
}
db.lock(true)
err := db.rollback(tx)
if err != nil {
t.Errorf("Got an error rolling back: %s", err)
}
assertBucketExists(t, db, "b1", false)
}
func TestDBAddRootBucket(t *testing.T) {
fmt.Println("-- TestDBAddRootBucket")
db := openTestDB()
b, created := db.addBucket("")
if created {
t.Errorf("Expected bucket '' to not be created, it was")
}
if !b.isRoot() {
t.Errorf("Expected bucket '' to be root, it wasn't")
}
}
func TestDBDeleteRootBucket(t *testing.T) {
fmt.Println("-- TestDBDeleteRootBucket")
db := openTestDB()
ok, err := db.deleteBucket("")
if !ok || err != ErrCannotDeleteRootBucket {
t.Errorf("Shouldn't be able to delete root bucket. got err:%s", err)
}
}
// TestCommitHooks -- verifies that functions execute upon commit
func TestDBCommitHooks(t *testing.T) {
fmt.Println("-- TestDBCommitHooks")
db := openTestDB()
i := 0
f := func() {
i++
}
tx := NewTransaction(false, db)
tx.Hooks(f)
db.lock(false)
db.commit(tx)
if i != 1 {
t.Errorf("Expected function to run, it did not")
}
}
// TestDBExpiration -- tests that expiring a key works
func TestDBExpiration(t *testing.T) {
fmt.Println("-- TestDBExpiration")
db, _ := Open(&Options{
InMemory: true,
BackgroundInterval: 10,
})
db.ReadWrite(func(tx *Tx) error {
err := tx.Set("expireme", "value", &SetMetadata{10})
return err
})
v, err := db.Get("expireme")
if err != nil {
t.Error(err)
}
if v != "value" {
t.Errorf("Expected expireme key to have value [value], got [%s]", v)
}
time.Sleep(30 * time.Millisecond)
_, err = db.Get("expireme")
if err != ErrKeyNotFound {
t.Errorf("Expected [%s] as error, got [%s]", ErrKeyNotFound, err)
}
}
func TestDBBackground(t *testing.T) {
fmt.Println("-- TestDBBackground")
}
func TestDBClose(t *testing.T) {
fmt.Println("-- TestDBClose")
db := openTestDB()
err := db.Close()
if err != nil {
t.Errorf("Got an error closing database: %s", err)
}
}
func assertDBKeyValue(t *testing.T, db *DB, key, value string, exists bool) {
v, err := db.Get(key)
if err == ErrKeyNotFound && exists {
t.Errorf("Expected key '%s' to exist, got %s", key, err)
return
}
if exists && v != value {
t.Errorf("Expected value '%s', got '%s'", value, v)
}
}
func assertBucketExists(t *testing.T, db *DB, bucket string, exists bool) {
_, ok := db.buckets[bucket]
if ok != exists {
t.Errorf("Expected bucket to exist: %t, got %t", exists, !exists)
}
}