-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqcache_test.go
160 lines (119 loc) · 3 KB
/
qcache_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
package qcache
import (
"bytes"
"fmt"
"log"
"testing"
)
// testing if we can create a cache store
func TestCreateCacheStore(t *testing.T) {
qcs, err := NewQCStore()
if err != nil {
log.Println("Error creating cache store:", err)
return
}
// does the cache store exist?
if qcs == nil {
t.Fatalf("Cache store is nil")
return
}
fmt.Println("Created cache store")
}
func TestCreateAddValuesToCacheStore(t *testing.T) {
log.Println("Testing the cache store")
qcs, err := NewQCStore()
if err != nil {
log.Println("Error creating cache store:", err)
return
}
// does the cache store exist?
if qcs == nil {
t.Fatalf("Cache store is nil")
return
}
fmt.Println("Created cache store")
testStringKey := "testStringKey"
testByteKey := "testByteKey"
testStringValue := "testStringValue"
testByteValue := []byte("testByteValue")
// set a string value in the cache
err = qcs.SetStr(testStringKey, testStringValue)
if err != nil {
log.Println("Error setting string value in cache store:", err)
return
}
// set a byte array value in the cache
err = qcs.SetBytes(testByteKey, testByteValue)
if err != nil {
log.Println("Error setting byte array value in cache store:", err)
return
}
fmt.Println("Added values to cache store")
// check if the values are in the cache
// get a string value from the cache
strVal, err := qcs.GetStr(testStringKey)
if err != nil {
log.Println("Error getting string value from cache store:", err)
return
}
if strVal.Type != TYPE_STRING {
t.Fatalf("Value is not a string")
return
}
strValActual := strVal.Value.(string)
if strValActual != testStringValue {
t.Fatalf("Value is not the expected string")
return
}
// does our strVal match our testStringValue?
if strVal.Value != testStringValue {
t.Fatalf("String value does not match testStringValue")
return
}
// get a byte array value from the cache
byteVal, err := qcs.GetBytes(testByteKey)
if err != nil {
log.Println("Error getting byte array value from cache store:", err)
return
}
if byteVal.Type != TYPE_BYTES {
t.Fatalf("Value is not a byte array")
return
}
byteValActual := byteVal.Value.([]byte)
// does our byteVal match our testByteValue?
areBytesEqual := bytes.Equal(byteValActual, testByteValue)
if !areBytesEqual {
t.Fatalf("Byte array value does not match testByteValue")
return
}
// what are our keys in the cache?
allKeys, err := qcs.ListAllKeys()
if err != nil {
log.Println("Error listing keys in cache store:", err)
return
}
// is the testStringKey in the cache?
isStringKeyInCache := false
for _, key := range allKeys {
if key == testStringKey {
isStringKeyInCache = true
}
}
if !isStringKeyInCache {
t.Fatalf("String key is not in cache")
return
}
// is the testByteKey in the cache?
isByteKeyInCache := false
for _, key := range allKeys {
if key == testByteKey {
isByteKeyInCache = true
}
}
if !isByteKeyInCache {
t.Fatalf("Byte key is not in cache")
return
}
log.Println("Cache store is working!")
}