-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsqlite3_test.go
125 lines (95 loc) · 2.59 KB
/
sqlite3_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
package sqlite3
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
)
const (
testKey = "foo"
testValue = "bar"
testTable = "cache"
testDBPath = "/cache.db"
)
func TestSqlite3(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
db, err := sql.Open("sqlite3", dir+testDBPath)
if err != nil {
t.Skip(err)
}
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
c, err := New(db, testTable)
if err != nil {
t.Skip(err)
}
if err := c.Save(testKey, testValue, 1*time.Nanosecond); err != nil {
t.Errorf("save fail: expected nil, got %v", err)
}
if _, err := c.Fetch(testKey); err == nil {
t.Errorf("fetch fail: expected an error, got %v", err)
}
_ = c.Save(testKey, testValue, 10*time.Second)
if res, _ := c.Fetch(testKey); res != testValue {
t.Errorf("fetch fail, wrong value: expected %s, got %s", testValue, res)
}
_ = c.Save(testKey, testValue, 0)
if !c.Contains(testKey) {
t.Errorf("contains failed: the key %s should be exist", testKey)
}
_ = c.Save("bar", testValue, 0)
if values := c.FetchMulti([]string{testKey, "bar"}); len(values) == 0 {
t.Errorf("fetch multi failed: expected %d, got %d", 2, len(values))
}
if err := c.Flush(); err != nil {
t.Errorf("flush failed: expected nil, got %v", err)
}
if c.Contains(testKey) {
t.Errorf("contains failed: the key %s should not be exist", testKey)
}
}
func TestSqlite3Fail(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
db, _ := sql.Open("sqlite3", dir+testDBPath)
_ = db.Close()
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
if _, err := New(db, testTable); err == nil {
t.Errorf("constructor failed: expected an error, got %v", err)
}
db, _ = sql.Open("sqlite3", testDBPath)
c, _ := New(db, testTable)
_ = db.Close()
if err := c.Save(testKey, testValue, 0); err == nil {
t.Errorf("save failed: expected an error, got %v", err)
}
if err := c.Delete(testKey); err == nil {
t.Errorf("delete failed: expected an error, got %v", err)
}
if err := c.Flush(); err == nil {
t.Errorf("flush failed: expected an error, got %v", err)
}
db, _ = sql.Open("sqlite3", testDBPath)
c, _ = New(db, testTable)
_, _ = db.Exec(fmt.Sprintf("DROP TABLE %s;", testTable))
if err := c.Save(testKey, testValue, 0); err == nil {
t.Errorf("save failed: expected an error, got %v", err)
}
if err := c.Delete(testKey); err == nil {
t.Errorf("delete failed: expected an error, got %v", err)
}
if err := c.Flush(); err == nil {
t.Errorf("flush failed: expected an error, got %v", err)
}
}