-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkvstore_test.go
127 lines (124 loc) · 2.87 KB
/
kvstore_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
package minidb
import (
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
)
const MAXTEST = 100
func TestKVStore(t *testing.T) {
var tmp *os.File
var tx *Tx
tmp, _ = ioutil.TempFile("", "minidb-kvstore-testing-*")
db, err := Open("sqlite3", tmp.Name())
if err != nil {
t.Errorf("Open() failed: %s", err)
}
// test integer storage
var i int64
for i = 1; i < MAXTEST; i++ {
rvalue := rand.Int63()
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.SetInt(i, rvalue)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
if db.GetInt(i) != rvalue {
t.Errorf("failure setting int value, given %d, expected %d", db.GetInt(i), rvalue)
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.DeleteInt(i)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
}
// test string storage
for i = 1; i < MAXTEST; i++ {
rvalue := String(40)
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.SetStr(i, rvalue)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
if db.GetStr(i) != rvalue {
t.Errorf(`failure setting string value, given "%s", expected "%s"`, db.GetStr(i), rvalue)
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.DeleteStr(i)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
}
// test blob storage
for i = 1; i < MAXTEST; i++ {
rvalue := []byte(String(40))
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.SetBlob(i, rvalue)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
if string(db.GetBlob(i)) != string(rvalue) {
t.Errorf(`failure setting blob value, given "%s", expected "%s"`, string(db.GetBlob(i)), string(rvalue))
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.DeleteBlob(i)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
}
// test date storage
for i = 1; i < MAXTEST; i++ {
rvalue, err := ParseTime(NewRandomDateStr())
_ = err
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.SetDate(i, rvalue)
d1 := rvalue.UTC().Format(time.RFC3339)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
d2 := db.GetDate(i).UTC().Format(time.RFC3339)
if d1 != d2 {
t.Errorf(`failure setting date value, given "%s", expected "%s"`, d2, d1)
}
tx, err = db.Begin()
if err != nil {
t.Errorf("db.Begin transaction failed: %s", err)
}
tx.DeleteBlob(i)
err = tx.Commit()
if err != nil {
t.Errorf("tx.Commit failed: %s", err)
}
}
db.Close()
os.Remove(tmp.Name())
}