forked from jaredwray/cacheable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaching.test.ts
201 lines (175 loc) · 5.5 KB
/
caching.test.ts
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
191
192
193
194
195
196
197
198
199
200
201
import { describe, it, beforeEach, expect } from 'vitest';
import { faker } from '@faker-js/faker';
import { caching, Cache, MemoryConfig, memoryStore } from '../src';
import { sleep } from './utils';
describe('caching', () => {
let cache: Cache;
let key: string;
const defaultTtl = 100;
let value: string;
describe('constructor', () => {
it('should from store', async () => {
const store = memoryStore();
await expect(caching(store)).resolves.toBeDefined();
});
});
describe('get() and set()', () => {
beforeEach(async () => {
cache = await caching('memory');
key = faker.random.alpha(20);
value = faker.datatype.string();
});
it('lets us set and get data in cache', async () => {
await cache.set(key, value, defaultTtl);
await sleep(20);
await expect(cache.get(key)).resolves.toEqual(value);
});
it('should error no isCacheable value', () =>
expect(cache.set(key, undefined)).rejects.toStrictEqual(
new Error('no cacheable value undefined'),
));
it('should error no isCacheable value', () =>
expect(cache.store.mset([[key, undefined]])).rejects.toStrictEqual(
new Error('no cacheable value undefined'),
));
it('lets us set and get data without a callback', async () => {
cache = await caching(async (arg?: MemoryConfig) => memoryStore(arg));
await cache.set(key, value, defaultTtl);
await sleep(20);
await expect(cache.get(key)).resolves.toEqual(value);
});
it('lets us set and get data without options object or callback', async () => {
cache = await caching(async (arg?: MemoryConfig) => memoryStore(arg));
await cache.set(key, value);
await sleep(20);
await expect(cache.get(key)).resolves.toEqual(value);
});
});
describe('mget() and mset()', function () {
let key2: string;
let value2: string;
const store = 'memory';
beforeEach(async () => {
key = faker.datatype.string(20);
value = faker.datatype.string();
key2 = faker.datatype.string(20);
value2 = faker.datatype.string();
cache = await caching(store, {
ttl: defaultTtl,
});
});
it('lets us set and get several keys and data in cache', async () => {
await cache.store.mset(
[
[key, value],
[key2, value2],
],
defaultTtl,
);
await sleep(20);
await expect(cache.store.mget(key, key2)).resolves.toStrictEqual([
value,
value2,
]);
});
it('lets us set and get data without options', async () => {
await cache.store.mset(
[
[key, value],
[key2, value2],
],
defaultTtl,
);
await sleep(20);
await expect(cache.store.mget(key, key2)).resolves.toStrictEqual([
value,
value2,
]);
});
});
describe('del()', function () {
beforeEach(async () => {
cache = await caching('memory');
key = faker.datatype.string(20);
value = faker.datatype.string();
await cache.set(key, value, defaultTtl);
});
it('deletes data from cache', async () => {
await expect(cache.get(key)).resolves.toEqual(value);
await cache.del(key);
await expect(cache.get(key)).resolves.toBeUndefined();
});
describe('with multiple keys', function () {
let key2: string;
let value2: string;
beforeEach(async () => {
cache = await caching('memory');
key2 = faker.datatype.string(20);
value2 = faker.datatype.string();
await cache.store.mset(
[
[key, value],
[key2, value2],
],
defaultTtl,
);
});
it('deletes an an array of keys', async () => {
await expect(cache.store.mget(key, key2)).resolves.toStrictEqual([
value,
value2,
]);
await cache.store.mdel(key, key2);
await expect(cache.store.mget(key, key2)).resolves.toStrictEqual([
undefined,
undefined,
]);
});
});
});
describe('reset()', () => {
let key2: string;
let value2: string;
beforeEach(async () => {
cache = await caching('memory');
key = faker.datatype.string(20);
value = faker.datatype.string();
await cache.set(key, value);
key2 = faker.datatype.string(20);
value2 = faker.datatype.string();
await cache.set(key2, value2);
});
it('clears the cache', async () => {
await cache.reset();
await expect(cache.get(key)).resolves.toBeUndefined();
await expect(cache.get(key2)).resolves.toBeUndefined();
});
});
describe('keys()', () => {
let keyCount: number;
let savedKeys: string[];
beforeEach(async () => {
keyCount = 10;
cache = await caching('memory');
savedKeys = (
await Promise.all(
Array.from({ length: keyCount }).map(async (_, i) => {
const key =
(i % 3 === 0 ? 'prefix' : '') + faker.datatype.string(20);
value = faker.datatype.string();
await cache.set(key, value);
return key;
}),
)
).sort((a, b) => a.localeCompare(b));
});
it('calls back with all keys in cache', () =>
expect(
cache.store.keys().then((x) => x.sort((a, b) => a.localeCompare(b))),
).resolves.toStrictEqual(savedKeys));
});
describe('issues', () => {
it('#183', () =>
expect(cache.wrap('constructor', async () => 0)).resolves.toEqual(0));
});
});