-
Notifications
You must be signed in to change notification settings - Fork 0
/
xisdb_test.go
80 lines (75 loc) · 1.71 KB
/
xisdb_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
package xisdb
import (
"fmt"
"testing"
)
func openTestDB() *DB {
db, _ := Open(&Options{
InMemory: true,
BackgroundInterval: -1,
DisableExpiration: true,
})
return db
}
// NOTE: many of these tests will fail unexpectedly as they just test high-level, not underlying
// Underlying APIs will be used that these tests will assume pass appropriately
func TestXisGet(t *testing.T) {
fmt.Println("-- TestXisGet")
tests := []struct {
key, value string
add bool
err error
}{
{"key", "value", true, nil},
{"key", "value", false, ErrKeyNotFound},
}
for i, test := range tests {
db := openTestDB()
if test.add {
db.ReadWrite(func(tx *Tx) error {
return tx.Set(test.key, test.value, nil)
})
}
val, err := db.Get(test.key)
if err != test.err {
t.Errorf("Test %d failed: expected error '%s', got '%s'", i+1, test.err, err)
continue
}
if test.err != nil {
continue
}
if val != test.value {
t.Errorf("Test %d failed: expected value '%s', got '%s'", i+1, test.value, val)
}
}
}
func TestXisExists(t *testing.T) {
fmt.Println("-- TestXisExists")
tests := []struct {
key string
add, exists bool
err error
}{
{"key", true, true, nil},
{"key", false, false, nil},
}
for i, test := range tests {
db := openTestDB()
if test.add {
db.ReadWrite(func(tx *Tx) error {
return tx.Set(test.key, "test", nil)
})
}
exists, err := db.Exists(test.key)
if err != test.err {
t.Errorf("Test %d failed: expected error '%s', got '%s'", i+1, test.err, err)
continue
}
if test.err != nil {
continue
}
if exists != test.exists {
t.Errorf("Test %d failed: expected exists %t, got %t", i+1, test.exists, exists)
}
}
}