-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlcache_test.go
190 lines (158 loc) · 4.24 KB
/
sqlcache_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
package gosqlcache
import (
"database/sql"
"database/sql/driver"
"github.com/astaxie/beego/cache"
"testing"
"time"
)
import . "./test"
func TestNoCache(t *testing.T) {
// Start Setup
memCache := SecondsTimeoutCacheWrapper{cache.NewMemoryCache()}
log := NewTestLogger(t)
sqlCacher := NewSqlCacher(&memCache, log)
sqlCacheSpy := SpyOnSqlCacher(sqlCacher)
mockConn := MockConn{}
mockDriver := MockDriver{
Conn: &mockConn,
}
sqlCacheDriver := NewSqlCacheDriver(&mockDriver, sqlCacheSpy, log)
sql.Register(t.Name(), sqlCacheDriver)
db, err := sql.Open(t.Name(), "")
if err != nil {
t.Fatal("Could not connect to database", err)
}
// End Setup
defer db.Close()
defer sqlCacher.ClearAll()
mockConn.Rows = SpyOnRows(&cachedRows{
cols: []string{"number"},
data: [][]driver.Value{[]driver.Value{5}},
})
rows, err := db.Query("SELECT 5")
if err != nil {
t.Fatal("Could not select 5", err)
}
defer rows.Close()
var n int
ok := rows.Next()
if !ok {
t.Fatal("Could not get next")
}
rows.Scan(&n)
if n != 5 {
t.Fatal("Could not read result: ", n)
}
if sqlCacheSpy.WasEverCalled() {
t.Fatal("Cache should not have been called without registering query")
}
if !mockConn.WasCalledWith("Query", []interface{}{"SELECT 5", []driver.Value{}}) {
t.Logf("%#v", mockConn.Spy.Calls)
t.Fatal("Query should have been called")
}
}
func TestCacheCheckMiss(t *testing.T) {
// Start Setup
memCache := SecondsTimeoutCacheWrapper{cache.NewMemoryCache()}
log := NewTestLogger(t)
sqlCacher := NewSqlCacher(&memCache, log)
sqlCacheSpy := SpyOnSqlCacher(sqlCacher)
mockConn := MockConn{}
mockDriver := MockDriver{
Conn: &mockConn,
}
sqlCacheDriver := NewSqlCacheDriver(&mockDriver, sqlCacheSpy, log)
sql.Register(t.Name(), sqlCacheDriver)
db, err := sql.Open(t.Name(), "")
if err != nil {
t.Fatal("Could not connect to database", err)
}
// End Setup
defer db.Close()
defer sqlCacher.ClearAll()
sqlCacheDriver.RegisterQuery("SELECT 5", 1*time.Minute)
mockConn.Rows = SpyOnRows(&cachedRows{
cols: []string{"number"},
data: [][]driver.Value{[]driver.Value{5}},
})
rows, err := db.Query("SELECT 5")
if err != nil {
t.Fatal("Could not select 5", err)
}
defer rows.Close()
var n int
ok := rows.Next()
if !ok {
t.Fatal("Could not get next")
}
rows.Scan(&n)
if n != 5 {
t.Fatal("Could not read result: ", n)
}
if !sqlCacheSpy.WasCalled("GetQueryRows") {
t.Fatal("SqlCache GetQueryRows should have been checked")
}
if !mockConn.WasCalled("Query") {
t.Logf("%#v", mockConn.Spy.Calls)
t.Fatal("Query should have been called since cache was empty")
}
if !sqlCacheSpy.WasCalled("PutQueryRows") {
t.Logf("%#v", mockConn.Spy.Calls)
t.Fatal("Query results should have been cached")
}
}
func TestCacheCheckHit(t *testing.T) {
// Start Setup
memCache := SecondsTimeoutCacheWrapper{cache.NewMemoryCache()}
log := NewTestLogger(t)
sqlCacher := NewSqlCacher(&memCache, log)
sqlCacheSpy := SpyOnSqlCacher(sqlCacher)
mockConn := MockConn{}
mockDriver := MockDriver{
Conn: &mockConn,
}
sqlCacheDriver := NewSqlCacheDriver(&mockDriver, sqlCacheSpy, log)
sql.Register(t.Name(), sqlCacheDriver)
db, err := sql.Open(t.Name(), "")
if err != nil {
t.Fatal("Could not connect to database", err)
}
// End Setup
defer db.Close()
defer sqlCacher.ClearAll()
sqlCacheDriver.RegisterQuery("SELECT 5", 1*time.Minute)
cacheRows := SpyOnRows(&cachedRows{
cols: []string{"number"},
data: [][]driver.Value{[]driver.Value{5}},
})
err = sqlCacher.PutQueryRows("SELECT 5", []driver.Value{}, cacheRows, 1*time.Minute)
if err != nil {
t.Fatal("Unable to precache rows for test", err)
}
rows, err := db.Query("SELECT 5")
if err != nil {
t.Fatal("Could not select 5", err)
}
defer rows.Close()
var n int
ok := rows.Next()
if !ok {
t.Fatal("Could not get next")
}
rows.Scan(&n)
if n != 5 {
t.Fatal("Could not read result: ", n)
}
if !sqlCacheSpy.WasCalled("GetQueryRows") {
t.Fatal("SqlCache GetQueryRows should have been checked")
}
if mockConn.WasCalled("Query") {
t.Logf("%#v", mockConn.Spy.Calls)
t.Fatal("Query should not have been called since cache was not empty")
}
if sqlCacheSpy.WasCalled("PutQueryRows") {
t.Logf("%#v", mockConn.Spy.Calls)
t.Fatal("Query results should not have been re-cached")
}
}