UNPKG

10.7 kBJavaScriptView Raw
1const assert = require('assert');
2let redis = require("redis");
3let client = redis.createClient();
4let Cache = require('../namespace/cache');
5let productionCache = new Cache({redis: client}, {development : false});
6let developmentCache = new Cache({redis: client}, {development : true});
7
8describe('Glad.cache', function () {
9
10 it('should cache an object and retrieve it', function () {
11 return productionCache.store('test001', {foo : true})
12 .then(() => productionCache.get('test001'))
13 .then(data => {
14 assert.deepEqual(data, {foo: true});
15 });
16 });
17
18 it('should cache a string and retrieve it', function () {
19 return productionCache.store('stringtest', 'testdata')
20 .then(() => productionCache.get('stringtest'))
21 .then(data => {
22 assert.equal(data, 'testdata');
23 });
24 });
25
26 it('should cache an object, retrieve it, delete it', function () {
27 return productionCache.store('test001', {foo : true})
28 .then(() => productionCache.get('test001'))
29 .then(data => {
30 return assert.deepEqual(data, {foo: true});
31 })
32 .then(() => productionCache.clear('test001'))
33 .then(() => productionCache.get('test001'))
34 .then(data => {
35 return assert.equal(data, false);
36 });
37 });
38
39 it('cache.clear should only remove the key passed in, when one is present', function () {
40 return productionCache.store('test001', {foo : true})
41 .then(() => productionCache.store('test002', {foo : false}))
42 .then(() => productionCache.get('test001'))
43 .then(data => {
44 return assert.deepEqual(data, {foo: true});
45 })
46 .then(() => productionCache.get('test002'))
47 .then(data => {
48 return assert.deepEqual(data, {foo: false});
49 })
50 .then(() => productionCache.clear('test001'))
51 .then(() => productionCache.get('test001'))
52 .then(data => {
53 return assert.equal(data, false);
54 })
55 .then(() => productionCache.get('test002'))
56 .then(data => {
57 return assert.deepEqual(data, {foo: false});
58 });
59 });
60
61 it('cache.clear should remove all keys passed in, when none are present', function () {
62 return productionCache.store('test001', {foo : true})
63 .then(() => productionCache.store('test002', {foo : false}))
64 .then(() => productionCache.get('test001'))
65 .then(data => {
66 return assert.deepEqual(data, {foo: true});
67 })
68 .then(() => productionCache.get('test002'))
69 .then(data => {
70 return assert.deepEqual(data, {foo: false});
71 })
72 .then(() => productionCache.clear())
73 .then(() => productionCache.get('test001'))
74 .then(data => {
75 return assert.equal(data, false);
76 })
77 .then(() => productionCache.get('test002'))
78 .then(data => {
79 return assert.equal(data, false);
80 })
81 });
82
83 it('cache.resolve should resolve the outer promise when a hit is found, and cache a string', function () {
84 return productionCache.store('test001', 'testdata')
85 .then(() => productionCache.get('test001'))
86 .then(data => {
87 return assert.equal(data, 'testdata');
88 })
89 .then(() => {
90 return new Promise( (resolve, reject) => {
91 productionCache.resolve('test001', resolve).miss(() => {
92 throw new Error('Miss should not be called');
93 });
94 });
95 })
96 .then(() => {
97 return true;
98 })
99 });
100
101 it('cache.resolve should cache a number', function () {
102 return productionCache.store('test001', 54321)
103 .then(() => productionCache.get('test001'))
104 .then(data => {
105 return assert.equal(data, 54321);
106 })
107 .then(() => {
108 return new Promise( (resolve, reject) => {
109 productionCache.resolve('test001', resolve).miss(() => {
110 throw new Error('Miss should not be called');
111 });
112 });
113 })
114 .then(() => {
115 return true;
116 })
117 });
118
119 it('cache.resolve should cache an object', function () {
120 return productionCache.store('test001', {foo : '98'})
121 .then(() => productionCache.get('test001'))
122 .then(data => {
123 return assert.deepEqual(data, {foo : '98'});
124 })
125 .then(() => {
126 return new Promise( (resolve, reject) => {
127 productionCache.resolve('test001', resolve).miss(() => {
128 throw new Error('Miss should not be called');
129 });
130 });
131 })
132 .then(() => {
133 return true;
134 })
135 });
136
137 it('cache.list should list all keys', function () {
138 return productionCache.clear()
139 .then(() => productionCache.store('test001', 'testing-001'))
140 .then(() => productionCache.store('test002', 'testing-002'))
141 .then(() => productionCache.store('test003', 'testing-003'))
142 .then(() => productionCache.store('test004', 'testing-004'))
143 .then(() => productionCache.store('non-match', 'testing-non-match'))
144 .then(() => productionCache.list())
145 .then(keys => assert.deepEqual(keys.sort((a,b) => a < b), ['test001', 'test002', 'test003', 'test004', 'non-match'].sort((a,b) => a < b)))
146 });
147
148 it('cache.list should list only matching keys when a pattern is provided', function () {
149 return productionCache.clear()
150 .then(() => productionCache.store('test001', 'testing-001'))
151 .then(() => productionCache.store('test002', 'testing-002'))
152 .then(() => productionCache.store('test003', 'testing-003'))
153 .then(() => productionCache.store('test004', 'testing-004'))
154 .then(() => productionCache.store('non-match', 'testing-non-match'))
155 .then(() => productionCache.list('test*'))
156 .then(keys => assert.deepEqual(keys.sort((a,b) => a < b), ['test001', 'test002', 'test003', 'test004'].sort((a,b) => a < b)))
157 });
158
159 it('cache.list should list only matching keys when a pattern is provided (2)', function () {
160 return productionCache.clear()
161 .then(() => productionCache.store('test001', 'testing-001'))
162 .then(() => productionCache.store('test002', 'testing-002'))
163 .then(() => productionCache.store('test003', 'testing-003'))
164 .then(() => productionCache.store('test004', 'testing-004'))
165 .then(() => productionCache.store('non-match', 'testing-non-match'))
166 .then(() => productionCache.list('non*'))
167 .then(keys => assert.deepEqual(keys, ['non-match']))
168 });
169
170 it('cache.clearWhere should remove only matching keys (1)', function () {
171 return productionCache.clear()
172 .then(() => productionCache.store('test001', 'testing-001'))
173 .then(() => productionCache.store('test002', 'testing-002'))
174 .then(() => productionCache.store('test003', 'testing-003'))
175 .then(() => productionCache.store('test004', 'testing-004'))
176 .then(() => productionCache.store('non-match', 'testing-non-match'))
177 .then(() => productionCache.clearWhere('test*'))
178 .then(() => productionCache.list())
179 .then(keys => assert.deepEqual(keys, ['non-match']))
180 .then(() => productionCache.get('non-match')
181 .then(data => assert.equal(data, 'testing-non-match')));
182 });
183
184 it('cache.clearWhere should remove only matching keys (2)', function () {
185 return productionCache.clear()
186 .then(() => productionCache.store('widgets/32', 'testing-001'))
187 .then(() => productionCache.store('widgets/41', 'testing-002'))
188 .then(() => productionCache.store('widgets/190', 'testing-003'))
189 .then(() => productionCache.store('widgets/720', 'testing-004'))
190 .then(() => productionCache.store('non-match', 'testing-non-match'))
191 .then(() => productionCache.clearWhere('widgets/*'))
192 .then(() => productionCache.list())
193 .then(keys => assert.deepEqual(keys, ['non-match']))
194 .then(() => productionCache.get('non-match')
195 .then(data => assert.equal(data, 'testing-non-match')));
196 });
197
198 it('cache.clearWhere should remove only matching keys (3)', function () {
199 return productionCache.clear()
200 .then(() => productionCache.store('widgets/32', 'testing-001'))
201 .then(() => productionCache.store('widgets/41', 'testing-002'))
202 .then(() => productionCache.store('widgets/190', 'testing-003'))
203 .then(() => productionCache.store('widgets/720', 'testing-004'))
204 .then(() => productionCache.store('foo/widgets/720', 'testing-004'))
205 .then(() => productionCache.store('non-match', 'testing-non-match'))
206 .then(() => productionCache.clearWhere('widgets/*'))
207 .then(() => productionCache.list())
208 .then(keys => assert.deepEqual(keys.sort((a,b) => a < b), ['non-match', 'foo/widgets/720'].sort((a,b) => a < b)))
209 .then(() => productionCache.get('non-match')
210 .then(data => assert.equal(data, 'testing-non-match')));
211 });
212
213 it('cache.clearWhere should remove only matching keys (4)', function () {
214 return productionCache.clear()
215 .then(() => productionCache.store('widgets/32', 'testing-001'))
216 .then(() => productionCache.store('widgets/41', 'testing-002'))
217 .then(() => productionCache.store('widgets/190', 'testing-003'))
218 .then(() => productionCache.store('widgets/720', 'testing-004'))
219 .then(() => productionCache.store('foo/widgets/720', 'testing-004'))
220 .then(() => productionCache.store('non-match', 'testing-non-match'))
221 .then(() => productionCache.clearWhere('*widgets/*'))
222 .then(() => productionCache.list())
223 .then(keys => assert.deepEqual(keys, ['non-match']))
224 .then(() => productionCache.get('non-match')
225 .then(data => assert.equal(data, 'testing-non-match')));
226 });
227
228 it('caching should be disabled in development', function () {
229 return productionCache.clear()
230 .then(() => developmentCache.store('development', 'nope'))
231 .then(() => developmentCache.get('development'))
232 .then(result => assert.equal(result, false));
233 });
234
235 it('cache.resolve should always hit the miss function in development', function () {
236 return developmentCache.store('dev', 100)
237 .then(() => developmentCache.get('test'))
238 .then(data => {
239 return assert.equal(data, false);
240 })
241 .then(() => {
242 return new Promise( (resolve, reject) => {
243 developmentCache.resolve('dev', resolve).miss((cache) => {
244 cache(100);
245 resolve('miss');
246 });
247 });
248 })
249 .then(result => {
250 return assert.equal(result, 'miss');
251 })
252 .then(() => {
253 return new Promise( (resolve, reject) => {
254 developmentCache.resolve('dev', resolve).miss((cache) => {
255 resolve('miss');
256 });
257 });
258 })
259 .then(result => {
260 return assert.equal(result, 'miss');
261 })
262 });
263
264});